Skip to content

Commit 8e819cc

Browse files
committed
fix(middlewares): allow other middlewares to modify errors response
1 parent f011953 commit 8e819cc

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

src/aProblem.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { APIGatewayProxyResultV2 } from 'aws-lambda'
1+
import type { APIGatewayProxyStructuredResultV2 } from 'aws-lambda'
22
import type { Static } from '@sinclair/typebox'
33
import { Context, type ProblemDetail } from '@hello.nrfcloud.com/proto/hello'
44

55
export const aProblem = (
66
problem: Omit<Static<typeof ProblemDetail>, '@context'>,
77
cacheForSeconds: number = 60,
8-
): APIGatewayProxyResultV2 => ({
8+
): APIGatewayProxyStructuredResultV2 => ({
99
statusCode: problem.status,
1010
headers: {
1111
'content-type': 'application/problem+json',

src/aResponse.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import type {
2-
APIGatewayProxyResultV2,
3-
APIGatewayProxyStructuredResultV2,
4-
} from 'aws-lambda'
1+
import type { APIGatewayProxyStructuredResultV2 } from 'aws-lambda'
52
import type { HttpStatusCode } from '@hello.nrfcloud.com/proto/hello'
63

74
export const aResponse = (
@@ -11,7 +8,7 @@ export const aResponse = (
118
} & Record<string, unknown>,
129
cacheForSeconds: number = 60,
1310
headers?: APIGatewayProxyStructuredResultV2['headers'],
14-
): APIGatewayProxyResultV2 => {
11+
): APIGatewayProxyStructuredResultV2 => {
1512
const body = result !== undefined ? JSON.stringify(result) : undefined
1613
return {
1714
statusCode: status,

src/corsResponse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type {
22
APIGatewayProxyEventHeaders,
3-
APIGatewayProxyResultV2,
3+
APIGatewayProxyStructuredResultV2,
44
} from 'aws-lambda'
55
import { corsHeaders } from './corsHeaders.js'
66

77
export const corsResponse = (event: {
88
headers: APIGatewayProxyEventHeaders
9-
}): APIGatewayProxyResultV2 => ({
9+
}): APIGatewayProxyStructuredResultV2 => ({
1010
statusCode: 200,
1111
headers: corsHeaders(event),
1212
})

src/problemResponse.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,23 @@ export const problemResponse = (): MiddlewareObj<
3333
LambdaContext
3434
> => ({
3535
onError: async (req) => {
36+
if (req.response !== undefined) return
3637
if (req.error instanceof ValidationFailedError) {
37-
return aProblem({
38+
req.response = aProblem({
3839
title: 'Validation failed',
3940
status: HttpStatusCode.BAD_REQUEST,
4041
detail: formatTypeBoxErrors(req.error.errors),
4142
})
43+
} else if (req.error instanceof ProblemDetailError) {
44+
req.response = aProblem(req.error.problem)
45+
} else {
46+
req.response = aProblem({
47+
title:
48+
req.error instanceof Error
49+
? req.error.message
50+
: 'Internal Server Error',
51+
status: HttpStatusCode.INTERNAL_SERVER_ERROR,
52+
})
4253
}
43-
if (req.error instanceof ProblemDetailError) {
44-
return aProblem(req.error.problem)
45-
}
46-
return aProblem({
47-
title:
48-
req.error instanceof Error
49-
? req.error.message
50-
: 'Internal Server Error',
51-
status: HttpStatusCode.INTERNAL_SERVER_ERROR,
52-
})
5354
},
5455
})

0 commit comments

Comments
 (0)