graceful-error
Advanced tools
Comparing version 1.0.0 to 1.1.0
23
index.js
@@ -33,9 +33,10 @@ class GracefulError extends Error { | ||
INFO: 'info', | ||
NOTICE: 'notice', | ||
ALERT: 'alert', | ||
WARN: 'warn', | ||
ERROR: 'error', | ||
ALERT: 'alert', | ||
CRITICAL: 'critical', | ||
}; | ||
function createError({ message, appId, logIndex, code, level, feature }, context) { | ||
function createError({ message, appId, logIndex, code, level, feature }, context = {}) { | ||
let err = new GracefulError({ message, appId, logIndex, code, level, feature, context }); | ||
@@ -45,5 +46,23 @@ return err; | ||
function wrapError(err, options = {}, context = {}) { | ||
if (err.name === 'GracefulError') { | ||
return err; | ||
} | ||
let error = new GracefulError({ | ||
message: (options && options.message) || err.message, | ||
appId: (options && options.appId) || err.appId, | ||
logIndex: (options && options.logIndex) || err.logIndex, | ||
code: (options && options.code) || err.code, | ||
level: (options && options.level) || err.level, | ||
feature: (options && options.feature) || err.feature, | ||
context, | ||
}); | ||
error.stack = err.stack; | ||
return error; | ||
} | ||
module.exports = { | ||
createError, | ||
wrapError, | ||
ErrorLevelEnum: Object.freeze(ErrorLevelEnum), | ||
}; |
{ | ||
"name": "graceful-error", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# graceful-error | ||
### 一、基础介绍 | ||
GracefulError 在 Node 原生错误对象 Error 的基础之上,新增了 6 个属性,如下所示。 | ||
#### Node 原生错误对象自有属性 | ||
@@ -9,3 +13,3 @@ | ||
#### 扩展属性 | ||
#### 新增扩展属性 | ||
@@ -19,8 +23,43 @@ - appId 应用唯一标识 | ||
### API | ||
### 二、API | ||
#### 1. ErrorLevelEnum | ||
ErrorLevelEnum 错误级别枚举对象,原始定义如下 | ||
```js | ||
const ErrorLevelEnum = { | ||
INFO: 'info', | ||
NOTICE: 'notice', | ||
ALERT: 'alert', | ||
WARN: 'warn', | ||
ERROR: 'error', | ||
CRITICAL: 'critical', | ||
}; | ||
``` | ||
示例代码: | ||
```js | ||
ErrorLevelEnum.INFO; // 'info' | ||
ErrorLevelEnum.ERROR; // 'error' | ||
``` | ||
#### 2. createError(options, context) | ||
参数说明: | ||
- options 错误配置对象 | ||
- options.message 错误描述信息 | ||
- options.appId 应用唯一标识 | ||
- options.logIndex 日志索引 | ||
- options.code 错误码 | ||
- options.level 错误级别,例如:info、warn、error | ||
- options.feature 业务标识 | ||
- context 错误上下文对象,适用于存储一些错误上下文信息 | ||
示例代码: | ||
```js | ||
const { createError, ErrorLevelEnum } = require('graceful-error'); | ||
// 1.createError | ||
const err = createError( | ||
@@ -39,14 +78,27 @@ { | ||
); | ||
``` | ||
// 2.toJSON | ||
const errJSON = err.toJSON(); | ||
#### 3. wrapError(err, options, context) | ||
// 3.ErrorLevelEnum 可选值 | ||
const ErrorLevelEnum = { | ||
INFO: 'info', | ||
WARN: 'warn', | ||
ERROR: 'error', | ||
ALERT: 'alert', | ||
CRITICAL: 'critical', | ||
}; | ||
将一个错误对象包装成 GracefulError 类型的错误。 | ||
参数说明: | ||
- err 原始的错误对象 | ||
- options 同 createError options 参数 | ||
- context 同 createError context 参数 | ||
```js | ||
const { wrapError } = require('graceful-error'); | ||
const oldError = new Error('hello world'); | ||
const newError = wrapError(oldError); | ||
``` | ||
#### 4. toJSON() | ||
将 err 错误对象转换成一个 JSON 对象 | ||
```js | ||
const errJSON = err.toJSON(); | ||
``` |
6755
87
102