UNPKG

@p-j/eapi-middleware-errorhandler

Version:

An Error Handler middleware to work within an EAPI app. Check worker-eapi-template for context.

34 lines 1.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withErrorHandler = void 0; /** * 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). */ function withErrorHandler({ enableDebug = false, forwardError } = {}) { return function _withErrorHandler(requestHandler) { return async function errorHandler({ event, request, params }) { try { // await is not superfluous here as otherwise the catch is bypassed return await requestHandler({ event, request, params }); } catch (error) { // While developing adding a debug param will allow you to see the stack trace directly const debug = new URL(request.url).searchParams.get('debug') === 'true' && enableDebug; // if a forward function has been defined, we pass the error to it alongside the original event // in case you need to use event.waitUntil while posting the error to a remote monitoring service for instance if (forwardError && error instanceof Error) forwardError({ error, event, request, params }); // TODO: handle Accept header (eg: respond with JSON vs Text) return new Response(debug && error instanceof Error ? error.stack || error.toString() : null, { status: 500, }); } }; }; } exports.withErrorHandler = withErrorHandler; //# sourceMappingURL=withErrorHandler.js.map