Skip to content

Commit ade173a

Browse files
authored
Merge pull request #42 from devforth/add_alert_when_api_throw_error
added try catch to callApi + alert for catch statement
2 parents 38dfb8f + 028a0ef commit ade173a

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

adminforth/spa/src/i18n.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ export function initI18n(app: ReturnType<typeof createApp>) {
5151
})
5252

5353
app.use(i18n);
54-
54+
return i18n
5555
}

adminforth/spa/src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export const app: ReturnType<typeof createApp> = createApp(App)
1313
app.use(createPinia())
1414
app.use(router)
1515

16-
initI18n(app);
16+
// get access to i18n instance outside components
17+
window.i18n = initI18n(app);
1718

1819

1920
/* IMPORTANT:ADMINFORTH CUSTOM USES */

adminforth/spa/src/utils.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { onMounted, ref, resolveComponent } from 'vue';
22
import type { CoreConfig } from './spa_types/core';
33
import type { ValidationObject } from './types/AdminForthConfig';
4-
5-
64
import router from "./router";
75
import { useCoreStore } from './stores/core';
86
import { useUserStore } from './stores/user';
97
import { Dropdown } from 'flowbite';
108

9+
1110
const LS_LANG_KEY = `afLanguage`;
1211

1312
export async function callApi({path, method, body=undefined}: {
@@ -23,13 +22,18 @@ export async function callApi({path, method, body=undefined}: {
2322
body: JSON.stringify(body),
2423
};
2524
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();
25+
try {
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();
33+
} catch(e){
34+
window.adminforth.alert({variant:'danger', message:window.i18n?.global?.t('Something went wrong, please try again later'),})
35+
console.error(`error in callApi ${path}`,e);
36+
}
3337
}
3438

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

0 commit comments

Comments
 (0)