swagger tools error handler middleware not catching errors

冷暖自知 提交于 2019-12-03 12:41:19
Aikon Mogwai

I see two problems.

At first: error handler must have 4 args, so statusCode will be ignored.

errorHandler(err, req, res, next) // correct definition

Second place is

next("the request timed out", null, null, null, 504);

First argument of error handle must be Error object not String, so correct code will be

next(new Error("the request timed out")); // other args passed by closure

There are many variants how pass statusCode.

// 1. Bad way: Pass string with delimiter
next(new Error("the request timed out;404"));
...
// In error handler
var args = err.message.split() // => args[0] = the request timed out, args[1] = 404

// 2. Check message error text
If (err.message == 'the request timed out')  
    statusCode = 404;

// 3. Best way is use custom error

More about custom error here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!