[英]How should I handle all my errors in one place?
I would like to know how I should handle my errors in one place using node.js 我想知道如何使用node.js在一个地方处理错误
This is how I did: 这是我做的:
First I create loggers/errors.js 首先,我创建loggers / errors.js
And inside I have this object: 在里面,我有这个对象:
var Errors = {
bad_request: 'Some error related with bad request'
};
Than, in other files I just require this file, and when some statement or some error can occur, i just call: 然后,在其他文件中,我只需要此文件,并且当某些语句或某些错误可能发生时,我只需调用:
var Errors = require('../loggers/errors');
if (something here) {
console.log(Errors.bad_request);
}
I would like to know if this is a good approach, if I am doing right, or if there is a module that already does what I want, but with extra features. 我想知道这是否是一个好方法,是否做得对,或者是否有一个模块已经可以满足我的要求,但是具有额外的功能。
You could do something like this: 您可以执行以下操作:
module.exports = {
BadRequest: 'Request was bad',
Timeout: 'Server timed out'
}
And require it in the main file 并在主文件中要求它
var Error = require ('../loggers/errors.js');
if(statement) {
console.log (Error.BadRequest );
}
You need to declare module.exports or it won't be able to be required. 您需要声明module.exports,否则将不需要它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.