@@ -137,7 +137,7 @@ You can access request details such as headers, query parameters, and body using
137
137
!!! note "Coming soon"
138
138
Please open an issue if you would like us to prioritize this feature.
139
139
140
- ### Raising HTTP errors
140
+ ### Throwing HTTP errors
141
141
142
142
!!! note "Coming soon"
143
143
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.
308
308
309
309
#### Exception Handling
310
310
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
+
311
400
### Fine grained responses
312
401
313
402
You can use the Web API's ` Response ` object to have full control over the response. For
0 commit comments