/** * Invalid PSBT Error * * Error thrown when a PSBT is invalid or malformed * * @module features/gas-free/errors/InvalidPSBTError */ import { GasFreeError, GasFreeErrorCode } from './GasFreeError'; /** * InvalidPSBTError class * * Thrown when PSBT construction or validation fails. */ export class InvalidPSBTError extends GasFreeError { /** Validation errors */ public readonly validationErrors?: string[]; /** PSBT data (truncated for security) */ public readonly psbtPreview?: string; /** * Create an InvalidPSBTError * * @param message - Error message * @param validationErrors - Array of validation error messages * @param psbt - PSBT data (will be truncated) * @param originalError - Original error */ constructor( message: string, validationErrors?: string[], psbt?: string, originalError?: Error ) { super(message, GasFreeErrorCode.INVALID_PSBT, originalError, { validationErrors, psbtLength: psbt?.length, }); this.name = 'InvalidPSBTError'; this.validationErrors = validationErrors; // Only store first 50 chars of PSBT for debugging (security) this.psbtPreview = psbt ? psbt.substring(0, 50) + '...' : undefined; // Maintains proper stack trace for where our error was thrown (only available on V8) if (Error.captureStackTrace) { Error.captureStackTrace(this, InvalidPSBTError); } } /** * Create error for PSBT construction failure */ static constructionFailed( reason: string, originalError?: Error ): InvalidPSBTError { return new InvalidPSBTError( `PSBT construction failed: ${reason}`, [reason], undefined, originalError ); } /** * Create error for PSBT validation failure */ static validationFailed(errors: string[], psbt?: string): InvalidPSBTError { return new InvalidPSBTError( `PSBT validation failed:\n- ${errors.join('\n- ')}`, errors, psbt ); } /** * Create error for malformed PSBT */ static malformed(reason: string, psbt?: string): InvalidPSBTError { return new InvalidPSBTError(`Malformed PSBT: ${reason}`, [reason], psbt); } /** * Convert error to JSON */ toJSON(): Record { return { ...super.toJSON(), validationErrors: this.validationErrors, psbtPreview: this.psbtPreview, }; } }