UNPKG

ts-api-core

Version:

Nodejs api framework core

59 lines 2.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ParamError = exports.BaseError = void 0; const result_1 = require("../../base/result"); class BaseError extends Error { constructor(code = result_1.ResultCode.BASE, msg = BaseError._FAILURE_MSG, data = undefined, errorName = 'BaseError') { super(msg); this._code = code; this.data = data; this.name = errorName; } get code() { return this._code; } set code(value) { this._code = value; } getResponseObject() { return new result_1.Result(this.code, this.message, this.data); } /** * 抛出一个默认的错误,允许自定义消息 * @param msg 消息 * @param data 返回的data */ static throwDefaultError(msg = BaseError._FAILURE_MSG, data = undefined) { throw new BaseError(result_1.ResultCode.BASE, msg, data); } /** * 包装错误对象并向外抛出 * @param error 错误对象 * @param action 正在执行的动作 * @param data 返回的data */ static throwError(error, action = '', data = undefined) { if (error) { console.error(`执行${action}时报错:${error.message}`); if (error instanceof BaseError) { throw error; } if (action) { throw new BaseError(result_1.ResultCode.BASE, `执行${action}时报错:${error.message}`, data); } throw new BaseError(result_1.ResultCode.BASE, error.message, data); } } } exports.BaseError = BaseError; BaseError._FAILURE_MSG = '未知异常,请联系管理员'; /** * 参数错误 */ class ParamError extends BaseError { constructor(data) { super(result_1.ResultCode.BASE, `参数校验错误,${data}`, undefined, 'ParamError'); } } exports.ParamError = ParamError; //# sourceMappingURL=mvc.error.js.map