|
| 1 | +/** |
| 2 | + * Maps raw contract/RPC error strings to user-friendly messages. |
| 3 | + * Patterns are tested in order — first match wins. The contract-side |
| 4 | + * patterns mirror the exact UserError strings in contracts/grudge.py. |
| 5 | + */ |
| 6 | + |
| 7 | +interface ErrorMapping { |
| 8 | + pattern: RegExp; |
| 9 | + message: string; |
| 10 | +} |
| 11 | + |
| 12 | +const CONTRACT_ERRORS: ErrorMapping[] = [ |
| 13 | + // ── challenge lifecycle ─────────────────────────────────────────────── |
| 14 | + { pattern: /challenge not found/i, message: "This grudge doesn't exist on-chain." }, |
| 15 | + { pattern: /challenge is closed/i, message: "This grudge is closed — no more moves on it." }, |
| 16 | + { pattern: /already settled/i, message: "This grudge has already been settled." }, |
| 17 | + { pattern: /deadline has not passed/i, message: "The deadline hasn't hit yet — settling opens then." }, |
| 18 | + { pattern: /deadline passed/i, message: "The deadline has passed — the only move left is to settle." }, |
| 19 | + |
| 20 | + // ── creation ────────────────────────────────────────────────────────── |
| 21 | + { pattern: /self-stake required/i, message: "Put GEN where your mouth is — a self-stake is required." }, |
| 22 | + { pattern: /duration must be/i, message: "Duration must be between 1 and 365 days." }, |
| 23 | + { pattern: /required_proofs must be/i, message: "Required proofs must be between 1 and the number of days." }, |
| 24 | + { pattern: /statement must be/i, message: "The promise must be 12–280 characters." }, |
| 25 | + { pattern: /evidence_policy must be/i, message: "The evidence policy must be 4–280 characters." }, |
| 26 | + { pattern: /category must be/i, message: "Category is too long (max 32 characters)." }, |
| 27 | + { pattern: /statement rejected/i, message: "The validators rejected this promise — make it concrete, time-boxed, and verifiable." }, |
| 28 | + |
| 29 | + // ── staking ─────────────────────────────────────────────────────────── |
| 30 | + { pattern: /stake amount required/i, message: "Add a GEN amount to stake." }, |
| 31 | + { pattern: /side must be/i, message: "Pick a side: believe or doubt." }, |
| 32 | + { pattern: /creator cannot doubt themselves/i, message: "You can't doubt your own promise — that's just quitting." }, |
| 33 | + { pattern: /staking window closed/i, message: "The staking window has closed for this grudge." }, |
| 34 | + |
| 35 | + // ── evidence & disputes ─────────────────────────────────────────────── |
| 36 | + { pattern: /only the challenger submits evidence/i, message: "Only the challenger can submit evidence." }, |
| 37 | + { pattern: /evidence must be/i, message: "Evidence must be 4–4000 characters." }, |
| 38 | + { pattern: /evidence already submitted/i, message: "Already submitted for this proof period — come back next period." }, |
| 39 | + { pattern: /creator cannot dispute/i, message: "You can't dispute your own evidence." }, |
| 40 | + { pattern: /no such evidence entry/i, message: "That evidence entry doesn't exist." }, |
| 41 | + { pattern: /counter_evidence must be/i, message: "Counter-evidence must be 4–4000 characters." }, |
| 42 | + { pattern: /only verified evidence can be disputed/i, message: "Only VERIFIED evidence can be disputed." }, |
| 43 | + { pattern: /already disputed/i, message: "This evidence has already been disputed." }, |
| 44 | + |
| 45 | + // ── claims ──────────────────────────────────────────────────────────── |
| 46 | + { pattern: /nothing to claim/i, message: "Nothing to claim for this wallet." }, |
| 47 | + |
| 48 | + // ── wallet / network ────────────────────────────────────────────────── |
| 49 | + { pattern: /no wallet (connected|available)/i, message: "Connect your wallet to continue." }, |
| 50 | + { pattern: /user rejected|rejected by user|user denied/i, message: "Transaction cancelled." }, |
| 51 | + { pattern: /insufficient funds/i, message: "Insufficient GEN balance — top up from the Bradbury faucet." }, |
| 52 | + { pattern: /rate limit|429|too many requests/i, message: "The RPC is rate-limiting — wait a moment and try again." }, |
| 53 | + { pattern: /switch your wallet/i, message: "Switch your wallet to GenLayer Testnet Bradbury to continue." }, |
| 54 | + { pattern: /network|fetch|Failed to fetch|ECONNREFUSED|timeout/i, message: "Network error — check your connection and try again." }, |
| 55 | +]; |
| 56 | + |
| 57 | +const FALLBACK = "Something went wrong. Please try again."; |
| 58 | + |
| 59 | +/** |
| 60 | + * Extracts the deepest human-readable string from any error shape thrown |
| 61 | + * by genlayer-js, viem, or the contract VM (UserError nests in cause/details). |
| 62 | + */ |
| 63 | +export function extractRawMessage(err: unknown): string { |
| 64 | + if (!err) return FALLBACK; |
| 65 | + |
| 66 | + const e = err as Record<string, unknown>; |
| 67 | + const cause = e.cause as Record<string, unknown> | undefined; |
| 68 | + const candidates = [e.details, e.shortMessage, cause?.message, cause?.details, e.message].filter( |
| 69 | + Boolean, |
| 70 | + ) as string[]; |
| 71 | + |
| 72 | + // The actual UserError string is usually inside a quoted block |
| 73 | + for (const raw of candidates) { |
| 74 | + const m = String(raw).match(/UserError[^:]*:\s*(.+?)(?:\n|$)/); |
| 75 | + if (m?.[1]) return m[1].trim(); |
| 76 | + } |
| 77 | + |
| 78 | + return candidates[0] ? String(candidates[0]) : FALLBACK; |
| 79 | +} |
| 80 | + |
| 81 | +/** Returns a user-friendly message for any contract/wallet error. */ |
| 82 | +export function friendlyError(err: unknown): string { |
| 83 | + const raw = extractRawMessage(err); |
| 84 | + |
| 85 | + for (const { pattern, message } of CONTRACT_ERRORS) { |
| 86 | + if (pattern.test(raw)) return message; |
| 87 | + } |
| 88 | + |
| 89 | + // Trim noisy viem prefixes before falling back |
| 90 | + const cleaned = raw |
| 91 | + .replace(/^(ContractFunctionExecutionError|ContractFunctionRevertedError|Error):\s*/i, "") |
| 92 | + .replace(/\n.*/s, "") |
| 93 | + .trim(); |
| 94 | + |
| 95 | + return cleaned || FALLBACK; |
| 96 | +} |
0 commit comments