fix: Error handling
This commit is contained in:
parent
a85ddae7ad
commit
4963e31473
@ -25,6 +25,7 @@ export default class ApiError extends Error {
|
||||
toJSON(development: boolean = false) {
|
||||
return {
|
||||
message: this.message,
|
||||
statusCode: this.statusCode,
|
||||
code: this.code,
|
||||
data: development ? this.data : undefined
|
||||
};
|
||||
|
@ -14,13 +14,15 @@ const errorHandler: express.ErrorRequestHandler = (
|
||||
if (err instanceof ApiError) {
|
||||
// TODO: different content-type?
|
||||
res.status(err.statusCode).json(err.toJSON(isDev));
|
||||
} else {
|
||||
} else if (err) {
|
||||
const internalError = new ApiError(
|
||||
"An internal server error has occurred",
|
||||
ErrorCode.Internal
|
||||
ErrorCode.Internal,
|
||||
err.stack ? err.stack : err
|
||||
);
|
||||
errorHandler(internalError, req, res, next);
|
||||
}
|
||||
next();
|
||||
};
|
||||
|
||||
export default errorHandler;
|
||||
|
Loading…
x
Reference in New Issue
Block a user