@energica-city/shared-amplify-utils
Version:
Shared utilities for AWS Amplify projects
67 lines • 2.82 kB
TypeScript
/**
* Type definition for error context objects that can be passed to throwError.
* This allows for structured error information while maintaining type safety.
*/
export type ErrorContext = Record<string, unknown>;
/**
* Enhanced error throwing function that ensures consistent error handling and logging
* across the application. It automatically logs errors with structured data and creates
* properly formatted Error objects with additional context.
*
* @param messageOrError - The primary error message (string) or Error object to throw
* @param originalErrorOrContext - Optional: original error object or additional context
* @throws {Error} Always throws a properly formatted Error with enhanced context
*
* @example
* ```typescript
* // Simple message
* throwError('Database connection failed');
*
* // Message with context
* throwError('User not found', { userId: '123', operation: 'getUser' });
*
* // Message with original error
* throwError('Failed to process request', originalError);
*
* // Error object (will extract message)
* throwError(new Error('Something went wrong'));
*
* // Array of errors (will combine messages)
* throwError([error1, error2, 'Additional info']);
* ```
*/
export declare function throwError(messageOrError: string | Error | unknown[], originalErrorOrContext?: unknown): never;
/**
* Utility function to safely extract an error message from an unknown error type.
* It handles various types of error objects, strings, and other values to produce a consistent
* string representation of the error, which is useful for displaying error messages to users.
*
* @param error - The error from which to extract the message. It can be of any type.
* @returns A string representing the error message.
* @example
* try {
* // some operation
* } catch (error) {
* const message = extractErrorMessage(error);
* console.log('Error occurred:', message);
* }
*/
export declare function extractErrorMessage(error: unknown): string;
/**
* Utility function to create error context objects with proper filtering.
* This ensures consistent context structure while removing undefined values
* that could clutter logs or cause serialization issues.
*
* @param context - Raw context object that may contain undefined values
* @returns Filtered context object with undefined values removed
* @example
* const context = createErrorContext({
* userId: user?.id, // might be undefined
* operation: 'deleteUser', // always defined
* requestId: req.id // always defined
* });
* // Result: { operation: 'deleteUser', requestId: 'abc123' }
* // (userId omitted because it was undefined)
*/
export declare function createErrorContext(context: Record<string, unknown>): Record<string, unknown>;
//# sourceMappingURL=error.d.ts.map