Skip to content

Commit 5975cc8

Browse files
committed
Added documentation for built-in error handlers and fixed some docs for CORS
1 parent 1ea7144 commit 5975cc8

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

docs/features/event-handler/rest.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,6 @@ Please [check this issue](https://github.com/aws-powertools/powertools-lambda-ty
130130

131131
You can access request details such as headers, query parameters, and body using the `Request` object provided to your route handlers and middleware functions via `reqCtx.req`.
132132

133-
### Handling not found routes
134-
135-
By default, we return a `404 Not Found` response for any unmatched routes.
136-
137-
You can use the `notFound()` method as a higher-order function or class method decorator to override this behavior, and return a custom response.
138-
139-
=== "index.ts"
140-
141-
```ts hl_lines="11"
142-
--8<-- "examples/snippets/event-handler/rest/gettingStarted_handle_not_found.ts"
143-
```
144-
145133
### Error handling
146134

147135
You can use the `errorHandler()` method as a higher-order function or class method decorator to define a custom error handler for errors thrown in your route handlers or middleware.
@@ -158,6 +146,20 @@ Error handlers receive the error object and the request context as arguments, an
158146
--8<-- "examples/snippets/event-handler/rest/gettingStarted_error_handling.ts:4"
159147
```
160148

149+
### Built-in Error Handlers
150+
151+
We provide built-in error handlers for common routing errors so you don't have to specify the Error type explicitly.
152+
153+
You can use the `notFound()` and `methodNotAllowed()` methods as higher-order functions or class method decorators to customize error responses for unmatched routes and unsupported HTTP methods.
154+
155+
By default, we return a `404 Not Found` response for unmatched routes.
156+
157+
=== "index.ts"
158+
159+
```ts hl_lines="11 23"
160+
--8<-- "examples/snippets/event-handler/rest/gettingStarted_built_in_error_handler.ts"
161+
```
162+
161163
### Throwing HTTP errors
162164

163165
You can throw HTTP errors in your route handlers to stop execution and return specific HTTP status codes and messages. Event Handler provides a set of built-in HTTP error classes that you can use to throw common HTTP errors.
@@ -439,11 +441,10 @@ For convenience, these are the default CORS settings applied when you register t
439441
| Key | Default Value | Description |
440442
| --------------- | ---------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
441443
| `origin` | `*` | Specifies the allowed origin(s) that can access the resource. Use `*` to allow all origins. |
442-
| `methods` | `GET,HEAD,PUT,PATCH,POST,DELETE` | Specifies the allowed HTTP methods. |
444+
| `methods` | `['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT']` | Specifies the allowed HTTP methods. |
443445
| `allowHeaders` | `[Authorization, Content-Type, X-Amz-Date, X-Api-Key, X-Amz-Security-Token]` | Specifies the allowed headers that can be used in the actual request. |
444446
| `exposeHeaders` | `[]` | Any additional header beyond the [safe listed by CORS specification](https://developer.mozilla.org/en-US/docs/Glossary/CORS-safelisted_response_header){target="_blank"}. |
445447
| `credentials` | `false` | Only necessary when you need to expose cookies, authorization headers or TLS client certificates. |
446-
| `maxAge` | `0` | Indicates how long the results of a preflight request can be cached. Value is in seconds. |
447448

448449
#### Per-route overrides
449450

examples/snippets/event-handler/rest/gettingStarted_handle_not_found.ts renamed to examples/snippets/event-handler/rest/gettingStarted_built_in_error_handler.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,13 @@ app.notFound(async (error, reqCtx) => {
2020
};
2121
});
2222

23+
app.methodNotAllowed(async (error) => {
24+
logger.error('Method not allowed', { error });
25+
26+
return {
27+
body: 'This method is not allowed',
28+
};
29+
});
30+
2331
export const handler = async (event: unknown, context: Context) =>
2432
app.resolve(event, context);

0 commit comments

Comments
 (0)