graceful-error
Advanced tools
Comparing version 1.1.4 to 1.1.5
@@ -1,18 +0,23 @@ | ||
interface IBaseOptions { | ||
interface IOptions { | ||
message: string; | ||
code: string | number; | ||
message: string; | ||
msg: string; | ||
appId?: string; | ||
logIndex?: string; | ||
level?: string; | ||
errorType?: string; | ||
feature?: string; | ||
} | ||
interface IGracefulErrorOptions extends IBaseOptions { | ||
context: object; | ||
interface IContext { | ||
[key: string]: any; | ||
} | ||
interface IGracefulErrorOptions extends IOptions { | ||
context: IContext; | ||
} | ||
interface IGracefulErrorToJSONResult extends IBaseOptions { | ||
interface IGracefulErrorToJSONResult extends IOptions { | ||
name: string; | ||
stack: string; | ||
context: object; | ||
context: IContext; | ||
} | ||
@@ -26,4 +31,4 @@ | ||
export function createError(options: IBaseOptions, context?: object): GracefulError; | ||
export function wrapError(err: any, options?: IBaseOptions, context?: object): GracefulError; | ||
export function createError(options: IOptions, context?: IContext): GracefulError; | ||
export function wrapError(err: any, options?: IOptions, context?: IContext): GracefulError; | ||
export enum ErrorLevelEnum { | ||
@@ -30,0 +35,0 @@ INFO, |
class GracefulError extends Error { | ||
constructor({ message, appId, logIndex, code, msg, level, feature, context }) { | ||
constructor({ message, appId, logIndex, code, msg, level, errorType, feature, context }) { | ||
super(message); | ||
@@ -10,2 +10,3 @@ this.name = 'GracefulError'; | ||
this.level = level; | ||
this.errorType = errorType; | ||
this.feature = feature; | ||
@@ -28,2 +29,3 @@ this.context = context; | ||
level: this.level, | ||
errorType: this.errorType, | ||
feature: this.feature, | ||
@@ -44,4 +46,4 @@ context: this.context, | ||
function createError({ message, appId, logIndex, code, msg, level, feature }, context = {}) { | ||
let err = new GracefulError({ message, appId, logIndex, code, msg, level, feature, context }); | ||
function createError({ message, appId, logIndex, code, msg, level, errorType, feature }, context = {}) { | ||
let err = new GracefulError({ message, appId, logIndex, code, msg, level, errorType, feature, context }); | ||
return err; | ||
@@ -61,2 +63,3 @@ } | ||
level: (options && options.level) || err.level, | ||
errorType: (options && options.errorType) || err.errorType, | ||
feature: (options && options.feature) || err.feature, | ||
@@ -63,0 +66,0 @@ context, |
{ | ||
"name": "graceful-error", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,8 +5,8 @@ # graceful-error | ||
GracefulError 在 Node 原生错误对象 Error 的基础之上,新增了 7 个属性,如下所示。 | ||
GracefulError 在 Node 原生错误对象 Error 的基础之上,新增了 8 个属性,如下所示。 | ||
#### Node 原生错误对象自有属性 | ||
- name 错误类名 | ||
- message 错误描述信息 | ||
- name 错误名 | ||
- message 错误描述信息,用于日志打印 | ||
- stack 错误堆栈 | ||
@@ -16,18 +16,15 @@ | ||
- code 错误唯一标识,可以是数字,也可以是字符串 | ||
- msg 错误提示信息,返回给前端展示用的错误提示信息 | ||
- level 错误级别,例如:info、warn、error | ||
- errorType 错误类型 | ||
- appId 应用唯一标识 | ||
- logIndex 日志索引 | ||
- code 错误码 | ||
- msg 错误提示信息 | ||
- level 错误级别,例如:info、warn、error | ||
- feature 业务标识 | ||
- context 错误上下文对象,适用于存储一些错误上下文信息 | ||
> 注意:code 和 msg 一般返回给前端使用,其他字段用于日志打印,记录错误发生时的一些信息,如下所示。 | ||
> 注意: | ||
```json | ||
{ | ||
"code": "NETWORK_ERROR", | ||
"msg": "网络错误,请稍后重试" | ||
} | ||
``` | ||
- code / msg 返回给前端展示用的错误码和错误提示信息; | ||
- appId / logIndex 后端日志上报应用标识及日志索引; | ||
@@ -63,11 +60,14 @@ ### 二、API | ||
- options 错误配置对象 | ||
- options.message 错误描述信息 | ||
- options.appId 应用唯一标识 | ||
- options.logIndex 日志索引 | ||
- options.code 错误码 | ||
- options.msg 错误提示信息 | ||
- options.level 错误级别,例如:info、warn、error | ||
- options.feature 业务标识 | ||
- context 错误上下文对象,适用于存储一些错误上下文信息 | ||
- options.message 必传,错误描述信息 | ||
- options.code 必传,错误码 | ||
- options.msg 必传,错误提示信息 | ||
- options.level 可选,错误级别,例如:info、warn、error | ||
- options.errorType 可选,错误类型 | ||
- options.appId 可选,应用唯一标识 | ||
- options.logIndex 可选,日志索引 | ||
- options.feature 可选,业务标识 | ||
- context 可选,错误上下文对象,适用于存储一些错误上下文信息 | ||
示例代码: | ||
@@ -80,7 +80,8 @@ | ||
message: 'Invalid HTTP header value.', | ||
code: 'ERR_HTTP_INVALID_HEADER_VALUE', | ||
msg: '请求参数不合理', | ||
level: ErrorLevelEnum.ERROR, | ||
errorType: 'demo_type', | ||
appId: 'demo', | ||
logIndex: 'demo_log_index', | ||
code: 'ERR_HTTP_INVALID_HEADER_VALUE', | ||
msg: 'HTTP 请求头非法', | ||
level: ErrorLevelEnum.ERROR, | ||
feature: 'demo_feature', | ||
@@ -87,0 +88,0 @@ }, |
7765
101
115