Skip to content

Commit 1191853

Browse files
authored
Merge pull request #62 from zeroflare/dev-frontend
Enhance error handling in registration components to gracefully manag…
2 parents 8f28524 + 5f10c72 commit 1191853

2 files changed

Lines changed: 55 additions & 9 deletions

File tree

frontend/register/src/App.tsx

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,26 @@ function App() {
6767
}
6868

6969
if (response.ok) {
70-
const data = await response.json()
70+
let data: { email?: string } = {}
71+
try {
72+
data = await response.json()
73+
} catch {
74+
setError('伺服器回應格式錯誤')
75+
return
76+
}
7177
if (data.email) {
7278
setEmail(data.email)
7379
setTokenExpired(false)
7480
} else {
7581
setError('無法取得註冊資訊')
7682
}
7783
} else {
78-
const errorData = await response.json()
84+
let errorData: { error?: string } = {}
85+
try {
86+
errorData = await response.json()
87+
} catch {
88+
// 回應不是 JSON 格式,忽略解析錯誤
89+
}
7990
// 檢查是否為 token 不存在或已過期的錯誤
8091
if (response.status === 404 && errorData.error === 'token 不存在或已過期') {
8192
setTokenExpired(true)
@@ -172,7 +183,13 @@ function App() {
172183
`${API_BASE_URL}/register/result?transactionId=${transactionId}`
173184
)
174185
if (response.ok) {
175-
const data: RegistrationResult = await response.json()
186+
let data: RegistrationResult
187+
try {
188+
data = await response.json()
189+
} catch {
190+
console.error('檢查註冊狀態失敗: 伺服器回應格式錯誤')
191+
return
192+
}
176193
// 如果是 "Waiting for registration" 訊息,繼續輪詢
177194
if (data.message === 'Waiting for registration') {
178195
console.log('等待註冊:', data.message)
@@ -187,10 +204,20 @@ function App() {
187204
}
188205
} else if (response.status === 400) {
189206
// 還在等待驗證,繼續輪詢
190-
const data = await response.json()
207+
let data: { message?: string } = {}
208+
try {
209+
data = await response.json()
210+
} catch {
211+
// 回應不是 JSON 格式,忽略
212+
}
191213
console.log('等待註冊:', data.message)
192214
} else {
193-
const errorData = await response.json()
215+
let errorData: { error?: string } = {}
216+
try {
217+
errorData = await response.json()
218+
} catch {
219+
// 回應不是 JSON 格式,忽略
220+
}
194221
console.error('檢查註冊狀態失敗:', errorData)
195222
}
196223
} catch (err) {

frontend/register/src/components/RegistrationForm.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ export function RegistrationForm({
140140
})
141141

142142
if (!response.ok) {
143-
const errorData = await response.json()
143+
let errorData: { error?: string } = {}
144+
try {
145+
errorData = await response.json()
146+
} catch {
147+
// 回應不是 JSON 格式,忽略解析錯誤
148+
}
144149
if (response.status === 429) {
145150
// 冷卻期錯誤
146151
setOtpCooldown(60)
@@ -151,7 +156,11 @@ export function RegistrationForm({
151156
throw new Error(errorData.error || '發送驗證碼失敗')
152157
}
153158

154-
await response.json()
159+
try {
160+
await response.json()
161+
} catch {
162+
// 成功回應但不是 JSON 格式,忽略
163+
}
155164
setOtpSent(true)
156165
setOtpCooldown(60) // 設置 60 秒冷卻期
157166
setOtpExpiry(600) // 設置 10 分鐘有效期
@@ -278,15 +287,25 @@ export function RegistrationForm({
278287
})
279288

280289
if (!response.ok) {
281-
const errorData = await response.json()
290+
let errorData: { error?: string } = {}
291+
try {
292+
errorData = await response.json()
293+
} catch {
294+
// 回應不是 JSON 格式,忽略解析錯誤
295+
}
282296
// 檢查是否為 token 不存在或已過期的錯誤
283297
if (response.status === 404 && errorData.error === 'token 不存在或已過期') {
284298
throw new Error('token 不存在或已過期')
285299
}
286300
throw new Error(errorData.error || '註冊失敗')
287301
}
288302

289-
const data: QRCodeResponse = await response.json()
303+
let data: QRCodeResponse
304+
try {
305+
data = await response.json()
306+
} catch {
307+
throw new Error('伺服器回應格式錯誤')
308+
}
290309
setError('')
291310
onSubmit(data)
292311
} catch (err) {

0 commit comments

Comments
 (0)