Skip to content

Commit e0ad0a4

Browse files
committed
docs: additional clarifications
1 parent 599ec43 commit e0ad0a4

File tree

3 files changed

+12
-19
lines changed

3 files changed

+12
-19
lines changed

docs/features/event-handler/rest.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ You can use the `errorHandler()` method as a higher-order function or class meth
148148

149149
This allows you to catch and return custom error responses, or perform any other error handling logic you need.
150150

151+
Error handlers receive the error object and the request context as arguments, and can return a [`Response` object](#returning-response-objects) or a JavaScript object that will be auto-serialized as per the [response auto-serialization](#response-auto-serialization) section.
152+
151153
!!! tip "You can also pass a list of error classes to the `errorHandler()` method."
152154

153155
=== "index.ts"
@@ -158,15 +160,17 @@ This allows you to catch and return custom error responses, or perform any other
158160

159161
### Throwing HTTP errors
160162

161-
You can throw HTTP errors in your route handlers to 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.
163+
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.
162164

163165
This ensures that your Lambda function doesn't fail but returns a well-defined HTTP error response to the client.
164166

165167
If you need to send custom headers or a different response structure/code, you can use the [Response](#returning-response-objects) object instead.
166168

169+
!!! tip "You can throw HTTP errors in your route handlers, middleware, or custom error handlers!"
170+
167171
=== "index.ts"
168172

169-
```ts hl_lines="3 10"
173+
```ts hl_lines="3 11"
170174
--8<-- "examples/snippets/event-handler/rest/gettingStarted_throwing_http_errors.ts:3"
171175
```
172176

@@ -202,7 +206,7 @@ To avoid repeating the prefix in each route definition, you can use the `prefix`
202206

203207
=== "index.ts"
204208

205-
```ts hl_lines="6 9"
209+
```ts hl_lines="4 7"
206210
--8<-- "examples/snippets/event-handler/rest/gettingStarted_route_prefix.ts:3"
207211
```
208212

@@ -318,6 +322,11 @@ that no post-processing of your request will occur.
318322
A common pattern to create reusable middleware is to implement a factory functions that
319323
accepts configuration options and returns a middleware function.
320324

325+
!!! note "Always `await next()` unless returning early"
326+
Middleware functions must always call `await next()` to pass control to the next middleware
327+
in the chain, unless you are intentionally returning early by returning a `Response` or
328+
JSON object.
329+
321330
=== "index.ts"
322331

323332
```ts hl_lines="20-21 36 41"

examples/snippets/event-handler/rest/gettingStarted_error_handling.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ app.errorHandler(GetTodoError, async (error, reqCtx) => {
1717
return {
1818
statusCode: HttpErrorCodes.BAD_REQUEST,
1919
message: `Bad request: ${error.message} - ${reqCtx.req.headers.get('x-correlation-id')}`,
20-
error: 'BadRequest',
2120
};
2221
});
2322

examples/snippets/event-handler/rest/gettingStarted_rute_prefix.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)