8000 fix: respond with a 504 if appropriate · ITninja04/postgres-meta@07be77a · GitHub
[go: up one dir, main page]

Skip to content

Commit 07be77a

Browse files
darorasoedirgo
authored andcommitted
fix: respond with a 504 if appropriate
When we terminate an upstream connection due to a timeout, we ought to respond with a 504 rather than a 400.
1 parent 4b9f2fb commit 07be77a

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/server/routes/query.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'
22
import { PostgresMeta } from '../../lib'
33
import * as Parser from '../../lib/Parser'
44
import { DEFAULT_POOL_CONFIG } from '../constants'
5-
import { extractRequestForLogging } from '../utils'
5+
import { extractRequestForLogging, translateErrorToResponseCode } from '../utils'
66

77
const errorOnEmptyQuery = (request: FastifyRequest) => {
88
if (!(request.body as any).query) {
@@ -25,7 +25,7 @@ export default async (fastify: FastifyInstance) => {
2525
await pgMeta.end()
2626
if (error) {
2727
request.log.error({ error, request: extractRequestForLogging(request) })
28-
reply.code(400)
28+
reply.code(translateErrorToResponseCode(error))
2929
return { error: error.message }
3030
}
3131

@@ -43,7 +43,7 @@ export default async (fastify: FastifyInstance) => {
4343

4444
if (error) {
4545
request.log.error({ error, request: extractRequestForLogging(request) })
46-
reply.code(400)
46+
reply.code(translateErrorToResponseCode(error))
4747
return { error: error.message }
4848
}
4949

@@ -61,7 +61,7 @@ export default async (fastify: FastifyInstance) => {
6161

6262
if (error) {
6363
request.log.error({ error, request: extractRequestForLogging(request) })
64-
reply.code(400)
64+
reply.code(translateErrorToResponseCode(error))
6565
return { error: error.message }
6666
}
6767

@@ -78,7 +78,7 @@ export default async (fastify: FastifyInstance) => {
7878

7979
if (error) {
8080
request.log.error({ error, request: extractRequestForLogging(request) })
81-
reply.code(400)
81+
reply.code(translateErrorToResponseCode(error))
8282
return { error: error.message }
8383
}
8484

src/server/routes/tables.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FastifyInstance } from 'fastify'
22
import { PostgresMeta } from '../../lib'
33
import { DEFAULT_POOL_CONFIG } from '../constants'
4-
import { extractRequestForLogging } from '../utils'
4+
import { extractRequestForLogging, translateErrorToResponseCode } from '../utils'
55

66
export default async (fastify: FastifyInstance) => {
77
fastify.get<{
@@ -22,7 +22,7 @@ export default async (fastify: FastifyInstance) => {
2222
await pgMeta.end()
2323
if (error) {
2424
request.log.error({ error, request: extractRequestForLogging(request) })
25-
reply.code(500)
25+
reply.code(translateErrorToResponseCode(error, 500))
2626
return { error: error.message }
2727
}
2828

src/server/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,13 @@ export const extractRequestForLogging = (request: FastifyRequest) => {
1616
pg,
1717
}
1818
}
19+
20+
export function translateErrorToResponseCode(
21+
error: { message: string },
22+
defaultResponseCode = 400
23+
): number {
24+
if (error.message === 'Connection terminated due to connection timeout') {
25+
return 504
26+
}
27+
return defaultResponseCode
28+
}

0 commit comments

Comments
 (0)
0