| 1 | /** |
| 2 | * The API answered with an error. `code` is the machine-readable error ("invalid_amount", "transaction_not_found", …), |
| 3 | * `status` the HTTP status (undefined for connection errors), `message` the readable text some errors carry. |
| 4 | */ |
| 5 | export class HBankError extends Error { |
| 6 | readonly code: string; |
| 7 | readonly status: number | undefined; |
| 8 | readonly body: unknown; |
| 9 | |
| 10 | constructor(code: string, status: number | undefined, message?: string, body?: unknown) { |
| 11 | super(message ? `${code}: ${message}` : code); |
| 12 | this.name = "HBankError"; |
| 13 | this.code = code; |
| 14 | this.status = status; |
| 15 | this.body = body; |
| 16 | } |
| 17 | |
| 18 | /** The readable message the API sent with the error, if any. */ |
| 19 | get detail(): string | undefined { |
| 20 | const b = this.body as { message?: unknown } | undefined; |
| 21 | return typeof b?.message === "string" ? b.message : undefined; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /** 401 `missing_api_key` or 403 `invalid_api_key`: the key is missing, wrong or revoked, or the business is inactive. */ |
| 26 | export class AuthenticationError extends HBankError { |
| 27 | constructor(code: string, status: number, message?: string) { |
| 28 | super(code, status, message, message); |
| 29 | this.name = "AuthenticationError"; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * HBank couldn't be reached, or the request timed out (`code` is "connection_failed" or "timeout"). |
| 35 | * The request may or may not have reached HBank: before retrying a refund, check `refunded` with `getPayment`. |
| 36 | */ |
| 37 | export class HBankConnectionError extends HBankError { |
| 38 | constructor(code: "connection_failed" | "timeout", message: string, cause?: unknown) { |
| 39 | super(code, undefined, message); |
| 40 | this.name = "HBankConnectionError"; |
| 41 | if (cause !== undefined) (this as { cause?: unknown }).cause = cause; |
| 42 | } |
| 43 | } |