Skip to main content
Module

x/workers_router/index.ts>WorkerRouter#recover

A router for Worker Runtimes such and Cloudflare Workers or Service Workers.
Latest
method WorkerRouter.prototype.recover
import { WorkerRouter } from "https://deno.land/x/workers_router@v0.3.0-pre.6/index.ts";

Register a special route to recover from an error during execution of a regular route.

In addition to the usual context properties, the provided handler receives a response and error property. In case of a well-known error (typically caused by middleware), the response contains a Fetch API Response object with matching status and status text set. In case of an unknown error, the response is a generic "internal server error" and the error property contains the value caught by the catch block.

Recover routes don't execute the router-level middleware (which might have caused the error), but can have middleware specifically for this route. Note that if another error occurs during the execution of this middleware, there are no more safety nets and an internal server error response is returned.

If a global DEBUG variable is set (or process.env.NODE_ENV is set to development in case of webpack) the router will throw on an unhandled error. This is to make it easier to spot problems during development. Otherwise, the router will not throw but instead dispatch a error event on itself before returning an empty internal server error response.

Parameters

path: string

Parameters

path: string
middleware: Middleware<ErrorContext, X>
handler: Handler<X>