Skip to content

Commit b9cb68a

Browse files
committed
add goodcitizen section
1 parent 40adb7d commit b9cb68a

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

docs/features/event-handler/rest.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,18 @@ pre-processing and in reverse order during post-processing.
446446
!!! note "Composition order"
447447
Unlike traditional function composition which typically works right-to-left, `composeMiddleware` follows the convention used by most web frameworks and executes middleware left-to-right (first to last in the array). This means `composeMiddleware([a, b, c])` executes middleware `a` first, then `b`, then `c`.
448448

449+
#### Being a good citizen
450+
451+
Middleware can add subtle improvements to request/response processing, but also add significant complexity if you're not careful.
452+
453+
Keep the following in mind when authoring middleware for Event Handler:
454+
455+
* **Call the next middleware.** If you are not returning early by returns a `Response` object
456+
or JSON object, always ensure you call the `next` function.
457+
* **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.
459+
* **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.
460+
449461
### Fine grained responses
450462

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

0 commit comments

Comments
 (0)