node.js 中的生产质量错误处理
Production-quality error handling in node.js
我正在处理 node.js 服务器应用程序中的请求错误处理。我已经定义了一个处理这些错误的回调函数:
app.use(function errorHandler(err, req, res, next) {
res.send(err, {status: err, message: 'error'});
}
);
作为开发人员,这对我来说很好,因为它会打印如下堆栈跟踪:
{
"status": {
"stack": "Error\\n at MongooseError.ValidationError (/home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/error/validation.js:22:16)\\n at model.Document.invalidate (/home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/document.js:1216:32)\\n at /home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/document.js:1090:18\\n at validate (/home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/schematype.js:653:7)\\n at /home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/schematype.js:681:9\\n at Array.forEach (native)\\n at SchemaString.SchemaType.doValidate (/home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/schematype.js:658:19)\\n at /home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/document.js:1088:11\\n at process._tickCallback (node.js:415:13)",
"message": "User validation failed",
"name": "ValidationError",
"errors": {
"email": {
"properties": {
"regexp": {},
"type": "regexp",
"message": "Path `{PATH}` is invalid ({VALUE}).",
"path": "email",
"value": "test@exa@mple.com"
},
"stack": "Error\\n at MongooseError.ValidatorError (/home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/error/validator.js:25:16)\\n at validate (/home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/schematype.js:652:13)\\n at /home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/schematype.js:681:9\\n at Array.forEach (native)\\n at SchemaString.SchemaType.doValidate (/home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/schematype.js:658:19)\\n at /home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/document.js:1088:11\\n at process._tickCallback (node.js:415:13)",
"message": "Path `email` is invalid (test@exa@mple.com).",
"name": "ValidatorError",
"kind": "regexp",
"path": "email",
"value": "test@exa@mple.com"
}
}
},
"message": "error"
}
但是,我想在生产版本中以一种简洁、用户友好的格式显示它,而不会泄露整个堆栈跟踪。现在我可以在每个请求处理函数中指定错误状态和消息,但仍然有特定信息,如上所示,输入的电子邮件无效,我不想为每个检查的字段手动输入由验证者。是否有任何现有的样板可以为我完成这项工作?
Mongoose 验证错误很难处理。我的一般方法是只处理第一个错误(让用户一次处理一个错误)并发送路径和消息,因为其余的不会真正为非开发人员带来太多额外的意义。
first = err.errors[Object.keys(err.errors)[0]]
res.send({
path: first.path,
message: first.message
});
我还建议使用自定义 API 风格的错误格式,您可以坚持使用它来处理所有错误 - 这将使可维护性变得更加容易。
我有一套我依赖的预定义错误模板 - 这是一个。
// If the client missed a required parameter
exports.missingParam = function (res, domain, param) {
res.status(400).send({
status: "failed",
errors: [
{
status: "400",
domain: domain,
reason: "required",
message: "Required parameter: "+param,
locationType: "parameter",
location: param
}
]
});
}
我正在处理 node.js 服务器应用程序中的请求错误处理。我已经定义了一个处理这些错误的回调函数:
app.use(function errorHandler(err, req, res, next) {
res.send(err, {status: err, message: 'error'});
}
);
作为开发人员,这对我来说很好,因为它会打印如下堆栈跟踪:
{
"status": {
"stack": "Error\\n at MongooseError.ValidationError (/home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/error/validation.js:22:16)\\n at model.Document.invalidate (/home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/document.js:1216:32)\\n at /home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/document.js:1090:18\\n at validate (/home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/schematype.js:653:7)\\n at /home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/schematype.js:681:9\\n at Array.forEach (native)\\n at SchemaString.SchemaType.doValidate (/home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/schematype.js:658:19)\\n at /home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/document.js:1088:11\\n at process._tickCallback (node.js:415:13)",
"message": "User validation failed",
"name": "ValidationError",
"errors": {
"email": {
"properties": {
"regexp": {},
"type": "regexp",
"message": "Path `{PATH}` is invalid ({VALUE}).",
"path": "email",
"value": "test@exa@mple.com"
},
"stack": "Error\\n at MongooseError.ValidatorError (/home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/error/validator.js:25:16)\\n at validate (/home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/schematype.js:652:13)\\n at /home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/schematype.js:681:9\\n at Array.forEach (native)\\n at SchemaString.SchemaType.doValidate (/home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/schematype.js:658:19)\\n at /home/osboxes/skipodium_rest_server/node_modules/mongoose/lib/document.js:1088:11\\n at process._tickCallback (node.js:415:13)",
"message": "Path `email` is invalid (test@exa@mple.com).",
"name": "ValidatorError",
"kind": "regexp",
"path": "email",
"value": "test@exa@mple.com"
}
}
},
"message": "error"
}
但是,我想在生产版本中以一种简洁、用户友好的格式显示它,而不会泄露整个堆栈跟踪。现在我可以在每个请求处理函数中指定错误状态和消息,但仍然有特定信息,如上所示,输入的电子邮件无效,我不想为每个检查的字段手动输入由验证者。是否有任何现有的样板可以为我完成这项工作?
Mongoose 验证错误很难处理。我的一般方法是只处理第一个错误(让用户一次处理一个错误)并发送路径和消息,因为其余的不会真正为非开发人员带来太多额外的意义。
first = err.errors[Object.keys(err.errors)[0]]
res.send({
path: first.path,
message: first.message
});
我还建议使用自定义 API 风格的错误格式,您可以坚持使用它来处理所有错误 - 这将使可维护性变得更加容易。
我有一套我依赖的预定义错误模板 - 这是一个。
// If the client missed a required parameter
exports.missingParam = function (res, domain, param) {
res.status(400).send({
status: "failed",
errors: [
{
status: "400",
domain: domain,
reason: "required",
message: "Required parameter: "+param,
locationType: "parameter",
location: param
}
]
});
}