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
118
119
120
121
122
123
124
|
import { observable, action, makeObservable } from 'mobx';
import { Actions } from '../actions/lib/actions';
import { ApiInterface } from '../api';
import { Stores } from '../@types/stores.types';
import Request from './lib/Request';
import TypedStore from './lib/TypedStore';
interface Message {
type: 'error' | 'log' | 'info';
error?: {
message?: string;
status?: number;
};
request?: {
result: any;
wasExecuted: any;
method: any;
};
response?: any;
server?: any;
info?: any;
url?: string;
line?: number;
}
export default class GlobalErrorStore extends TypedStore {
@observable error: any | null = null;
@observable messages: Message[] = [];
@observable response: object = {};
// TODO: Get rid of the @ts-ignores in this function.
constructor(stores: Stores, api: ApiInterface, actions: Actions) {
super(stores, api, actions);
makeObservable(this);
window.addEventListener('error', (...errorArgs: any[]): void => {
// @ts-ignore ts-message: Expected 5 arguments, but got 2.
this._handleConsoleError.call(this, ['error', ...errorArgs]);
});
const origConsoleError = console.error;
window.console.error = (...errorArgs: any[]) => {
// @ts-ignore ts-message: Expected 5 arguments, but got 2.
this._handleConsoleError.call(this, ['error', ...errorArgs]);
origConsoleError.apply(this, errorArgs);
};
const origConsoleLog = console.log;
window.console.log = (...logArgs: any[]) => {
// @ts-ignore ts-message: Expected 5 arguments, but got 2.
this._handleConsoleError.call(this, ['log', ...logArgs]);
origConsoleLog.apply(this, logArgs);
};
const origConsoleInfo = console.info;
window.console.info = (...infoArgs: any[]) => {
// @ts-ignore 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: {
isError: any;
error: { json: () => object | PromiseLike<object> };
result: any;
wasExecuted: any;
_method: any;
}): Promise<void> => {
if (request.isError) {
this.error = request.error;
if (request.error.json) {
try {
this.response = await request.error.json();
} catch {
this.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,
},
error: this.error,
response: this.response,
server: window['ferdium'].stores.settings.app.server,
});
} else {
window['ferdium'].stores.app.authRequestFailed = false;
}
};
}
|