import { logger } from '@fiverr-private/obs'; /** * The possible errors that could be thrown. */ export const ERRORS = { BEACON_ALREADY_EXISTS: 'Beacon is already exists, please ensure to use a unique name per beacon', BEACON_DOES_NOT_EXISTS: 'Beacon does not exists, make sure to call `register` before trying to set beacon ready hook.', BEACON_EXECUTION_FAILED: 'Beacon execution failed', } as const; /** * The retry function to be triggered upon beacon errors. * @returns {Promise} */ export type ActionRetry = () => Promise; /** * The error handler to be triggered upon beacon errors. */ export type ActionErrorHandler = (error: ActionExecutionError, retry: ActionRetry) => void; /** * The custom error to be thrown when beacon execution fails. */ export class ActionExecutionError extends Error { override name: string; override stack: string; attempt: number; failReason: string; constructor(name: string, attempt: number, error: Error) { super(); this.message = ERRORS.BEACON_EXECUTION_FAILED; this.name = name; this.attempt = attempt; this.stack = error.stack || ''; this.failReason = error.message; } } /** * Default error handler to be triggered upon beacon errors. */ export const defaultErrorHandler: ActionErrorHandler = (beaconError) => logger.error(beaconError);