refactor: add devMessage field (useful in front-end)

This commit is contained in:
Methapon2001 2024-04-02 15:26:37 +07:00
parent aefbc965ab
commit ba65c1fe83
3 changed files with 14 additions and 1 deletions

View file

@ -20,6 +20,14 @@ const APP_PORT = +(process.env.APP_PORT || 3000);
RegisterRoutes(app);
app.get("*", (_, res) =>
res.status(404).send({
status: 404,
message: "Route not found.",
devMessage: "unknown_url",
}),
);
app.use(error);
app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`));

View file

@ -6,13 +6,15 @@ class HttpError extends Error {
*/
status: HttpStatus;
message: string;
devMessage?: string;
constructor(status: HttpStatus, message: string) {
constructor(status: HttpStatus, message: string, devMessage?: string) {
super(message);
this.name = "HttpError";
this.status = status;
this.message = message;
this.devMessage = devMessage;
}
}

View file

@ -8,6 +8,7 @@ function error(error: Error, _req: Request, res: Response, _next: NextFunction)
return res.status(error.status).json({
status: error.status,
message: error.message,
devMessage: error.devMessage,
});
}
@ -16,6 +17,7 @@ function error(error: Error, _req: Request, res: Response, _next: NextFunction)
status: HttpStatus.UNPROCESSABLE_ENTITY,
message: "Validation error(s).",
detail: error.fields,
devMessage: "missing_or_invalid_parameter",
});
}
@ -24,6 +26,7 @@ function error(error: Error, _req: Request, res: Response, _next: NextFunction)
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
status: HttpStatus.INTERNAL_SERVER_ERROR,
message: error.message,
devMessage: "system_error",
});
}