UNPKG

@bsv/sdk

Version:

BSV Blockchain Software Development Kit

81 lines 3 kB
export class WalletError extends Error { code; isError = true; constructor(message, code = 1, stack) { super(message); this.code = code; this.name = this.constructor.name; if (stack !== undefined && stack !== null && stack !== '') { this.stack = stack; } else { Error.captureStackTrace(this, this.constructor); } } /** * Safely serializes a WalletError (including special cases), Error or unknown error to JSON. * * Safely means avoiding deep, large, circular issues. * * Example deserialization can be found in HTTPWalletJSON.ts of bsv ts-sdk. * * @param error * @returns stringified JSON representation of the error such that it can be deserialized to a WalletError. */ static unknownToJson(error) { let e; if (error.isError === true && String(error.name).startsWith('WERR_')) { e = { name: error.name, message: error.message, isError: true }; if (e.name === 'WERR_REVIEW_ACTIONS') { e.reviewActionResults = error.reviewActionResults; e.sendWithResults = error.sendWithResults; e.txid = error.txid; e.tx = error.tx; e.noSendChange = error.noSendChange; e.code = 5; } else if (e.name === 'WERR_INVALID_PARAMETER') { e.parameter = error.parameter; e.code = 6; } else if (e.name === 'WERR_INSUFFICIENT_FUNDS') { e.totalSatoshisNeeded = error.totalSatoshisNeeded; e.moreSatoshisNeeded = error.moreSatoshisNeeded; e.code = 7; } } else if (error instanceof Error) { e = { name: error.constructor.name, message: error.message, isError: true }; } else { e = { name: 'WERR_UNKNOWN', message: String(error), isError: true }; } const json = JSON.stringify(e); return json; } } // NOTE: Enum values must not exceed the UInt8 range (0–255) export var walletErrors; (function (walletErrors) { walletErrors[walletErrors["unknownError"] = 1] = "unknownError"; walletErrors[walletErrors["unsupportedAction"] = 2] = "unsupportedAction"; walletErrors[walletErrors["invalidHmac"] = 3] = "invalidHmac"; walletErrors[walletErrors["invalidSignature"] = 4] = "invalidSignature"; walletErrors[walletErrors["reviewActions"] = 5] = "reviewActions"; walletErrors[walletErrors["invalidParameter"] = 6] = "invalidParameter"; walletErrors[walletErrors["insufficientFunds"] = 7] = "insufficientFunds"; })(walletErrors || (walletErrors = {})); export default WalletError; //# sourceMappingURL=WalletError.js.map