import * as util from 'util' import { err } from './logger' export const CustomErrorContextSymbol = Symbol('CustomErrorContextSymbol') process.on('uncaughtException', (error: Error) => { console.error(error.stack) process.exit(1) }) /** * 创建匿名错误消息类 ,供外部 throw new [name](message?: string) AnonymousError * @param name 错误名 * @param code 错误号 * @param message 错误消息 */ function AE (name: string, code: number, msg?: string) { return class AnonymousError extends CustomError { constructor (message?: string | Error) { super(message) this.name = name this.code = code this[CustomErrorContextSymbol] = name let mess = [] if (msg) { mess.push(msg) } if (this.message) { mess.push(this.message) } this.message = mess.join(' -- ') } } } /** * 自定义错误 */ export class CustomError { [key: string]: any [CustomErrorContextSymbol]: any constructor (err?: string | Error) { if (err instanceof Error) { this.stack = err.stack this.message = err.message } else { if (typeof err === 'string') { this.message = err.toString() } Error.captureStackTrace(this, this.constructor) } this.name = this.constructor.name this[CustomErrorContextSymbol] = this.constructor.name } } util.inherits(CustomError, Error) /** * 自定义错误集合 */ export const UnKnownError = AE('UnKnownError', 500, '未知错误')