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.
 
 
 
 
 
 

33 lines
753 B

import { ErrorCode, toHttpStatus } from "@common/ErrorCode";
export default class ApiError extends Error {
name = "ApiError";
statusCode: number;
code: ErrorCode;
data: any;
constructor(
message: string,
code: ErrorCode = ErrorCode.BadRequest,
data: any = {}
) {
super(message);
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(development: boolean = false) {
return {
message: this.message,
statusCode: this.statusCode,
code: this.code,
data: development ? this.data : undefined
};
}
}