Catch express bodyParser错误 [英] Catch express bodyParser error

查看:129
本文介绍了Catch express bodyParser错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我发送一个json对象时,我想抓住bodyParser()中间件的错误,因为我想发送一个自定义的响应而不是一般的400错误。



这是我有的,它的作品:

  app.use(express.bodyParser()); 
app.use(function(error,req,res,next){
// Catch bodyParser error
if(error.message ===invalid json){
sendError(res,myCustomErrorMessage);
} else {
next();
}
});

但这似乎对我来说是一个非常丑陋的方法,因为我正在比较可能会改变的错误信息未来快递版本。有没有其他方法可以捕捉bodyParser()错误?



编辑:



这是请求时的错误body有一个无效的json:

  {
stack:'错误:在Object.exports上无效json\\\
。错误(< path> /node_modules/express/node_modules/connect/lib/utils.js:55:13)\\\
IncomingMessage。< anonymous> (< path> /node_modules/express/node_modules/connect/lib/middleware/json.js:74:71)\\\
在_coming_readable的IncomingMessage.EventEmitter.emit(events.js:92:17)\\\
。 js:872:14\\\
at process._tickDomainCallback(node.js:459:13)',
arguments:undefined,
type:undefined,
message:'invalid json'
状态:400
}

漂亮的打印堆栈:

 错误:Object.exports.error(< path> / node_modules / express / node_modules / connect / lib / utils)中的json 
无效.js:55:13)
在IncomingMessage。< anonymous> (< path> /node_modules/express/node_modules/connect/lib/middleware/json.js:74:71)
在IncomingMessage.EventEmitter.emit(events.js:92:17)
at _stream_readable.js:872:14
at process._tickDomainCallback(node.js:459:13)


解决方案

确定,发现:



bodyParser()是json(),urlencoded()和multipart ()。我只需要调用json(),捕获错误并调用urlencoded()和multipart()。



bodyParser source

  app.use(express.json() ); 
app.use(function(error,req,res,next){
// Catch json error
sendError(res,myCustomErrorMessage);
});

app.use(express.urlencoded());
app.use(express.multipart());


I want to catch the error from the bodyParser() middleware when I send a json object and it is invalid because I want to send a custom response instead of a generic 400 error.

This is what I have and it works:

app.use (express.bodyParser ());
app.use (function (error, req, res, next){
    //Catch bodyParser error
    if (error.message === "invalid json"){
        sendError (res, myCustomErrorMessage);
    }else{
        next ();
    }
});

But this seems to me a very ugly approach because I'm comparing the error message which could change in future express versions. There's any other way to catch bodyParser() errors?

EDIT:

This is the error when the request body has an invalid json:

{
  stack: 'Error: invalid json\n    at Object.exports.error (<path>/node_modules/express/node_modules/connect/lib/utils.js:55:13)\n    at IncomingMessage.<anonymous> (<path>/node_modules/express/node_modules/connect/lib/middleware/json.js:74:71)\n    at IncomingMessage.EventEmitter.emit (events.js:92:17)\n    at _stream_readable.js:872:14\n    at process._tickDomainCallback (node.js:459:13)',
  arguments: undefined,
  type: undefined,
  message: 'invalid json',
  status: 400
}

Pretty printed stack:

Error: invalid json
    at Object.exports.error (<path>/node_modules/express/node_modules/connect/lib/utils.js:55:13)
    at IncomingMessage.<anonymous> (<path>/node_modules/express/node_modules/connect/lib/middleware/json.js:74:71)
    at IncomingMessage.EventEmitter.emit (events.js:92:17)
    at _stream_readable.js:872:14
    at process._tickDomainCallback (node.js:459:13)

解决方案

Ok, found it:

bodyParser() is a convenience function for json(), urlencoded() and multipart(). I just need to call to json(), catch the error and call to urlencoded() and multipart().

bodyParser source

app.use (express.json ());
app.use (function (error, req, res, next){
    //Catch json error
    sendError (res, myCustomErrorMessage);
});

app.use (express.urlencoded ());
app.use (express.multipart ());

这篇关于Catch express bodyParser错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