UNPKG

applesauce-wallet-connect

Version:

NIP-47 Nostr Wallet Connect implementation for both clients and services.

82 lines (81 loc) 3.06 kB
/** Base class for all NIP-47 wallet connect errors */ export class WalletBaseError extends Error { constructor(message) { super(message); this.name = this.constructor.name; } } /** The client is sending commands too fast. It should retry in a few seconds. */ export class RateLimitedError extends WalletBaseError { code = "RATE_LIMITED"; } /** The command is not known or is intentionally not implemented. */ export class NotImplementedError extends WalletBaseError { code = "NOT_IMPLEMENTED"; } /** The wallet does not have enough funds to cover a fee reserve or the payment amount. */ export class InsufficientBalanceError extends WalletBaseError { code = "INSUFFICIENT_BALANCE"; } /** The wallet has exceeded its spending quota. */ export class QuotaExceededError extends WalletBaseError { code = "QUOTA_EXCEEDED"; } /** This public key is not allowed to do this operation. */ export class RestrictedError extends WalletBaseError { code = "RESTRICTED"; } /** This public key has no wallet connected. */ export class UnauthorizedError extends WalletBaseError { code = "UNAUTHORIZED"; } /** An internal error. */ export class InternalError extends WalletBaseError { code = "INTERNAL"; } /** The encryption type of the request is not supported by the wallet service. */ export class UnsupportedEncryptionError extends WalletBaseError { code = "UNSUPPORTED_ENCRYPTION"; } /** The payment failed. This may be due to a timeout, exhausting all routes, insufficient capacity or similar. */ export class PaymentFailedError extends WalletBaseError { code = "PAYMENT_FAILED"; } /** The invoice could not be found by the given parameters. */ export class NotFoundError extends WalletBaseError { code = "NOT_FOUND"; } /** Other error. */ export class OtherError extends WalletBaseError { code = "OTHER"; } /** Factory function to create NWC error instances from error code and message */ export function createWalletError(code, message) { switch (code) { case "RATE_LIMITED": return new RateLimitedError(message); case "NOT_IMPLEMENTED": return new NotImplementedError(message); case "INSUFFICIENT_BALANCE": return new InsufficientBalanceError(message); case "QUOTA_EXCEEDED": return new QuotaExceededError(message); case "RESTRICTED": return new RestrictedError(message); case "UNAUTHORIZED": return new UnauthorizedError(message); case "INTERNAL": return new InternalError(message); case "UNSUPPORTED_ENCRYPTION": return new UnsupportedEncryptionError(message); case "PAYMENT_FAILED": return new PaymentFailedError(message); case "NOT_FOUND": return new NotFoundError(message); case "OTHER": return new OtherError(message); default: // This should never happen with proper typing, but provides a fallback return new OtherError(message); } }