diff --git a/README.md b/README.md index 180974e3..c159361c 100644 --- a/README.md +++ b/README.md @@ -114,9 +114,9 @@ server.removeRoute("/hello-log-headers"); Each route must have a handler function that generates a [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response). Upon receiving a request the `server.requestHandler` will construct a [RequestContext](https://deno.land/x/peko/server.ts?s=RequestContext) and pass it into global middleware, then route-specific middleware and finally the route handler. Global and route-specific middleware are invoked in the order they are added. If a response is returned by any middleware along the chain no subsequent middleware/handler will run. -We can design efficient logging/post-response operations by utilizing the middleware cascade. The server returns the response into previously called middleware that implement `await next()` before responding to the client request. See the below snippet or `middleware/logger.ts` for examples of utlizing the cascade. +We can design efficient logging/post-response operations by utilizing the middleware cascade. The server returns the response into previously called middleware that implement `await next()` before responding to the client request. See the below snippet or `middleware/logger.ts` for examples of utlizing the cascade. Returning a new `Response` after awaiting `next` will overwrite the response to be sent to the client. -If no matching route is found for a request an empty 404 response is sent. If an error occurs in handling a request the exception will not be caught and no response will be given to the client. Both of these behaviours can be overwritten with the following middleware: +If no matching route is found for a request an empty 404 response is sent. If an error occurs in handling a request an empty 500 response is sent. Both of these behaviours can be overwritten with the following middleware: ``` server.use(async (_, next) => { diff --git a/utils/Cascade.ts b/utils/Cascade.ts index 9c687ad1..8b63e214 100644 --- a/utils/Cascade.ts +++ b/utils/Cascade.ts @@ -12,7 +12,6 @@ export class Cascade { constructor(public ctx: RequestContext, private toCall: Array) {} - async run(fcn: Middleware): Promise { if (!fcn) return @@ -25,7 +24,7 @@ export class Cascade { try { const result = await fcnPromise(this.ctx, async () => await this.run(this.toCall[++this.called])) - if (!this.response && result) { + if (result) { this.response = result } else { await this.run(this.toCall[++this.called])