1
0

errorLog.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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(_) {
  19. return this.errorLogInfoList || [];
  20. },
  21. getErrorLogListCount(_) {
  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 {
  53. config: { url = '', data: params = '', method = 'get', headers = {} } = {},
  54. data = {},
  55. } = error.response;
  56. errInfo.url = url;
  57. errInfo.name = 'Ajax Error!';
  58. errInfo.file = '-';
  59. errInfo.stack = JSON.stringify(data);
  60. errInfo.detail = JSON.stringify({ params, method, headers });
  61. }
  62. this.addErrorLogInfo(errInfo as ErrorLogInfo);
  63. },
  64. },
  65. });
  66. // Need to be used outside the setup
  67. export function useErrorLogStoreWithOut() {
  68. return useErrorLogStore(store);
  69. }