@energica-city/shared-amplify-utils
Version:
Shared utilities for AWS Amplify projects
48 lines • 1.98 kB
TypeScript
import type { Middleware } from '../middlewareChain';
/**
* Configuration options for the GraphQL error handling middleware.
*/
export interface GraphQLErrorHandlerConfig {
/** Whether to include stack traces in logs (default: false in production). */
includeStackTrace?: boolean;
/** Additional context to include with all logged errors. */
defaultContext?: Record<string, unknown>;
}
/**
* Creates an error handling middleware for AppSync GraphQL Lambda resolvers.
*
* This middleware catches all errors from downstream middleware or the resolver,
* provides structured logging with GraphQL-specific context, and then re-throws
* the error to let AppSync handle the final response to the client. This ensures
* consistent error logging without interfering with AppSync's native error handling.
*
* It should be the first middleware in the chain to catch all subsequent errors.
*
* @param config - Configuration options for error handling behavior.
* @returns A middleware function that handles and logs errors.
*
* @example
* ```typescript
* const chain = MiddlewareChain.createLambdaChain();
*
* // Add error handling (should be the first middleware)
* chain.use('errorHandler', createGraphQLErrorHandler({
* includeStackTrace: process.env.NODE_ENV === 'development',
* defaultContext: { service: 'inventory-api', version: '1.2.0' }
* }));
*
* // Add other middleware
* chain.use('auth', authMiddleware);
*
* const handler = wrapLambdaHandler(chain, async (event, context) => {
* // No try/catch needed here; errors are handled automatically
* const item = await getItemById(event.arguments.id);
* return item;
* });
* ```
*/
export declare function createGraphQLErrorHandler<TInput extends {
event: Record<string, unknown>;
context: Record<string, unknown> | object;
}, TOutput = unknown>(config?: GraphQLErrorHandlerConfig): Middleware<TInput, TOutput>;
//# sourceMappingURL=GraphQLErrorHandler.d.ts.map