|
1 | 1 | import { ENV } from '@env'
|
2 |
| -import { isHttpError } from '@errors/HttpError.js' |
3 |
| -import { DiscordService } from '@lib/discord/DiscordService.js' |
4 |
| -import { FastifyPluginCallback } from 'fastify' |
| 2 | +import type { FastifyPluginCallback, FastifyRequest } from 'fastify' |
5 | 3 | import { container } from 'tsyringe'
|
6 | 4 | import fp from 'fastify-plugin'
|
| 5 | +import { isHttpError } from '@errors/HttpError.js' |
| 6 | +import { |
| 7 | + DiscordService, |
| 8 | + type MessagePayload, |
| 9 | + type MessageType, |
| 10 | +} from '@lib/discord/DiscordService.js' |
| 11 | +import { DynamicConfigService } from '@services/DynamicConfigService/index.js' |
| 12 | + |
| 13 | +const errorHandlerPlugin: FastifyPluginCallback = fp(async (fastify) => { |
| 14 | + const discord = container.resolve(DiscordService) |
| 15 | + const dynamicConfig = container.resolve(DynamicConfigService) |
7 | 16 |
|
8 |
| -// TODO: apply fastify-plugin |
9 |
| -const errorHandlerPlugin: FastifyPluginCallback = (fastify, _, done) => { |
10 |
| - fastify.addHook('preHandler', function (request, reply, done) { |
| 17 | + fastify.addHook('preHandler', (request, _, done) => { |
11 | 18 | if (request.body) {
|
12 | 19 | request.log.info({ body: request.body }, 'parsed body')
|
13 | 20 | }
|
14 | 21 | done()
|
15 | 22 | })
|
16 |
| - fastify.addHook('onError', (request, reply, error, done) => { |
17 |
| - request.log.error(error, 'fastify onError') |
18 |
| - const discord = container.resolve(DiscordService) |
19 |
| - discord |
20 |
| - .sendMessage('error', { |
21 |
| - type: 'fastify OnError', |
22 |
| - body: request?.body, |
23 |
| - query: request?.query, |
24 |
| - error, |
25 |
| - user: request?.user, |
26 |
| - ip: request?.ip, |
| 23 | + |
| 24 | + const sendErrorToDiscord = async ( |
| 25 | + type: MessageType, |
| 26 | + payload: MessagePayload, |
| 27 | + request: FastifyRequest, |
| 28 | + ) => { |
| 29 | + const isBlocked = await dynamicConfig.isBlockedUser(request.user?.username) |
| 30 | + if (isBlocked) { |
| 31 | + fastify.log.info('Blocked user, skipping error message') |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + try { |
| 36 | + await discord.sendMessage(type, { |
| 37 | + ...payload, |
| 38 | + body: payload.body ?? request.body ?? 'none', |
| 39 | + query: payload.query ?? request.query ?? 'none', |
| 40 | + user: request.user, |
| 41 | + ip: request.ip, |
27 | 42 | })
|
28 |
| - .catch(console.error) |
29 |
| - done() |
| 43 | + } catch (discordError) { |
| 44 | + fastify.log.error(discordError, 'Failed to send error message to Discord') |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + fastify.addHook('onError', async (request, _, error) => { |
| 49 | + request.log.error(error, 'fastify onError') |
| 50 | + await sendErrorToDiscord('error', { type: 'fastify OnError', error }, request) |
30 | 51 | })
|
31 |
| - fastify.setErrorHandler((error, request, reply) => { |
| 52 | + |
| 53 | + fastify.setErrorHandler(async (error, request, reply) => { |
| 54 | + const errorResponse = { |
| 55 | + message: error.message || 'Internal Server Error', |
| 56 | + name: error.name || 'Error', |
| 57 | + stack: ENV.appEnv === 'development' ? error.stack : undefined, |
| 58 | + } |
| 59 | + |
32 | 60 | if (isHttpError(error)) {
|
33 |
| - reply.status(error.statusCode).send({ |
34 |
| - message: error.message, |
35 |
| - name: error.name, |
36 |
| - stack: ENV.appEnv === 'development' ? error.stack : undefined, |
37 |
| - }) |
| 61 | + reply.status(error.statusCode).send(errorResponse) |
38 | 62 | } else {
|
39 |
| - reply.status(500).send({ |
40 |
| - message: error.message || 'Internal Server Error', |
41 |
| - name: error.name || 'Error', |
42 |
| - stack: ENV.appEnv === 'development' ? error.stack : undefined, |
43 |
| - }) |
| 63 | + reply.status(500).send(errorResponse) |
44 | 64 | }
|
45 | 65 |
|
46 | 66 | if (ENV.appEnv === 'development') {
|
47 | 67 | request.log.error(error, 'fastify handleError')
|
48 | 68 | } else {
|
49 |
| - const discord = container.resolve(DiscordService) |
50 |
| - discord |
51 |
| - .sendMessage('error', { |
52 |
| - type: 'fastify handleError', |
53 |
| - body: request?.body, |
54 |
| - query: request?.query, |
55 |
| - error, |
56 |
| - user: request?.user, |
57 |
| - ip: request?.ip, |
58 |
| - }) |
59 |
| - .catch(console.error) |
| 69 | + await sendErrorToDiscord('error', { type: 'fastify handleError', error }, request) |
60 | 70 | }
|
61 | 71 | })
|
| 72 | +}) |
62 | 73 |
|
63 |
| - done() |
64 |
| -} |
65 |
| - |
66 |
| -export default fp(errorHandlerPlugin) |
| 74 | +export default errorHandlerPlugin |
0 commit comments