UNPKG

@fjell/core

Version:

Core Item and Key Framework for Fjell

80 lines (79 loc) 2.56 kB
import { ActionError } from "../errors/ActionError"; import { OperationContext } from "./OperationContext"; /** * Executes an async operation and enhances any ActionError thrown with operation context. * * This is the primary utility for wrapping operations across all fjell packages. * * @param operation - The async operation to execute * @param context - The operation context to add to any errors * @returns The result of the operation * @throws Enhanced ActionError or original error * * @example * ```typescript * // In fjell/lib * return executeWithContext( * () => toWrap.get(key), * { * itemType: 'user', * operationType: 'get', * operationName: 'get', * params: { key }, * key * } * ); * * // In express-router * return executeWithContext( * () => operations.action('approve', params, key), * { * itemType: 'invoice', * operationType: 'action', * operationName: 'approve', * params, * key * } * ); * ``` */ export declare function executeWithContext<T>(operation: () => Promise<T>, context: OperationContext): Promise<T>; /** * Synchronous version of executeWithContext for non-async operations. * * @param operation - The sync operation to execute * @param context - The operation context to add to any errors * @returns The result of the operation * @throws Enhanced ActionError or original error */ export declare function executeWithContextSync<T>(operation: () => T, context: OperationContext): T; /** * Enhances an error with operation context if it's an ActionError. * If it's not an ActionError, returns the original error unchanged. * * This allows database implementations and business logic to throw * ActionErrors with minimal context, and have that context filled in * by the operation wrapper. * * @param error - The error to enhance * @param context - The operation context to add * @returns The enhanced or original error */ export declare function enhanceError(error: Error, context: OperationContext): Error; /** * Type guard to check if an error is an ActionError. * * @param error - The error to check * @returns True if the error is an ActionError */ export declare function isActionError(error: unknown): error is ActionError; /** * Extracts error info from an ActionError, or creates a generic error info * for non-ActionErrors. * * Useful for logging and error reporting. * * @param error - The error to extract info from * @returns The error info */ export declare function getErrorInfo(error: unknown): any;