Restify异常被神秘地抛出

Restify exception being mysteriously thrown

我定义了以下 Restify 路由处理程序。

MyModule.prototype.uploadDateChecker = function (req, res, next) {
    req.locals = {};
    var handlerConfig = req._self = this;

    //Uncommenting this line does throw the exception with the message.
    //next(new Error("Test Error"));

    var continueProcessing = commonUtils.processInputs(req, res, next, handlerConfig, __function, __line);
    ...
    //If I uncomment this one instead, some other exception is thrown first somewhere resulting in: {"code":"InternalError","message":""}
    //next(new Error("Test Error: "+continueProcessing));

出于某种原因,在 commonUtils.processInputs 函数 returns 之后,客户端收到 500 错误消息“”。如果我添加一个

next(new Error("Test Error"));

就在调用 processInputs 之前,然后 500 错误在正文中有一个 "Test Error"。如果我将它移到 processInputs 之后,则 500 没有消息。显然,任何消息都从 processInputs 内部抛出另一个异常。但是,如果我进入该 processInputs 并在 return 之前添加 next(new Error("Test Error")) 语句,则客户端会收到测试错误。所以我不知道 how/why 当抛出行被注释掉时会抛出空白消息。

处理输入结束:

...
commonUtils.processOutputs = function (req, res, next, handlerConfig, func, line) {
    var resp = commonUtils.enterExit(req, res, next, handlerConfig, func, line, "START");
    //Uncommenting this line results in: {"message":"Test Error: true"}
    //next(new Error("Test Error: "+resp));

    return resp;

}

没有消息的 HTTP 响应正文:

{"code":"InternalError","message":""}

带有消息的 HTTP 响应正文(在 processInputs 内):

{"message":"Test Error: true"}

事实证明,我在 commonUtils.processInputs 内部调用了 eval,它抛出了一个错误,我立即发现了。然后我调用 next(err.message+"some other text") 而不是 next(new Error(errmessage+"some other text"));由于 "next(err)" 期望 err 是一个 Error 对象,因此抛出了由于语法错误导致的新错误。