-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy patherror-handler.ts
80 lines (72 loc) · 2.38 KB
/
error-handler.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { FastifyInstance } from 'fastify'
import { FastifyError } from '@fastify/error'
import { DatabaseError } from 'pg'
import { ErrorCode, isRenderableError } from '@internal/errors'
/**
* The global error handler for all the uncaught exceptions within a request.
* We try our best to display meaningful information to our users
* and log any error that occurs
* @param app
*/
export const setErrorHandler = (app: FastifyInstance) => {
app.setErrorHandler<Error>(function (error, request, reply) {
// We assign the error received.
// it will be logged in the request log plugin
request.executionError = error
// database error
if (
error instanceof DatabaseError &&
[
'Authentication error', // supavisor specific
'Max client connections reached',
'remaining connection slots are reserved for non-replication superuser connections',
'no more connections allowed',
'sorry, too many clients already',
'server login has been failing, try again later',
].some((msg) => (error as DatabaseError).message.includes(msg))
) {
return reply.status(429).send({
statusCode: `429`,
error: 'too_many_connections',
code: ErrorCode.SlowDown,
message: 'Too many connections issued to the database',
})
}
if (isRenderableError(error)) {
const renderableError = error.render()
const statusCode = error.userStatusCode
? error.userStatusCode
: renderableError.statusCode === '500'
? 500
: 400
if (renderableError.code === ErrorCode.AbortedTerminate) {
reply.header('Connection', 'close')
reply.raw.once('finish', () => {
setTimeout(() => {
if (!request.raw.closed) {
request.raw.destroy()
}
}, 3000)
})
}
return reply.status(statusCode).send({
...renderableError,
error: error.error || renderableError.code,
})
}
// Fastify errors
if ('statusCode' in error) {
const err = error as FastifyError
return reply.status((error as any).statusCode || 500).send({
statusCode: `${err.statusCode}`,
error: err.name,
message: err.message,
})
}
return reply.status(500).send({
statusCode: '500',
error: 'Internal',
message: 'Internal Server Error',
})
})
}