Skip to content

Commit 96316da

Browse files
committed
remove global and route specific middleware sections and re-order some text in error handling section
1 parent b3afbf8 commit 96316da

File tree

1 file changed

+38
-79
lines changed

1 file changed

+38
-79
lines changed

docs/features/event-handler/rest.md

Lines changed: 38 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -162,30 +162,6 @@ You can configure CORS at the router level via the `cors` middleware.
162162

163163
### Middleware
164164

165-
```mermaid
166-
sequenceDiagram
167-
participant Request
168-
participant Router
169-
participant M1 as Middleware 1
170-
participant M2 as Middleware 2
171-
participant Handler as Route Handler
172-
173-
Request->>Router: Incoming Request
174-
Router->>M1: Execute (params, reqCtx, next)
175-
Note over M1: Pre-processing
176-
M1->>M2: Call next()
177-
Note over M2: Pre-processing
178-
M2->>Handler: Call next()
179-
Note over Handler: Execute handler
180-
Handler-->>M2: Return
181-
Note over M2: Post-processing
182-
M2-->>M1: Return
183-
Note over M1: Post-processing
184-
M1-->>Router: Return
185-
Router-->>Request: Response
186-
187-
```
188-
189165
Middleware are functions that execute during the request-response cycle, sitting between the
190166
incoming request and your route handler. They provide a way to implement cross-cutting
191167
concerns like authentication, logging, validation, and response transformation without
@@ -197,30 +173,12 @@ Each middleware function receives the following arguments:
197173
* **reqCtx** Request context containing the event, Lambda context, request, and response objects
198174
* **next** A function to pass control to the next middleware in the chain
199175

200-
Middleware can be applied globally, only on specific routes, or a combination of both.
201-
202-
#### Global middleware
203-
204-
You can use `app.use` to register middleware that should always run regardless of the route.
205-
206-
=== "index.ts"
207-
208-
```ts hl_lines="8-17"
209-
--8<-- "examples/snippets/event-handler/rest/advanced_mw_global_middleware.ts:3"
210-
```
176+
Middleware can be applied globally on all routes, only on specific routes, or a combination of both.
211177

212-
#### Route specific middleware
213-
214-
You can apply middleware to specific routes by passing them as arguments before the route
215-
handler.
216-
217-
=== "index.ts"
218-
219-
```ts hl_lines="9-18 25"
220-
--8<-- "examples/snippets/event-handler/rest/advanced_mw_route_middleware.ts:3"
221-
```
222-
223-
#### Order of execution
178+
Middleware follow an onion pattern where global middleware executes first in pre-processing,
179+
then route-specific middleware. After the handler executes, the order reverses for
180+
post-processing. When middleware modify the same response properties, the middleware that
181+
executes last in post-processing wins.
224182

225183
```mermaid
226184
sequenceDiagram
@@ -246,10 +204,11 @@ sequenceDiagram
246204
247205
```
248206

249-
Middleware follow an onion pattern where global middleware executes first in pre-processing,
250-
then route-specific middleware. After the handler executes, the order reverses for
251-
post-processing. When middleware modify the same response properties, the middleware that
252-
executes last in post-processing wins.
207+
#### Registering middleware
208+
209+
You can use `app.use` to register middleware that should always run regardless of the route
210+
and you can apply middleware to specific routes by passing them as arguments before the route
211+
handler.
253212

254213
=== "index.ts"
255214

@@ -265,6 +224,11 @@ executes last in post-processing wins.
265224

266225
#### Returning early
267226

227+
There are cases where you may want to terminate the execution of the middleware chain early. To
228+
do so, middleware can short-circuit processing by returning a `Response` or JSON object
229+
instead of calling `next()`. Neither the handler nor any subsequent middleware will run
230+
but the post-processing of already executed middleware will.
231+
268232
```mermaid
269233
sequenceDiagram
270234
participant Request
@@ -289,11 +253,6 @@ sequenceDiagram
289253
290254
```
291255

292-
There are cases where you may want to terminate the execution of the middleware chain early. To
293-
do so, middleware can short-circuit processing by returning a `Response` or JSON object
294-
instead of calling `next()`. Neither the handler nor any subsequent middleware will run
295-
but the post-processing of already executed middleware will.
296-
297256
=== "index.ts"
298257

299258
```ts hl_lines="13-18"
@@ -306,7 +265,11 @@ but the post-processing of already executed middleware will.
306265
--8<-- "examples/snippets/event-handler/rest/samples/advanced_mw_early_return.json"
307266
```
308267

