@p-j/eapi-middleware-errorhandler
Version:
An Error Handler middleware to work within an EAPI app. Check worker-eapi-template for context.
26 lines (25 loc) • 1.07 kB
TypeScript
/// <reference types="@p-j/eapi-types" />
/**
* The context of the current Error, extending the RequestContext
*/
export interface ErrorContext extends RequestContext {
error: Error;
}
/**
* External Error handler, used to log the error to an external system for instance.
*/
export interface ErrorForwarder {
(ctx: ErrorContext): void | Promise<void>;
}
export interface WithErrorHandlerOptions {
enableDebug?: boolean;
forwardError?: ErrorForwarder;
}
/**
* Middleware Factory returning a configured middleware for handling exceptions thrown by the requestHandler
* @param options
* @param options.enableDebug Optional: set to true to enable debug output using the ?debug=true querystring. Defaults to false.
* @param options.forwardError Optional: a function to do something else with the error; eg: log it ton an external system.
* It takes in an ErrorContext (a RequestContext extended with an error).
*/
export declare function withErrorHandler({ enableDebug, forwardError }?: WithErrorHandlerOptions): Middleware;