-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolveErrorStatus.ts
More file actions
30 lines (26 loc) · 874 Bytes
/
Copy pathresolveErrorStatus.ts
File metadata and controls
30 lines (26 loc) · 874 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
import type { FastifyError } from 'fastify'
import { DatabaseError } from 'pg'
const PG_CODE_TO_STATUS: Record<string, number> = {
'23505': 409, // unique_violation
'23503': 409, // foreign_key_violation
'23502': 400, // not_null_violation
'23514': 400, // check_violation
'22P02': 400, // invalid_text_representation (bad UUID, malformed input)
}
interface ResolvedStatus {
statusCode: number
trustMessage: boolean
}
const resolveErrorStatus = (error: FastifyError): ResolvedStatus => {
if (error.statusCode) {
return { statusCode: error.statusCode, trustMessage: true }
}
if (error instanceof DatabaseError && error.code) {
const mapped = PG_CODE_TO_STATUS[error.code]
if (mapped) {
return { statusCode: mapped, trustMessage: false }
}
}
return { statusCode: 500, trustMessage: false }
}
export default resolveErrorStatus