forked from Dshield-xyz/Dshield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.ts
More file actions
44 lines (37 loc) · 1.49 KB
/
Copy patherrors.ts
File metadata and controls
44 lines (37 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* Converts raw SDK / network / contract errors into short, readable messages.
* Call this inside every catch block before toasting.
*/
export function friendlyError(err: unknown): string {
const raw = err instanceof Error ? err.message : String(err);
const lower = raw.toLowerCase();
if (
lower.includes("user declined") ||
lower.includes("user rejected") ||
lower.includes("rejected the request") ||
lower.includes("declined access")
)
return "Cancelled — you declined the signature in your wallet.";
if (
lower.includes("failed to fetch") ||
lower.includes("networkerror") ||
lower.includes("network error") ||
lower.includes("etimedout") ||
lower.includes("econnrefused")
)
return "Network error — check your connection and try again.";
if (lower.includes("timed out") || lower.includes("timeout"))
return "The request timed out. The network may be busy — try again in a moment.";
if (lower.includes("insufficient") && lower.includes("fund"))
return "Insufficient funds — your wallet doesn't have enough USDC.";
if (
lower.includes("invoke_host_function") ||
lower.includes("transactionfailed") ||
lower.includes("tx_failed") ||
lower.includes("error(contract")
)
return "The transaction was rejected on-chain. Check your note and try again.";
// Keep raw message only if it's short enough to be readable as-is
if (raw.length < 100) return raw;
return "Something went wrong — please try again.";
}