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.
32 lines
664 B
32 lines
664 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() { |
|
return { |
|
message: this.message, |
|
code: this.code, |
|
data: this.data |
|
}; |
|
} |
|
}
|
|
|