errorLog.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import type { ErrorLogInfo } from '/#/store';
  2. import { defineStore } from 'pinia';
  3. import { store } from '/@/store';
  4. import { formatToDateTime } from '/@/utils/dateUtil';
  5. import projectSetting from '/@/settings/projectSetting';
  6. import { ErrorTypeEnum } from '/@/enums/exceptionEnum';
  7. export interface ErrorLogState {
  8. errorLogInfoList: Nullable<ErrorLogInfo[]>;
  9. errorLogListCount: number;
  10. }
  11. export const useErrorLogStore = defineStore({
  12. id: 'app-error-log',
  13. state: (): ErrorLogState => ({
  14. errorLogInfoList: null,
  15. errorLogListCount: 0,
  16. }),
  17. getters: {
  18. getErrorLogInfoList(): ErrorLogInfo[] {
  19. return this.errorLogInfoList || [];
  20. },
  21. getErrorLogListCount(): number {
  22. return this.errorLogListCount;
  23. },
  24. },
  25. actions: {
  26. addErrorLogInfo(info: ErrorLogInfo) {
  27. const item = {
  28. ...info,
  29. time: formatToDateTime(new Date()),
  30. };
  31. this.errorLogInfoList = [item, ...(this.errorLogInfoList || [])];
  32. this.errorLogListCount += 1;
  33. },
  34. setErrorLogListCount(count: number): void {
  35. this.errorLogListCount = count;
  36. },
  37. /**
  38. * Triggered after ajax request error
  39. * @param error
  40. * @returns
  41. */
  42. addAjaxErrorInfo(error) {
  43. const { useErrorHandle } = projectSetting;
  44. if (!useErrorHandle) {
  45. return;
  46. }
  47. const errInfo: Partial<ErrorLogInfo> = {
  48. message: error.message,
  49. type: ErrorTypeEnum.AJAX,
  50. };
  51. if (error.response) {
  52. const { config: { url = '', data: params = '', method = 'get', headers = {} } = {}, data = {} } = error.response;
  53. errInfo.url = url;
  54. errInfo.name = 'Ajax Error!';
  55. errInfo.file = '-';
  56. errInfo.stack = JSON.stringify(data);
  57. errInfo.detail = JSON.stringify({ params, method, headers });
  58. }
  59. this.addErrorLogInfo(errInfo as ErrorLogInfo);
  60. },
  61. },
  62. });
  63. // Need to be used outside the setup
  64. export function useErrorLogStoreWithOut() {
  65. return useErrorLogStore(store);
  66. }