You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
310
273
311
274
```mermaid
312
275
sequenceDiagram
@@ -321,21 +284,20 @@ sequenceDiagram
321
284
Router->>M1: Execute (params, reqCtx, next)
322
285
Note over M1: Pre-processing
323
286
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
327
290
Router->>EH: Handle error
328
291
EH-->>Router: HTTP 500 Response
329
292
Router-->>Request: HTTP 500 Error
330
293
Note over Handler: Never executed
331
294
332
295
```
333
296
334
-
<center>*Unhandled exceptions*</center>
297
+
<center>*Unhandled errors*</center>
335
298
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.
339
301
340
302
```mermaid
341
303
sequenceDiagram
@@ -349,8 +311,8 @@ sequenceDiagram
349
311
Router->>M1: Execute (params, reqCtx, next)
350
312
Note over M1: Pre-processing
351
313
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
354
316
M2->>Handler: Call next()
355
317
Note over Handler: Execute handler
356
318
Handler-->>M2: Return
@@ -362,10 +324,12 @@ sequenceDiagram
362
324
363
325
```
364
326
365
-
<center>*Handled exceptions*</center>
327
+
<center>*Handled errors*</center>
366
328
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.
369
333
370
334
```mermaid
371
335
sequenceDiagram
@@ -380,22 +344,17 @@ sequenceDiagram
380
344
Router->>M1: Execute (params, reqCtx, next)
381
345
Note over M1: Pre-processing
382
346
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
386
350
Router->>EH: Handle error
387
351
EH-->>Router: HTTP Error Response
388
352
Router-->>Request: HTTP Error Response
389
353
Note over Handler: Never executed
390
354
391
355
```
392
356
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>
399
358
400
359
#### Custom middleware
401
360
@@ -455,7 +414,7 @@ Keep the following in mind when authoring middleware for Event Handler:
455
414
***Call the next middleware.** If you are not returning early by returning a `Response` object
456
415
or JSON object, always ensure you call the `next` function.
457
416
***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.
459
418
***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.
0 commit comments