Skip to content

Commit be9f6c9

Browse files
committed
added try catch to callApi + alert for catch statement
1 parent 8c23d34 commit be9f6c9

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

adminforth/spa/src/utils.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ export async function callApi({path, method, body=undefined}: {
2323
body: JSON.stringify(body),
2424
};
2525
const fullPath = `${import.meta.env.VITE_ADMINFORTH_PUBLIC_PATH || ''}${path}`;
26-
const r = await fetch(fullPath, options);
27-
if (r.status == 401 ) {
28-
useUserStore().unauthorize();
29-
await router.push({ name: 'login' });
30-
return null;
31-
}
32-
return await r.json();
26+
try {
27+
const r = await fetch(fullPath, options);
28+
if (r.status == 401 ) {
29+
useUserStore().unauthorize();
30+
await router.push({ name: 'login' });
31+
return null;
32+
}
33+
return await r.json();
34+
} catch(e){
35+
window.adminforth.alert({variant:'danger', message:'Something went wrong, please try again later',})
36+
console.error('error',e);
37+
}
38+
3339
}
3440

3541
export async function callAdminForthApi({ path, method, body=undefined, headers=undefined }: {

0 commit comments

Comments
 (0)