2016-09-19 4 views
0

Restifyルートにルータハンドラが設定されています。そのハンドラでは、カスタムモジュールを呼び出してエラーチェックを行います。私がエラー状態になったとき、私のコードは次のエラーを返します(エラー)。私はブラウザにエラーメッセージが表示されますが、何らかの理由で私のコードはそれ以降も実行を続けます。Node.jsエラーを返した後にRestireコードが実行されます

Restifyルータハンドラ

HttpHandlers.prototype.callHttp = function(req, res, next) { 
myUtils.checkInputRules(req, res, next, handlerConfig.inputRules); 

//This code is getting executed: 
logger.debug("Updated ... 

と呼ばれている機能:

myUtils.checkInputRules = function checkInputRule(req, res, next, inputRules) { 
... 
     } else { 
      if (inputRule.ifFalse) { 
       var evalStr = inputRule.ifFalse; 
       if (evalStr != null) { 
        logger.debug("Executing condition.iFalse: "+evalStr); 

        //The code is itting this location 
        return next(new Error("Internal Error: Failure.")); 
... 
+0

あなたはちょうどエラーを投げることができますか? – Thomas

+0

@トーマスは、物事を支配するために、コール関数にreturn next(err)をコピーし、それが期待どおりに機能するようにしてから、checkInputRulesの先頭に移動しました。 Restify APIからの私の理解とStackoverflowのここの答えは、return next(err)がRestifyとExpressでエラーを設定する正しい方法だということです。 – user994165

答えて

2

あなたは全体のコードが含まれていませんでしたが、問題は、このようなものになることがありますが、関数から戻ったとき、それはあなたが戻ってくる機能が重要です。たとえば、次のように

function handler(req, res, next) { 
    helper(req, res, next); 
    // this will still run 
} 

function helper(req, res, next) { 
    if (something) return next(); 
} 
ここ

あなたがmyUtils.checkInputRules機能を実行していて、あなたのmyUtils.checkInputRules関数から戻ってきているが、あなたは実際にそうmyUtils.checkInputRules(req, res, next, handlerConfig.inputRules);後、すべてがまだ実行されHttpHandlers.prototype.callHttpから戻っていないようです。

コード全体が表示されませんでしたが、すべて同期しているようです。その場合は次のようなことができます:

function handler(req, res, next) { 
    if (helper(req, res, next)) { 
    // next() was already called 
    } else { 
    // do something else - next() not called yet... 
    } 
} 

function helper(req, res, next) { 
    if (something) { 
    next(); 
    // indicate that next() was already called: 
    return true; 
    } 
    // possibly do something else 
} 
関連する問題