Files
lucia-frontend/src/module/apiError.js
2026-03-06 08:50:01 +08:00

32 lines
1023 B
JavaScript

import router from "@/router/index";
import loadingStore from '@/stores/loading.js';
import {useToast} from 'vue-toast-notification';
import 'vue-toast-notification/dist/theme-sugar.css';
import axios from "axios";
import { deleteCookie } from "@/utils/cookieUtil.js";
// Delay loading and toast
const delay = (s = 0) => new Promise((resolve, reject) => setTimeout(resolve, s));
/**
* API catch error function
* @param {object} Error 後端 ERROR
* @param {string} toastMessage Toast 的提示文字
* @returns {string} Error HTTP Status
*/
export default async function apiError(error, toastMessage) {
if(error.request?.status === 401) {
delete axios.defaults.headers.common["Authorization"];
deleteCookie("luciaToken");
return router.push('/login');
}
const loading = loadingStore();
const $toast = useToast();
await delay();
loading.isLoading = true;
await delay(1000);
loading.isLoading = false;
await delay(500);
$toast.default(toastMessage, {position: 'bottom'});
}