UNPKG

@noony-serverless/core

Version:

A Middy base framework compatible with Firebase and GCP Cloud Functions with TypeScript

105 lines 3.8 kB
import { BaseMiddleware, Context } from '../core'; /** * Middleware class for handling errors in the application. * Implements the `BaseMiddleware` interface and provides an asynchronous * `onError` method that delegates error handling to the `handleError` function. * * @template TBody - The type of the request body payload (preserves type chain) * @template TUser - The type of the authenticated user (preserves type chain) * * @remarks * This middleware should be registered to catch and process errors that occur * during request handling. * * @method onError * @param error - The error object that was thrown. * @param context - The context in which the error occurred. * @returns A promise that resolves when error handling is complete. * * @example * Basic handler with error handling: * ```typescript * import { Handler, ErrorHandlerMiddleware, HttpError } from '@noony-serverless/core'; * * const createUserHandler = new Handler() * .use(new ErrorHandlerMiddleware()) * .handle(async (request, context) => { * if (!request.body?.email) { * throw new HttpError(400, 'Email is required', 'MISSING_EMAIL'); * } * * return { * success: true, * data: { id: 'user-123', email: request.body.email } * }; * }); * ``` * * @example * Google Cloud Functions integration: * ```typescript * import { http } from '@google-cloud/functions-framework'; * import { Handler, ErrorHandlerMiddleware } from '@noony-serverless/core'; * * const orderHandler = new Handler() * .use(new ErrorHandlerMiddleware()) * .handle(async (request, context) => { * // Handler logic that might throw errors * return { success: true, data: processedOrder }; * }); * * export const processOrder = http('processOrder', (req, res) => { * return orderHandler.execute(req, res); * }); * ``` */ export declare class ErrorHandlerMiddleware<TBody = unknown, TUser = unknown> implements BaseMiddleware<TBody, TUser> { onError(error: Error, context: Context<TBody, TUser>): Promise<void>; } /** * Creates an error handling middleware for processing errors in the application. * * @template TBody - The type of the request body payload (preserves type chain) * @template TUser - The type of the authenticated user (preserves type chain) * @returns {BaseMiddleware} An object implementing the `onError` method to handle errors. * * @remarks * The middleware's `onError` method asynchronously delegates error handling to the `handleError` function, * passing the error and context objects. * * @example * Basic usage with factory function: * ```typescript * import { Handler, errorHandler, HttpError } from '@noony-serverless/core'; * * const loginHandler = new Handler() * .use(errorHandler()) * .handle(async (request, context) => { * const { username, password } = request.body || {}; * * if (!username || !password) { * throw new HttpError(400, 'Credentials required', 'MISSING_CREDENTIALS'); * } * * const user = await authenticateUser(username, password); * return { success: true, data: { token: generateToken(user) } }; * }); * ``` * * @example * Multiple middleware chain: * ```typescript * import { Handler, errorHandler, BodyParserMiddleware } from '@noony-serverless/core'; * * const secureHandler = new Handler() * .use(new BodyParserMiddleware()) * .use(new AuthenticationMiddleware()) * .use(errorHandler()) // Should be last to catch all errors * .handle(async (request, context) => { * // Handler logic * return { success: true, data: result }; * }); * ``` */ export declare const errorHandler: <TBody = unknown, TUser = unknown>() => BaseMiddleware<TBody, TUser>; //# sourceMappingURL=errorHandlerMiddleware.d.ts.map