refactor: add devMessage field (useful in front-end)
This commit is contained in:
parent
aefbc965ab
commit
ba65c1fe83
3 changed files with 14 additions and 1 deletions
|
|
@ -20,6 +20,14 @@ const APP_PORT = +(process.env.APP_PORT || 3000);
|
||||||
|
|
||||||
RegisterRoutes(app);
|
RegisterRoutes(app);
|
||||||
|
|
||||||
|
app.get("*", (_, res) =>
|
||||||
|
res.status(404).send({
|
||||||
|
status: 404,
|
||||||
|
message: "Route not found.",
|
||||||
|
devMessage: "unknown_url",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
app.use(error);
|
app.use(error);
|
||||||
|
|
||||||
app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`));
|
app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`));
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,15 @@ class HttpError extends Error {
|
||||||
*/
|
*/
|
||||||
status: HttpStatus;
|
status: HttpStatus;
|
||||||
message: string;
|
message: string;
|
||||||
|
devMessage?: string;
|
||||||
|
|
||||||
constructor(status: HttpStatus, message: string) {
|
constructor(status: HttpStatus, message: string, devMessage?: string) {
|
||||||
super(message);
|
super(message);
|
||||||
|
|
||||||
this.name = "HttpError";
|
this.name = "HttpError";
|
||||||
this.status = status;
|
this.status = status;
|
||||||
this.message = message;
|
this.message = message;
|
||||||
|
this.devMessage = devMessage;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ function error(error: Error, _req: Request, res: Response, _next: NextFunction)
|
||||||
return res.status(error.status).json({
|
return res.status(error.status).json({
|
||||||
status: error.status,
|
status: error.status,
|
||||||
message: error.message,
|
message: error.message,
|
||||||
|
devMessage: error.devMessage,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -16,6 +17,7 @@ function error(error: Error, _req: Request, res: Response, _next: NextFunction)
|
||||||
status: HttpStatus.UNPROCESSABLE_ENTITY,
|
status: HttpStatus.UNPROCESSABLE_ENTITY,
|
||||||
message: "Validation error(s).",
|
message: "Validation error(s).",
|
||||||
detail: error.fields,
|
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({
|
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
|
||||||
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
message: error.message,
|
message: error.message,
|
||||||
|
devMessage: "system_error",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue