forked from riccardoperra/codeimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorHandler.ts
More file actions
32 lines (27 loc) · 849 Bytes
/
errorHandler.ts
File metadata and controls
32 lines (27 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import {HttpError} from '@fastify/sensible/lib/httpError.js';
import fp from 'fastify-plugin';
import {NotFoundEntityException} from '../common/exceptions/NotFoundEntityException.js';
export default fp(
async fastify => {
fastify.setErrorHandler((error, request, reply) => {
let httpError: HttpError | null = null;
if (error.statusCode) {
httpError = fastify.httpErrors.createError(
error.statusCode,
error.message,
);
} else {
if (error instanceof NotFoundEntityException) {
httpError = fastify.httpErrors.notFound(error.message);
}
}
if (httpError) {
httpError.stack = error.stack;
httpError.code = error.code;
return reply.send(httpError);
}
reply.send(error);
});
},
{name: 'appErrorsHandler'},
);