-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.middleware.ts
More file actions
38 lines (32 loc) · 1.17 KB
/
error.middleware.ts
File metadata and controls
38 lines (32 loc) · 1.17 KB
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
33
34
35
36
37
38
import type { FastifyInstance } from 'fastify';
import { AppError } from '@/common/errors/app-error';
import { env } from '@/config/env';
const allowedOrigins = env.CORS_ORIGIN.split(',').map((o) => o.trim());
const setCorsHeaders = (
request: { headers: { origin?: string } },
reply: { header: (name: string, value: string) => void }
) => {
const origin = request.headers.origin;
if (origin && allowedOrigins.includes(origin)) {
reply.header('Access-Control-Allow-Origin', origin);
reply.header('Access-Control-Allow-Credentials', 'true');
}
};
export const registerErrorHandler = (app: FastifyInstance) => {
app.setErrorHandler((error, request, reply) => {
setCorsHeaders(request, reply);
if (error instanceof AppError) {
request.log.warn({ err: error, code: error.code }, 'application error');
return reply.status(error.statusCode).send({
code: error.code,
message: error.message
});
}
const unknownError = error as Error;
request.log.error({ err: unknownError }, 'unhandled error');
return reply.status(500).send({
code: 'INTERNAL_SERVER_ERROR',
message: 'Unexpected server error'
});
});
};