node项目中的错误处理
node中Error对象的使用
使用captureStackTrace方法加入自带的错误信息
// Error对象自带的属性
Error.captureStackTrace// 如何使用captureStackTrace
var obj = {
message: 'something is wrong'
}
Error.captureStackTrace(obj)
throw obj // 此时会抛出obj对象的message内信息
使用try catch捕获错误
直接把代码写在try catch中即可捕获错误信息
try{
throw new Error('oh no')
}catch(e){
console.log(e)
}
在异步代码中,直接try catch是无法捕获错误信息的,可以使用如下方法
function foo(params, cb){
const error = new Error('something is wrong')
if(error) cb(error)
}
以上使用callback方式来做错误处理比较容易麻烦,容易出错,现在node已经支持async await所以尽量使用它们准没错
async function foo(){
try{
await bar()
}catch(e){
console.log(e)
}
}async function bar(){
throw new Error('async function got wrong)
}
foo()
基本错误类型
在项目会有多个地方对错误信息进行处理,所以先写一个基本错误类型,方便使用
// 基本错误类型
class HttpBaseError extends Error {
constructor(httpStatusCode, httpMsg, errCode, msg) {
super(`HTTP ERROR: ${msg}`);
this.httpStatusCode = httpStatusCode;
this.httpMsg = httpMsg;
this.errCode = errCode;
}
}try {
// 直接抛出定义好的错误即可
throw new HttpBaseError(404, '资源不存在', 10000, 'resouse is not found');
} catch (e) {
console.log(e.message);
console.log(e.httpStatusCode);
console.log(e.httpMsg);
console.log(e.errCode);
}
特定错误类型
除了基本类型,不同情况下会有不同错误信息,需要用一个特定的错误类型来处理特定的错误信息
// 一个参数错误类型
const ERROR_CODE = 40000 // 错误码
class HttpRequestParamError extends HttpBaseError {
constructor(paramName, desc, msg) {
super(200, desc, ERROR_CODE, `${paramName} wrong: ${msg}`)
}
}
这样,在参数错误的地方就能非常方便的调用这个错误类型来返回错误
抛错的逻辑
错误处理中,model,controller中的错误,有些是不能直接返回给用户的,应该只返回给model或controller的调用者。
使用错误处理









