如何使用响应或自定义错误处理 "PayloadTooLargeError"?
How to handle "PayloadTooLargeError" with a response or a custom error?
我正在尝试使用 :
设置 1kb 的负载限制
app.use(bodyParser.json({
limit: "1kb" ,
strict:true
}));
它工作得很好,因为它应该工作,但唯一的问题是我得到一个错误 of
PayloadTooLargeError: request entity too large
at readStream (E:\Work\FIGHTME\V1.0\api\node_modules\raw-body\index.js:155:17)
at getRawBody (E:\Work\FIGHTME\V1.0\api\node_modules\raw-body\index.js:108:12)
at read (E:\Work\FIGHTME\V1.0\api\node_modules\body-parser\lib\read.js:77:3)
at jsonParser (E:\Work\FIGHTME\V1.0\api\node_modules\body-parser\lib\types\json.js:135:5)
at Layer.handle [as handle_request] (E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\router\index.js:317:13)
at E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\router\index.js:284:7
at Function.process_params (E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\router\index.js:335:12)
at next (E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\router\index.js:275:10)
at expressInit (E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\middleware\init.js:40:5)
is there any way to handle this error through callback or anything so i show a custom error or send a custom response ?
你试过这样吗
app.use(express.json({limit: '1kb'}));
在 Express v4.16.0 及更高版本中支持将请求正文解析为中间件。
编辑
如果您想自定义错误消息,则需要定义错误处理程序中间件来执行此操作。像这样:
// Not tested code.
// Usually added as last middleware
app.use(function (err, req, res, next) {
if(err instanceof PayloadTooLargeError){
//res.status(<your status code>).send(<your response>);
res.status(400).send("Customized Response");
}
else{
next(err);
}
})
我正在尝试使用 :
设置 1kb 的负载限制app.use(bodyParser.json({
limit: "1kb" ,
strict:true
}));
它工作得很好,因为它应该工作,但唯一的问题是我得到一个错误 of
PayloadTooLargeError: request entity too large
at readStream (E:\Work\FIGHTME\V1.0\api\node_modules\raw-body\index.js:155:17)
at getRawBody (E:\Work\FIGHTME\V1.0\api\node_modules\raw-body\index.js:108:12)
at read (E:\Work\FIGHTME\V1.0\api\node_modules\body-parser\lib\read.js:77:3)
at jsonParser (E:\Work\FIGHTME\V1.0\api\node_modules\body-parser\lib\types\json.js:135:5)
at Layer.handle [as handle_request] (E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\router\index.js:317:13)
at E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\router\index.js:284:7
at Function.process_params (E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\router\index.js:335:12)
at next (E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\router\index.js:275:10)
at expressInit (E:\Work\FIGHTME\V1.0\api\node_modules\express\lib\middleware\init.js:40:5)
is there any way to handle this error through callback or anything so i show a custom error or send a custom response ?
你试过这样吗
app.use(express.json({limit: '1kb'}));
在 Express v4.16.0 及更高版本中支持将请求正文解析为中间件。
编辑
如果您想自定义错误消息,则需要定义错误处理程序中间件来执行此操作。像这样:
// Not tested code.
// Usually added as last middleware
app.use(function (err, req, res, next) {
if(err instanceof PayloadTooLargeError){
//res.status(<your status code>).send(<your response>);
res.status(400).send("Customized Response");
}
else{
next(err);
}
})