Skip to content

Commit 4e66712

Browse files
committed
add middleware exception handling section
1 parent 265ad04 commit 4e66712

File tree

1 file changed

+90
-1
lines changed

1 file changed

+90
-1
lines changed

docs/features/event-handler/rest.md

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ You can access request details such as headers, query parameters, and body using
137137
!!! note "Coming soon"
138138
Please open an issue if you would like us to prioritize this feature.
139139

140-
### Raising HTTP errors
140+
### Throwing HTTP errors
141141

142142
!!! note "Coming soon"
143143
Please open an issue if you would like us to prioritize this feature.
@@ -308,6 +308,95 @@ but the post-processing of already executed middleware will.
308308

309309
#### Exception Handling
310310

311+
```mermaid
312+
sequenceDiagram
313+
participant Request
314+
participant Router
315+
participant EH as Error Handler
316+
participant M1 as Middleware 1
317+
participant M2 as Middleware 2
318+
participant Handler as Route Handler
319+
320+
Request->>Router: Incoming Request
321+
Router->>M1: Execute (params, reqCtx, next)
322+
Note over M1: Pre-processing
323+
M1->>M2: Call next()
324+
Note over M2: Throws Exception
325+
M2-->>M1: Exception propagated
326+
M1-->>Router: Exception propagated
327+
Router->>EH: Handle error
328+
EH-->>Router: HTTP 500 Response
329+
Router-->>Request: HTTP 500 Error
330+
Note over Handler: Never executed
331+
332+
```
333+
334+
<center>*Unhandled exceptions*</center>
335+
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.
339+
340+
```mermaid
341+
sequenceDiagram
342+
participant Request
343+
participant Router
344+
participant M1 as Middleware 1
345+
participant M2 as Middleware 2
346+
participant Handler as Route Handler
347+
348+
Request->>Router: Incoming Request
349+
Router->>M1: Execute (params, reqCtx, next)
350+
Note over M1: Pre-processing
351+
M1->>M2: Call next()
352+
Note over M2: Exception thrown & caught
353+
Note over M2: Handle exception gracefully
354+
M2->>Handler: Call next()
355+
Note over Handler: Execute handler
356+
Handler-->>M2: Return
357+
Note over M2: Post-processing
358+
M2-->>M1: Return
359+
Note over M1: Post-processing
360+
M1-->>Router: Return
361+
Router-->>Request: Response
362+
363+
```
364+
365+
<center>*Handled exceptions*</center>
366+
367+
You can handle exceptions in middleware as you woul anywhere else, simply surround your code in
368+
a `try`/`catch` block and processing will occur as usual.
369+
370+
```mermaid
371+
sequenceDiagram
372+
participant Request
373+
participant Router
374+
participant EH as Error Handler
375+
participant M1 as Middleware 1
376+
participant M2 as Middleware 2
377+
participant Handler as Route Handler
378+
379+
Request->>Router: Incoming Request
380+
Router->>M1: Execute (params, reqCtx, next)
381+
Note over M1: Pre-processing
382+
M1->>M2: Call next()
383+
Note over M2: Intentionally throws exception
384+
M2-->>M1: Exception propagated
385+
M1-->>Router: Exception propagated
386+
Router->>EH: Handle error
387+
EH-->>Router: HTTP Error Response
388+
Router-->>Request: HTTP Error Response
389+
Note over Handler: Never executed
390+
391+
```
392+
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.
399+
311400
### Fine grained responses
312401

313402
You can use the Web API's `Response` object to have full control over the response. For

0 commit comments

Comments
 (0)