File size: 1,183 Bytes
813eca2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import assert from 'assert';
import _ from 'lodash';
export default class Exception extends Error {
/** 错误码 */
errcode: number;
/** 错误消息 */
errmsg: string;
/** 数据 */
data: any;
/** HTTP状态码 */
httpStatusCode: number;
/**
* 构造异常
*
* @param exception 异常
* @param _errmsg 异常消息
*/
constructor(exception: (string | number)[], _errmsg?: string) {
assert(_.isArray(exception), 'Exception must be Array');
const [errcode, errmsg] = exception as [number, string];
assert(_.isFinite(errcode), 'Exception errcode invalid');
assert(_.isString(errmsg), 'Exception errmsg invalid');
super(_errmsg || errmsg);
this.errcode = errcode;
this.errmsg = _errmsg || errmsg;
}
compare(exception: (string | number)[]) {
const [errcode] = exception as [number, string];
return this.errcode == errcode;
}
setHTTPStatusCode(value: number) {
this.httpStatusCode = value;
return this;
}
setData(value: any) {
this.data = _.defaultTo(value, null);
return this;
}
} |