error.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import store from '/@/store';
  2. import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper';
  3. import { VuexModule, getModule, Module, Mutation, Action } from 'vuex-module-decorators';
  4. import { formatToDateTime } from '/@/utils/dateUtil';
  5. import { ErrorTypeEnum } from '/@/enums/exceptionEnum';
  6. import { useSetting } from '/@/hooks/core/useSetting';
  7. export interface ErrorInfo {
  8. type: ErrorTypeEnum;
  9. file: string;
  10. name?: string;
  11. message: string;
  12. stack?: string;
  13. detail: string;
  14. url: string;
  15. time?: string;
  16. }
  17. export interface ErrorState {
  18. errorInfoState: ErrorInfo[] | null;
  19. errorListCountState: number;
  20. }
  21. const NAME = 'error';
  22. hotModuleUnregisterModule(NAME);
  23. @Module({ dynamic: true, namespaced: true, store, name: NAME })
  24. class Error extends VuexModule implements ErrorState {
  25. errorInfoState: ErrorInfo[] = [];
  26. errorListCountState = 0;
  27. get getErrorInfoState() {
  28. return this.errorInfoState;
  29. }
  30. get getErrorListCountState() {
  31. return this.errorListCountState;
  32. }
  33. @Mutation
  34. commitErrorInfoState(info: ErrorInfo): void {
  35. const item = {
  36. ...info,
  37. time: formatToDateTime(new Date()),
  38. };
  39. this.errorInfoState = [item, ...this.errorInfoState];
  40. this.errorListCountState += 1;
  41. }
  42. @Mutation
  43. commitErrorListCountState(count: number): void {
  44. this.errorListCountState = count;
  45. }
  46. @Action
  47. setupErrorHandle(error: any) {
  48. const { projectSetting } = useSetting();
  49. const { useErrorHandle } = projectSetting;
  50. if (!useErrorHandle) return;
  51. const errInfo: Partial<ErrorInfo> = {
  52. message: error.message,
  53. type: ErrorTypeEnum.AJAX,
  54. };
  55. if (error.response) {
  56. const {
  57. config: { url = '', data: params = '', method = 'get', headers = {} } = {},
  58. data = {},
  59. } = error.response;
  60. errInfo.url = url;
  61. errInfo.name = 'Ajax Error!';
  62. errInfo.file = '-';
  63. errInfo.stack = JSON.stringify(data);
  64. errInfo.detail = JSON.stringify({ params, method, headers });
  65. }
  66. this.commitErrorInfoState(errInfo as ErrorInfo);
  67. }
  68. }
  69. export { Error };
  70. export const errorStore = getModule<Error>(Error);