import { MallocError } from "../interfaces/errors"; interface ToReadableOpts { actionNames?: string[]; } export const getReadableErrorMessage = ( error: MallocError | any, opts: ToReadableOpts ): string => { if ((error as MallocError).mallocErrorMarker) { return getReadableMallocErrorMessage(error as MallocError, opts); } else { return JSON.stringify(error); } }; export const getReadableMallocErrorMessage = ( error: MallocError, opts: ToReadableOpts ): string => { switch (error.type) { case "Malloc Action Builder": return `Build Error for ${getActionName(error.actionIndex, opts)}: ${ error.message }`; case "Malloc Compiler": return `Compilation to Solana Error: ${error.message}`; case "Malloc Runner": return `Runtime Error: ${error.message}`; case "Malloc Builder": return `Build Error: ${error.message}`; default: break; } return ``; }; export const getActionName = (actionIdx: number, opts: ToReadableOpts) => opts.actionNames ? opts.actionNames[actionIdx] : `Action ${actionIdx + 1}`;