aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/GlobalErrorStore.ts
diff options
context:
space:
mode:
authorLibravatar Ricardo Cino <ricardo@cino.io>2022-06-22 16:21:23 +0200
committerLibravatar Vijay Aravamudhan <vraravam@users.noreply.github.com>2022-06-23 17:50:07 +0530
commitcaf43fc8d64c27a1aad4a5d6507313c513c1a447 (patch)
treec0a7bcb950cdf555b3e8a33a57e2241e7051861d /src/stores/GlobalErrorStore.ts
parentWorkaroud for in-app Password Recovery (#342) (diff)
downloadferdium-app-caf43fc8d64c27a1aad4a5d6507313c513c1a447.tar.gz
ferdium-app-caf43fc8d64c27a1aad4a5d6507313c513c1a447.tar.zst
ferdium-app-caf43fc8d64c27a1aad4a5d6507313c513c1a447.zip
chore: featureStore and GlobalErrorStore JS => TS
Diffstat (limited to 'src/stores/GlobalErrorStore.ts')
-rw-r--r--src/stores/GlobalErrorStore.ts122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/stores/GlobalErrorStore.ts b/src/stores/GlobalErrorStore.ts
new file mode 100644
index 000000000..cb364574b
--- /dev/null
+++ b/src/stores/GlobalErrorStore.ts
@@ -0,0 +1,122 @@
1import { observable, action } from 'mobx';
2import { Actions } from 'src/actions/lib/actions';
3import { ApiInterface } from 'src/api';
4import { Stores } from 'src/stores.types';
5import Request from './lib/Request';
6import TypedStore from './lib/TypedStore';
7
8interface Message {
9 type: 'error' | 'log' | 'info';
10 error?: {
11 message?: string;
12 status?: number;
13 };
14 request?: {
15 result: any;
16 wasExecuted: any;
17 method: any;
18 };
19 response?: any;
20 server?: any;
21 info?: any;
22 url?: string;
23 line?: number;
24}
25
26export default class GlobalErrorStore extends TypedStore {
27 @observable error: any | null = null;
28
29 @observable messages: Message[] = [];
30
31 @observable response: object = {};
32
33 // TODO: Get rid of the @ts-ignores in this function.
34 constructor(stores: Stores, api: ApiInterface, actions: Actions) {
35 super(stores, api, actions);
36
37 window.addEventListener('error', (...errorArgs: any[]): void => {
38 // @ts-ignore ts-message: Expected 5 arguments, but got 2.
39 this._handleConsoleError.call(this, ['error', ...errorArgs]);
40 });
41
42 const origConsoleError = console.error;
43 window.console.error = (...errorArgs: any[]) => {
44 // @ts-ignore ts-message: Expected 5 arguments, but got 2.
45 this._handleConsoleError.call(this, ['error', ...errorArgs]);
46 origConsoleError.apply(this, errorArgs);
47 };
48
49 const origConsoleLog = console.log;
50 window.console.log = (...logArgs: any[]) => {
51 // @ts-ignore ts-message: Expected 5 arguments, but got 2.
52 this._handleConsoleError.call(this, ['log', ...logArgs]);
53 origConsoleLog.apply(this, logArgs);
54 };
55
56 const origConsoleInfo = console.info;
57 window.console.info = (...infoArgs: any[]) => {
58 // @ts-ignore ts-message: Expected 5 arguments, but got 2.
59 this._handleConsoleError.call(this, ['info', ...infoArgs]);
60 origConsoleInfo.apply(this, infoArgs);
61 };
62
63 Request.registerHook(this._handleRequests);
64 }
65
66 async setup(): Promise<void> {
67 // Not implemented
68 }
69
70 _handleConsoleError(type: any, error: any, url: string, line: number) {
71 if (typeof type === 'object' && type.length > 0) {
72 this.messages.push({
73 type: type[0],
74 info: type,
75 });
76 } else {
77 this.messages.push({
78 type,
79 error,
80 url,
81 line,
82 });
83 }
84 }
85
86 @action _handleRequests = async (request: {
87 isError: any;
88 error: { json: () => object | PromiseLike<object> };
89 result: any;
90 wasExecuted: any;
91 _method: any;
92 }): Promise<void> => {
93 if (request.isError) {
94 this.error = request.error;
95
96 if (request.error.json) {
97 try {
98 this.response = await request.error.json();
99 } catch {
100 this.response = {};
101 }
102 if (this.error?.status === 401) {
103 window['ferdium'].stores.app.authRequestFailed = true;
104 }
105 }
106
107 this.messages.push({
108 type: 'error',
109 request: {
110 result: request.result,
111 wasExecuted: request.wasExecuted,
112 method: request._method,
113 },
114 error: this.error,
115 response: this.response,
116 server: window['ferdium'].stores.settings.app.server,
117 });
118 } else {
119 window['ferdium'].stores.app.authRequestFailed = false;
120 }
121 };
122}