309-
#### Exception Handling
268+
#### Error Handling
269+
270+
By default, any unhandled error in the middleware chain will be propagated as a HTTP
271+
500 back to the client. As you would expect, unlike early return, this stops the middleware
272+
chain entirely and no post-processing steps for any previously executed middleware will occur.
310273

311274
```mermaid
312275
sequenceDiagram
@@ -321,21 +284,20 @@ sequenceDiagram
321284
Router->>M1: Execute (params, reqCtx, next)
322285
Note over M1: Pre-processing
323286
M1->>M2: Call next()
324-
Note over M2: Throws Exception
325-
M2-->>M1: Exception propagated
326-
M1-->>Router: Exception propagated
287+
Note over M2: Throws Error
288+
M2-->>M1: Error propagated
289+
M1-->>Router: Error propagated
327290
Router->>EH: Handle error
328291
EH-->>Router: HTTP 500 Response
329292
Router-->>Request: HTTP 500 Error
330293
Note over Handler: Never executed
331294
332295
```
333296

334-
<center>*Unhandled exceptions*</center>
297+
<center>*Unhandled errors*</center>
335298

336-
By default, any unhandled exception in the middleware chain will be propagated as a HTTP
337-
500 back to the client. As you would expect, unlike early return, this stops the middleware
338-
chain entirely and no post-processing steps for any previously executed middleware will occur.
299+
You can handle errors in middleware as you would anywhere else, simply surround your code in
300+
a `try`/`catch` block and processing will occur as usual.
339301

340302
```mermaid
341303
sequenceDiagram
@@ -349,8 +311,8 @@ sequenceDiagram
349311
Router->>M1: Execute (params, reqCtx, next)
350312
Note over M1: Pre-processing
351313
M1->>M2: Call next()
352-
Note over M2: Exception thrown & caught
353-
Note over M2: Handle exception gracefully
314+
Note over M2: Error thrown & caught
315+
Note over M2: Handle error gracefully
354316
M2->>Handler: Call next()
355317
Note over Handler: Execute handler
356318
Handler-->>M2: Return
@@ -362,10 +324,12 @@ sequenceDiagram
362324
363325
```
364326

365-
<center>*Handled exceptions*</center>
327+
<center>*Handled errors*</center>
366328

367-
You can handle exceptions in middleware as you would anywhere else, simply surround your code in
368-
a `try`/`catch` block and processing will occur as usual.
329+
Similarly, you can choose to stop processing entirely by throwing an error in your
330+
middleware. Event handler provides many [built-in HTTP errors](#throwing-http-errors) that
331+
you can use or you can throw a custom error of your own. As noted above, this means
332+
that no post-processing of your request will occur.
369333

370334
```mermaid
371335
sequenceDiagram
@@ -380,22 +344,17 @@ sequenceDiagram
380344
Router->>M1: Execute (params, reqCtx, next)
381345
Note over M1: Pre-processing
382346
M1->>M2: Call next()
383-
Note over M2: Intentionally throws exception
384-
M2-->>M1: Exception propagated
385-
M1-->>Router: Exception propagated
347+
Note over M2: Intentionally throws error
348+
M2-->>M1: Error propagated
349+
M1-->>Router: Error propagated
386350
Router->>EH: Handle error
387351
EH-->>Router: HTTP Error Response
388352
Router-->>Request: HTTP Error Response
389353
Note over Handler: Never executed
390354
391355
```
392356

393-
<center>*Intentional exceptions*</center>
394-
395-
Similarly, you can choose to stop processing entirely by throwing an exception in your
396-
middleware. Event handler provides many [built-in HTTP errors](#throwing-http-errors) that
397-
you can use or you can throw a custom error of your own. As noted above, this means
398-
that no post-processing of your request will occur.
357+
<center>*Intentional errors*</center>
399358

400359
#### Custom middleware
401360

@@ -455,7 +414,7 @@ Keep the following in mind when authoring middleware for Event Handler:
455414
* **Call the next middleware.** If you are not returning early by returning a `Response` object
456415
or JSON object, always ensure you call the `next` function.
457416
* **Keep a lean scope.** Focus on a single task per middleware to ease composability and maintenance.
458-
* **Catch your own exceptions.** Catch and handle known exceptions to your logic, unless you want to raise HTTP Errors, or propagate specific exceptions to the client.
417+
* **Catch your own errors.** Catch and handle known errors to your logic, unless you want to raise HTTP Errors, or propagate specific errors to the client.
459418
* **Avoid destructuring the response object.** As mentioned in the [destructuring pitfalls](#avoiding-destructuring-pitfalls) section, always access the response through `reqCtx.res` rather than destructuring to avoid stale references.
460419

461420
### Fine grained responses

0 commit comments

Comments
 (0)