You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
707 B
27 lines
707 B
7 years ago
|
import { ErrorCode, toHttpStatus } from "@common/ErrorCode";
|
||
7 years ago
|
|
||
7 years ago
|
export default class ApiError extends Error {
|
||
7 years ago
|
name = "ApiError";
|
||
|
statusCode: number;
|
||
7 years ago
|
code: ErrorCode;
|
||
|
data: any;
|
||
7 years ago
|
|
||
7 years ago
|
constructor(message: string, code: ErrorCode = ErrorCode.BadRequest, data: any = {}) {
|
||
7 years ago
|
super(message);
|
||
7 years ago
|
this.statusCode = toHttpStatus(code);
|
||
|
this.code = code;
|
||
|
// tslint:disable-next-line:prefer-conditional-expression
|
||
|
if (data instanceof Error) {
|
||
|
this.data = data.toString();
|
||
|
} else {
|
||
|
this.data = data;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
toJSON() {
|
||
|
return {
|
||
|
message: this.message, code: this.code, data: this.data,
|
||
|
};
|
||
7 years ago
|
}
|
||
|
}
|