🔊 back: Improve fastify error handler logging
This commit is contained in:
committed by
Anton Shepilov
parent
29c52e276b
commit
a38c9e6675
@@ -1,10 +1,22 @@
|
|||||||
import { FastifyInstance } from "fastify";
|
import { FastifyInstance } from "fastify";
|
||||||
import { logger } from "../../framework/logger";
|
import { logger } from "../../framework/logger";
|
||||||
|
import { CrudException } from "../../framework/api/crud-service";
|
||||||
|
|
||||||
function serverErrorHandler(server: FastifyInstance): void {
|
function serverErrorHandler(server: FastifyInstance): void {
|
||||||
server.setErrorHandler(async (err, request, reply) => {
|
server.setErrorHandler(async (err, request, reply) => {
|
||||||
logger.error(`Got ${reply.statusCode} error on request ${request.id} : `, err);
|
if (reply.statusCode == 200 || !reply.statusCode) {
|
||||||
server.log.debug(`Got ${reply.statusCode} error on request ${request.id} : ${err.toString()}`);
|
const status = err.statusCode || (err as any).status;
|
||||||
|
reply.status(status < 400 ? 500 : status);
|
||||||
|
}
|
||||||
|
logger.error(
|
||||||
|
err instanceof CrudException
|
||||||
|
? {
|
||||||
|
type: "CrudException",
|
||||||
|
message: err.message,
|
||||||
|
}
|
||||||
|
: { err },
|
||||||
|
`Got ${reply.statusCode} error on request ${request.id} : ${err.toString()}`,
|
||||||
|
);
|
||||||
await reply.send(
|
await reply.send(
|
||||||
reply.statusCode == 500
|
reply.statusCode == 500
|
||||||
? {
|
? {
|
||||||
|
|||||||
Reference in New Issue
Block a user