forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.tsx
More file actions
198 lines (179 loc) · 5.66 KB
/
Copy patherror.tsx
File metadata and controls
198 lines (179 loc) · 5.66 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
'use client'
import React from 'react'
import Link from 'next/link'
import { useRouter } from 'next/navigation'
type Props = { error: Error; reset: () => void }
/**
* Safely extracts a string or number value from an unknown object by key.
* Returns undefined if the value doesn't exist or isn't a string/number.
*/
function getStringOrNumber(obj: unknown, key: string): string | number | undefined {
if (obj && typeof obj === 'object' && !Array.isArray(obj)) {
const val = (obj as Record<string, unknown>)[key]
if (typeof val === 'string' || typeof val === 'number') return val
}
return undefined
}
function parseError(e: unknown) {
const defaultResult = {
title: 'Something went wrong',
message: 'An unexpected error occurred. Please try again or contact support.',
}
if (!e) return defaultResult
const message = e instanceof Error ? e.message : String(e)
const lower = message.toLowerCase()
// RPC unreachable heuristics
if (
lower.includes('failed to fetch') ||
lower.includes('networkerror') ||
lower.includes('econnrefused') ||
lower.includes('econnreset') ||
lower.includes('timeout') ||
lower.includes('503') ||
lower.includes('502') ||
lower.includes('504') ||
(lower.includes('rpc') && lower.includes('unavailable')) ||
lower.includes('no available')
) {
return {
title: 'Stellar testnet RPC is temporarily unavailable',
message:
'The app could not reach the Stellar testnet RPC server. This is usually temporary — please try again in a few moments.',
}
}
// Wallet / user rejection heuristics
if (
lower.includes('user rejected') ||
lower.includes('user denied') ||
lower.includes('request rejected') ||
lower.includes('rejected by user') ||
(lower.includes('signature') && lower.includes('rejected')) ||
(lower.includes('wallet') && lower.includes('rejected'))
) {
return {
title: 'Transaction was rejected in your wallet',
message:
'It looks like you (or your wallet) rejected the transaction. If this was accidental, try sending again.',
}
}
// Try to extract structured contract error information
try {
// Some errors come as JSON in the message
const trimmed = message.trim()
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
const parsed: unknown = JSON.parse(trimmed)
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
const obj = parsed as Record<string, unknown>
const code =
getStringOrNumber(obj, 'code') ??
getStringOrNumber(obj, 'error_code') ??
getStringOrNumber(obj, 'err') ??
getStringOrNumber(obj.error, 'code')
const detail =
getStringOrNumber(obj, 'message') ??
getStringOrNumber(obj.error, 'message') ??
getStringOrNumber(obj, 'reason') ??
getStringOrNumber(obj, 'detail')
if (code || detail) {
return {
title: code ? `Contract error: ${String(code)}` : 'Contract error',
message: detail ? String(detail) : message,
}
}
}
}
} catch {
// ignore JSON parse failures
}
// Try to pull out an error code with a regex like "code: XYZ" or "error_code=XYZ"
const codeMatch = message.match(/(?:error[_ ]?code|code)[:=]\s*([A-Za-z0-9_-]+)/i)
if (codeMatch) {
return {
title: `Contract error: ${codeMatch[1]}`,
message,
}
}
// Fallback: show the original message
return {
title: 'Error',
message,
}
}
export default function AppError({ error, reset }: Props) {
const router = useRouter()
const { title, message } = parseError(error)
return (
<div
style={{
minHeight: '100vh',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
padding: '24px',
background: 'linear-gradient(180deg,#0f172a, #071030)',
}}
>
<div
style={{
maxWidth: 760,
width: '100%',
background: 'white',
borderRadius: 12,
padding: 24,
boxShadow: '0 8px 30px rgba(2,6,23,0.6)',
}}
>
<h1 style={{ margin: 0, fontSize: 22 }}>{title}</h1>
<p style={{ color: '#334155' }}>{message}</p>
<div style={{ display: 'flex', gap: 12, marginTop: 18 }}>
<button
onClick={() => reset()}
style={{
padding: '8px 14px',
background: '#0ea5a2',
color: 'white',
border: 'none',
borderRadius: 8,
cursor: 'pointer',
}}
>
Try again
</button>
<button
onClick={() => router.refresh()}
style={{
padding: '8px 14px',
background: '#e2e8f0',
color: '#0f172a',
border: 'none',
borderRadius: 8,
cursor: 'pointer',
}}
>
Refresh
</button>
<Link
href="/"
style={{
display: 'inline-flex',
alignItems: 'center',
textDecoration: 'none',
padding: '8px 14px',
borderRadius: 8,
background: '#f8fafc',
color: '#0f172a',
}}
>
Go home
</Link>
</div>
<details style={{ marginTop: 18 }}>
<summary style={{ cursor: 'pointer' }}>Show technical details</summary>
<pre style={{ whiteSpace: 'pre-wrap', wordBreak: 'break-word', marginTop: 8 }}>
{String(error?.stack ?? error)}
</pre>
</details>
</div>
</div>
)
}