-
Notifications
You must be signed in to change notification settings - Fork 18
/
errorHandler.ts
141 lines (126 loc) · 3.31 KB
/
errorHandler.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import {
type FreeformRecord,
isInternalError,
isObject,
isPublicNonRecoverableError,
isStandardizedError,
} from '@lokalise/node-core'
import type { FastifyReply, FastifyRequest } from 'fastify'
import pino from 'pino'
import {
hasZodFastifySchemaValidationErrors,
isResponseSerializationError,
} from 'fastify-type-provider-zod'
import type { AppInstance } from '../../app.js'
const knownAuthErrors = new Set([
'FST_JWT_NO_AUTHORIZATION_IN_HEADER',
'FST_JWT_AUTHORIZATION_TOKEN_EXPIRED',
'FST_JWT_AUTHORIZATION_TOKEN_INVALID',
])
type ResponseObject = {
statusCode: number
payload: {
message: string
errorCode: string
details?: FreeformRecord
}
}
function resolveLogObject(error: unknown): FreeformRecord {
if (isInternalError(error)) {
return {
msg: error.message,
code: error.errorCode,
details: error.details ? JSON.stringify(error.details) : undefined,
error: pino.stdSerializers.err({
name: error.name,
message: error.message,
stack: error.stack,
}),
}
}
return {
message: isObject(error) ? error.message : JSON.stringify(error),
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
error: error instanceof Error ? pino.stdSerializers.err(error) : error,
}
}
function resolveResponseObject(error: FreeformRecord): ResponseObject {
if (isPublicNonRecoverableError(error)) {
return {
statusCode: error.httpStatusCode ?? 500,
payload: {
message: error.message,
errorCode: error.errorCode,
details: error.details,
},
}
}
if (hasZodFastifySchemaValidationErrors(error)) {
return {
statusCode: 400,
payload: {
message: 'Invalid params',
errorCode: 'VALIDATION_ERROR',
details: {
error: error.validation,
},
},
}
}
if (isResponseSerializationError(error)) {
return {
statusCode: 500,
payload: {
message: 'Invalid response',
errorCode: 'RESPONSE_VALIDATION_ERROR',
details: {
error: error.cause.issues,
method: error.method,
url: error.url,
},
},
}
}
if (isStandardizedError(error)) {
if (knownAuthErrors.has(error.code)) {
const message =
error.code === 'FST_JWT_AUTHORIZATION_TOKEN_INVALID'
? 'Authorization token is invalid'
: error.message
return {
statusCode: 401,
payload: {
message,
errorCode: 'AUTH_FAILED',
},
}
}
}
return {
statusCode: 500,
payload: {
message: 'Internal server error',
errorCode: 'INTERNAL_SERVER_ERROR',
},
}
}
export const errorHandler = function (
this: AppInstance,
error: FreeformRecord,
request: FastifyRequest,
reply: FastifyReply,
): void {
const logObject = resolveLogObject(error)
// Potentially request can break before we resolved the context
if (request.reqContext) {
// this preserves correct request id field
request.reqContext.logger.error(logObject)
} else {
request.log.error(logObject)
}
if (isInternalError(error)) {
this.diContainer.cradle.errorReporter.report({ error })
}
const responseObject = resolveResponseObject(error)
void reply.status(responseObject.statusCode).send(responseObject.payload)
}