aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/GlobalErrorStore.ts
blob: 1da81eaa3dba81d0c774cc41d5daad88e7760f1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import type { Response } from 'electron';
import { action, makeObservable, observable } from 'mobx';
import type { Stores } from '../@types/stores.types';
import type { Actions } from '../actions/lib/actions';
import type { ApiInterface } from '../api';
import Request from './lib/Request';
import TypedStore from './lib/TypedStore';

interface Message {
  type: 'error' | 'log' | 'info';
  error?: {
    message?: string;
    status?: number;
  };
  request?: Request;
  response?: Response;
  server?: any;
  info?: any;
  url?: string;
  line?: number;
}

export default class GlobalErrorStore extends TypedStore {
  @observable error: any | null = null;

  @observable messages: Message[] = [];

  @observable response: Response = {} as Response;

  // TODO: Get rid of the @ts-expect-errors in this function.
  constructor(stores: Stores, api: ApiInterface, actions: Actions) {
    super(stores, api, actions);

    makeObservable(this);

    window.addEventListener('error', (...errorArgs: any[]): void => {
      // @ts-expect-error ts-message: Expected 5 arguments, but got 2.
      this._handleConsoleError.call(this, ['error', ...errorArgs]);
    });

    const origConsoleError = console.error;
    window.console.error = (...errorArgs: any[]) => {
      // @ts-expect-error ts-message: Expected 5 arguments, but got 2.
      this._handleConsoleError.call(this, ['error', ...errorArgs]);
      origConsoleError.apply(this, errorArgs);
    };

    // eslint-disable-next-line no-console
    const origConsoleLog = console.log;
    window.console.log = (...logArgs: any[]) => {
      // @ts-expect-error ts-message: Expected 5 arguments, but got 2.
      this._handleConsoleError.call(this, ['log', ...logArgs]);
      origConsoleLog.apply(this, logArgs);
    };

    // eslint-disable-next-line no-console
    const origConsoleInfo = console.info;
    window.console.info = (...infoArgs: any[]) => {
      // @ts-expect-error ts-message: Expected 5 arguments, but got 2.
      this._handleConsoleError.call(this, ['info', ...infoArgs]);
      origConsoleInfo.apply(this, infoArgs);
    };

    Request.registerHook(this._handleRequests);
  }

  async setup(): Promise<void> {
    // Not implemented
  }

  _handleConsoleError(type: any, error: any, url: string, line: number) {
    if (typeof type === 'object' && type.length > 0) {
      this.messages.push({
        type: type[0],
        info: type,
      });
    } else {
      this.messages.push({
        type,
        error,
        url,
        line,
      });
    }
  }

  @action _handleRequests = async (request: Request): Promise<void> => {
    if (request.isError) {
      this.error = request.error;

      if (request.error?.json) {
        try {
          this.response = await request.error.json();
        } catch {
          this.response = {} as Response;
        }
        if (this.error?.status === 401) {
          window['ferdium'].stores.app.authRequestFailed = true;
        }
      }

      this.messages.push({
        type: 'error',
        request: {
          result: request.result,
          wasExecuted: request.wasExecuted,
          method: request.method,
        } as Request,
        error: this.error,
        response: this.response,
        server: window['ferdium'].stores.settings.app.server,
      });
    } else {
      window['ferdium'].stores.app.authRequestFailed = false;
    }
  };
}