Skip to content

Commit 4308a5f

Browse files
committed
Surface API error message to user. Generally speaking this is not the best practice in a real app, since API error message may not be localized or could be too technical for user experince. But here it's fine.
1 parent 88568ff commit 4308a5f

File tree

7 files changed

+18
-18
lines changed

7 files changed

+18
-18
lines changed

app/components/AppAction.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ const newTaskSubmit = async () => {
141141
await refreshNuxtData()
142142
} catch (error: any) {
143143
toast.add({
144-
title: error.data?.message || 'Failed to create task',
144+
title: getApiErrorMessage(error),
145145
color: 'error',
146146
progress: false,
147147
})
@@ -177,7 +177,7 @@ const newRewardSubmit = async () => {
177177
await refreshNuxtData()
178178
} catch (error: any) {
179179
toast.add({
180-
title: error.data?.message || 'Failed to create reward',
180+
title: getApiErrorMessage(error),
181181
color: 'error',
182182
progress: false,
183183
})

app/components/AppMenu.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ const changeCredentialsSubmit = async () => {
213213
}
214214
} catch (error: any) {
215215
toast.add({
216-
title: error.data?.message || 'Failed to change password',
216+
title: getApiErrorMessage(error),
217217
color: 'error',
218218
progress: false,
219219
})

app/components/ChildView.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const handleCompleteTask = async (task: Task) => {
8686
await refreshNuxtData()
8787
} catch (error: any) {
8888
toast.add({
89-
title: 'Failed to mark task as completed',
89+
title: getApiErrorMessage(error),
9090
color: 'error',
9191
progress: false,
9292
})
@@ -111,7 +111,7 @@ const handleRejectTask = async (task: Task) => {
111111
await refreshNuxtData()
112112
} catch (error: any) {
113113
toast.add({
114-
title: 'Failed to mark task as incomplete',
114+
title: getApiErrorMessage(error),
115115
color: 'error',
116116
progress: false,
117117
})
@@ -146,7 +146,7 @@ const handleCompleteReward = async (reward: Reward) => {
146146
await refreshNuxtData()
147147
} catch (error: any) {
148148
toast.add({
149-
title: 'Failed to request reward redemption',
149+
title: getApiErrorMessage(error),
150150
color: 'error',
151151
progress: false,
152152
})
@@ -171,7 +171,7 @@ const handleRejectReward = async (reward: Reward) => {
171171
await refreshNuxtData()
172172
} catch (error: any) {
173173
toast.add({
174-
title: 'Failed to cancel reward redemption',
174+
title: getApiErrorMessage(error),
175175
color: 'error',
176176
progress: false,
177177
})

app/components/LoginForm.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const onSubmit = async () => {
117117
await navigateTo('/dashboard')
118118
} catch (error: any) {
119119
toast.add({
120-
title: getAPIErrorMessage(error),
120+
title: getApiErrorMessage(error),
121121
progress: false,
122122
color: 'error',
123123
})

app/components/ParentView.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const debouncedUpdatePoints = debounce(async (child: User, delta: number) => {
115115
emit('updatePoints', { childId: child.id, points: child.points })
116116
} catch (err) {
117117
console.error('Error updating points:', err)
118-
toast.add({ title: 'Failed to update points', progress: false })
118+
toast.add({ title: getApiErrorMessage(err), progress: false })
119119
}
120120
}, DEBOUNCE_WAIT_MS)
121121

app/pages/child/[id].vue

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ const handleCompleteTask = async (task: Task) => {
187187
await refreshNuxtData()
188188
} catch (error: any) {
189189
toast.add({
190-
title: 'Failed to approve task completion',
190+
title: getApiErrorMessage(error),
191191
color: 'error',
192192
progress: false,
193193
})
@@ -212,7 +212,7 @@ const handleRejectTask = async (task: Task) => {
212212
await refreshNuxtData()
213213
} catch (error: any) {
214214
toast.add({
215-
title: 'Failed to reject task completion',
215+
title: getApiErrorMessage(error),
216216
color: 'error',
217217
progress: false,
218218
})
@@ -237,7 +237,7 @@ const handleDeleteTask = async (task: Task) => {
237237
await refreshNuxtData()
238238
} catch (error: any) {
239239
toast.add({
240-
title: 'Failed to delete task',
240+
title: getApiErrorMessage(error),
241241
color: 'error',
242242
progress: false,
243243
})
@@ -275,7 +275,7 @@ const editTaskSubmit = async () => {
275275
await refreshNuxtData()
276276
} catch (error: any) {
277277
toast.add({
278-
title: 'Failed to update task',
278+
title: getApiErrorMessage(error),
279279
color: 'error',
280280
progress: false,
281281
})
@@ -314,7 +314,7 @@ const handleCompleteReward = async (reward: Reward) => {
314314
await refreshNuxtData()
315315
} catch (error: any) {
316316
toast.add({
317-
title: 'Failed to approve reward redemption',
317+
title: getApiErrorMessage(error),
318318
color: 'error',
319319
progress: false,
320320
})
@@ -374,7 +374,7 @@ const editRewardSubmit = async () => {
374374
await refreshNuxtData()
375375
} catch (error: any) {
376376
toast.add({
377-
title: 'Failed to update reward',
377+
title: getApiErrorMessage(error),
378378
color: 'error',
379379
progress: false,
380380
})
@@ -399,7 +399,7 @@ const handleRejectReward = async (reward: Reward) => {
399399
await refreshNuxtData()
400400
} catch (error: any) {
401401
toast.add({
402-
title: 'Failed to reject reward redemption',
402+
title: getApiErrorMessage(error),
403403
color: 'error',
404404
progress: false,
405405
})
@@ -424,7 +424,7 @@ const handleDeleteReward = async (reward: Reward) => {
424424
await refreshNuxtData()
425425
} catch (error: any) {
426426
toast.add({
427-
title: 'Failed to delete reward',
427+
title: getApiErrorMessage(error),
428428
color: 'error',
429429
progress: false,
430430
})

app/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ export function capitalize(str: string): string {
2323
return trimmed.charAt(0).toUpperCase() + trimmed.slice(1)
2424
}
2525

26-
export function getAPIErrorMessage(error: any): string {
26+
export function getApiErrorMessage(error: any): string {
2727
return error?.data?.message || 'Something went wrong'
2828
}

0 commit comments

Comments
 (0)