fix: errorHandler handles internal errors and limit data
Only show data in development
This commit is contained in:
parent
69c4c1c8e1
commit
2503756c60
@ -22,11 +22,11 @@ export default class ApiError extends Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON(development: boolean = false) {
|
||||||
return {
|
return {
|
||||||
message: this.message,
|
message: this.message,
|
||||||
code: this.code,
|
code: this.code,
|
||||||
data: this.data
|
data: development ? this.data : undefined
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
import * as express from "express";
|
import * as express from "express";
|
||||||
|
|
||||||
import ApiError from "@common/ApiError";
|
import ApiError from "@common/ApiError";
|
||||||
|
import { ErrorCode } from "@common/ErrorCode";
|
||||||
|
|
||||||
|
const isDev = process.env.NODE_ENV === "development";
|
||||||
|
|
||||||
const errorHandler: express.ErrorRequestHandler = (
|
const errorHandler: express.ErrorRequestHandler = (
|
||||||
err: any,
|
err: any,
|
||||||
@ -9,9 +12,11 @@ const errorHandler: express.ErrorRequestHandler = (
|
|||||||
next: express.NextFunction
|
next: express.NextFunction
|
||||||
) => {
|
) => {
|
||||||
if (err instanceof ApiError) {
|
if (err instanceof ApiError) {
|
||||||
res.status(err.statusCode).json(err.toJSON());
|
// TODO: different content-type?
|
||||||
|
res.status(err.statusCode).json(err.toJSON(isDev));
|
||||||
} else {
|
} else {
|
||||||
next(err);
|
const internalError = new ApiError("An internal server error has occurred", ErrorCode.Internal);
|
||||||
|
errorHandler(internalError, req, res, next);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user