| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import type { ErrorLogInfo } from '/#/store';
- import { defineStore } from 'pinia';
- import { store } from '/@/store';
- import { formatToDateTime } from '/@/utils/dateUtil';
- import projectSetting from '/@/settings/projectSetting';
- import { ErrorTypeEnum } from '/@/enums/exceptionEnum';
- export interface ErrorLogState {
- errorLogInfoList: Nullable<ErrorLogInfo[]>;
- errorLogListCount: number;
- }
- export const useErrorLogStore = defineStore({
- id: 'app-error-log',
- state: (): ErrorLogState => ({
- errorLogInfoList: null,
- errorLogListCount: 0,
- }),
- getters: {
- getErrorLogInfoList(): ErrorLogInfo[] {
- return this.errorLogInfoList || [];
- },
- getErrorLogListCount(): number {
- return this.errorLogListCount;
- },
- },
- actions: {
- addErrorLogInfo(info: ErrorLogInfo) {
- const item = {
- ...info,
- time: formatToDateTime(new Date()),
- };
- // [perf-base-v1] 上限保留最近 100 条,防止长时间运行错误日志无限累积占用内存
- this.errorLogInfoList = [item, ...(this.errorLogInfoList || [])].slice(0, 100);
- this.errorLogListCount += 1;
- },
- setErrorLogListCount(count: number): void {
- this.errorLogListCount = count;
- },
- /**
- * Triggered after ajax request error
- * @param error
- * @returns
- */
- addAjaxErrorInfo(error) {
- const { useErrorHandle } = projectSetting;
- if (!useErrorHandle) {
- return;
- }
- const errInfo: Partial<ErrorLogInfo> = {
- message: error.message,
- type: ErrorTypeEnum.AJAX,
- };
- if (error.response) {
- const { config: { url = '', data: params = '', method = 'get', headers = {} } = {}, data = {} } = error.response;
- errInfo.url = url;
- errInfo.name = 'Ajax Error!';
- errInfo.file = '-';
- errInfo.stack = JSON.stringify(data);
- errInfo.detail = JSON.stringify({ params, method, headers });
- }
- this.addErrorLogInfo(errInfo as ErrorLogInfo);
- },
- },
- });
- // Need to be used outside the setup
- export function useErrorLogStoreWithOut() {
- return useErrorLogStore(store);
- }
|