Skip to content

Commit aa9dbe9

Browse files
committed
fixed the type issues and coverage
1 parent bcdc8aa commit aa9dbe9

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

packages/event-handler/src/rest/Router.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,11 @@ class Router {
280280
...requestContext,
281281
scope: options?.scope,
282282
});
283+
const statusCode =
284+
result instanceof Response ? result.status : result.statusCode;
283285
return handlerResultToProxyResult(
284286
result,
285-
(result.status ??
286-
result.statusCode ??
287-
HttpErrorCodes.INTERNAL_SERVER_ERROR) as (typeof HttpErrorCodes)[keyof typeof HttpErrorCodes]
287+
statusCode as (typeof HttpErrorCodes)[keyof typeof HttpErrorCodes]
288288
);
289289
}
290290
}

packages/event-handler/src/rest/errors.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
12
import type { HandlerResponse, HttpStatusCode } from '../types/rest.js';
23
import { HttpErrorCodes } from './constants.js';
34

@@ -39,7 +40,9 @@ export abstract class ServiceError extends Error {
3940
statusCode: this.statusCode,
4041
error: this.errorType,
4142
message: this.message,
42-
...(this.details && { details: this.details }),
43+
...(this.details && {
44+
details: this.details as Record<string, JSONValue>,
45+
}),
4346
};
4447
}
4548
}

packages/event-handler/tests/unit/rest/Router/error-handling.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,33 @@ describe('Class: Router - Error Handling', () => {
431431
});
432432
});
433433

434+
it('handles returning an API Gateway Proxy result from the error handler', async () => {
435+
// Prepare
436+
const app = new Router();
437+
438+
app.errorHandler(BadRequestError, async () => ({
439+
statusCode: HttpErrorCodes.BAD_REQUEST,
440+
body: JSON.stringify({
441+
foo: 'bar',
442+
}),
443+
}));
444+
445+
app.get('/test', () => {
446+
throw new BadRequestError('test error');
447+
});
448+
449+
// Act
450+
const result = await app.resolve(createTestEvent('/test', 'GET'), context);
451+
452+
// Assess
453+
expect(result).toEqual({
454+
statusCode: HttpErrorCodes.BAD_REQUEST,
455+
body: JSON.stringify({
456+
foo: 'bar',
457+
}),
458+
});
459+
});
460+
434461
it('handles returning a JSONObject from the error handler', async () => {
435462
// Prepare
436463
const app = new Router();

0 commit comments

Comments
 (0)