From 2b0de525f6e67e7d24f4db0685f75ae9e758489d Mon Sep 17 00:00:00 2001 From: lizhen Date: Tue, 23 Aug 2022 23:29:22 +0800 Subject: [PATCH] feat: add config --- lib/decorator/index.ts | 27 +++++++++++++++++++++++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/decorator/index.ts b/lib/decorator/index.ts index 084747e..9caab93 100644 --- a/lib/decorator/index.ts +++ b/lib/decorator/index.ts @@ -338,9 +338,36 @@ export const defaultErrorWrapper = (error: any): ResponseData => { return { success: false, errorMessage: error.message, + errorCode: error.code, }; }; +export type ResponseErrorProps = + | string + | { + message: string; + code?: string; + error?: Error; + }; +export class ResponseError implements Error { + name: string = 'ResponseError'; + stack?: string; + message: string; + code?: string; + constructor(props: ResponseErrorProps) { + if (typeof props === 'string') { + this.message = props; + } else { + this.message = props.message; + this.code = props.code; + this.stack = props.error?.stack; + } + } + public toString() { + return `${this.name}${this.code ? `(code:${this.code})` : ''}: ${this.message}`; + } +} + export type ResponseJsonOptions = { /** * 用来转换返回的结果对象到标准的json diff --git a/package-lock.json b/package-lock.json index 3705335..e6b8e2e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "egg-fancy-decorator", - "version": "1.0.4", + "version": "1.0.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "egg-fancy-decorator", - "version": "1.0.4", + "version": "1.0.5", "license": "MIT", "dependencies": { "lodash": "^4.17.21", diff --git a/package.json b/package.json index f1df53d..9ea6c37 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "egg-fancy-decorator", - "version": "1.0.4", + "version": "1.0.5", "description": "Decorator support plugin for egg framework", "main": "lib/index.js", "types": "index.d.ts",