Closed eisneim closed 9 years ago
https://github.com/cojs/co-body/blob/master/lib/json.js#L36
error is thrown here, you can add this in your custom error handler:
try {
yield bodyParser({
formLimit: '200kb',
jsonLimit: '1mb',
}).call(this,next)
} catch (err) {}
stupid me!! just after i post the issue , i realized i can do it in that way! thanks anyway
this method will also have a problem, it will caught not only the JSON parse error but all the nested middleware's error and router's error , which means, app.on('error') will become useless.
how to make this better?
maybe we can add options.onerror
for koa-bodyparser
to support custom error handler?
it would be great ! my current solution is: add global error handler and check error type
if((err instanceof SyntaxError) && err.body ){
error = {
msg:'JSON对象语法错误,请检查',
detail: err.toString(),
}
}
koa-bodyparser
@1.6.0
pretty efficient!
some guy may try to post some wired string and set header to be json, i can caught the error but don't know how to response a custom message that tell them," dude, your json is invalid !"
i tried something like this:
i looked the source code, there is no error handling;