|
| 1 | +export type BaseErrorType = BaseError & { name: 'StacksKitError' } |
| 2 | + |
| 3 | +export class BaseError extends Error { |
| 4 | + override name = 'StacksKitError' |
| 5 | + |
| 6 | + shortMessage: string |
| 7 | + |
| 8 | + constructor(shortMessage: string, options?: { cause?: Error; details?: string }) { |
| 9 | + const message = [ |
| 10 | + shortMessage, |
| 11 | + options?.details && `Details: ${options.details}`, |
| 12 | + ].filter(Boolean).join('\n\n') |
| 13 | + |
| 14 | + super(message, options?.cause ? { cause: options.cause } : undefined) |
| 15 | + this.shortMessage = shortMessage |
| 16 | + } |
| 17 | + |
| 18 | + walk(fn?: (err: unknown) => boolean): unknown { |
| 19 | + return walk(this, fn) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function walk(err: unknown, fn?: (err: unknown) => boolean): unknown { |
| 24 | + if (fn?.(err)) return err |
| 25 | + if (err && typeof err === 'object' && 'cause' in err) { |
| 26 | + return walk((err as { cause: unknown }).cause, fn) |
| 27 | + } |
| 28 | + return err |
| 29 | +} |
| 30 | + |
| 31 | +export type WalletNotConnectedErrorType = WalletNotConnectedError & { |
| 32 | + name: 'WalletNotConnectedError' |
| 33 | +} |
| 34 | + |
| 35 | +export class WalletNotConnectedError extends BaseError { |
| 36 | + override name = 'WalletNotConnectedError' |
| 37 | + |
| 38 | + constructor() { |
| 39 | + super('Wallet is not connected') |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +export type WalletNotFoundErrorType = WalletNotFoundError & { |
| 44 | + name: 'WalletNotFoundError' |
| 45 | +} |
| 46 | + |
| 47 | +export class WalletNotFoundError extends BaseError { |
| 48 | + override name = 'WalletNotFoundError' |
| 49 | + |
| 50 | + wallet: string |
| 51 | + |
| 52 | + constructor({ wallet }: { wallet: string }) { |
| 53 | + super(`${wallet} wallet not found`, { |
| 54 | + details: 'The wallet extension may not be installed.', |
| 55 | + }) |
| 56 | + this.wallet = wallet |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +export type UnsupportedMethodErrorType = UnsupportedMethodError & { |
| 61 | + name: 'UnsupportedMethodError' |
| 62 | +} |
| 63 | + |
| 64 | +export class UnsupportedMethodError extends BaseError { |
| 65 | + override name = 'UnsupportedMethodError' |
| 66 | + |
| 67 | + method: string |
| 68 | + wallet: string |
| 69 | + |
| 70 | + constructor({ method, wallet }: { method: string; wallet: string }) { |
| 71 | + super(`${method} is not supported by ${wallet} wallet`) |
| 72 | + this.method = method |
| 73 | + this.wallet = wallet |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +export type WalletRequestErrorType = WalletRequestError & { |
| 78 | + name: 'WalletRequestError' |
| 79 | +} |
| 80 | + |
| 81 | +export class WalletRequestError extends BaseError { |
| 82 | + override name = 'WalletRequestError' |
| 83 | + |
| 84 | + method: string |
| 85 | + wallet: string |
| 86 | + |
| 87 | + constructor({ method, wallet, cause }: { method: string; wallet: string; cause: Error }) { |
| 88 | + super(`${wallet} wallet request failed`, { |
| 89 | + cause, |
| 90 | + details: cause.message, |
| 91 | + }) |
| 92 | + this.method = method |
| 93 | + this.wallet = wallet |
| 94 | + } |
| 95 | +} |
0 commit comments