@jsb188/app 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/utils/api.ts +77 -0
package/package.json
CHANGED
package/utils/api.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import i18n from '../i18n/';
|
|
1
2
|
import { getENVVariable } from '../main';
|
|
3
|
+
import type { ServerErrorObj } from '../types/app.d';
|
|
2
4
|
|
|
3
5
|
let AUTH_TOKEN: string | null = null;
|
|
4
6
|
|
|
@@ -20,3 +22,78 @@ export function setAuthToken(token: string | null) {
|
|
|
20
22
|
export function getAuthToken(): string | null {
|
|
21
23
|
return getENVVariable('IS_BROWSER') ? AUTH_TOKEN : null;
|
|
22
24
|
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Helper; check if i18n exists for this error
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
function i18nErrCodeExists(errorCode: string): boolean {
|
|
31
|
+
return !!(i18n.has(`error.${errorCode}`) ||
|
|
32
|
+
i18n.has(`error.${errorCode}_other`));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Helper; normalize all errors from server
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
export function normalizeServerError(error?: any): ServerErrorObj {
|
|
40
|
+
let statusCode = 500;
|
|
41
|
+
let errorCode;
|
|
42
|
+
let errorValue;
|
|
43
|
+
let iconName;
|
|
44
|
+
let message;
|
|
45
|
+
|
|
46
|
+
if (!error || error instanceof Error) {
|
|
47
|
+
if (error?.message === 'Failed to fetch') {
|
|
48
|
+
errorCode = 'network_error';
|
|
49
|
+
iconName = 'wifi-off';
|
|
50
|
+
message = i18n.t('error.network_error');
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!errorCode) {
|
|
55
|
+
if (error?.errors?.length) {
|
|
56
|
+
const loggedError = error.errors.find((gErr: ServerErrorObj) =>
|
|
57
|
+
gErr.errorCode &&
|
|
58
|
+
i18nErrCodeExists(gErr.errorCode)
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
if (loggedError) {
|
|
62
|
+
statusCode = loggedError.statusCode;
|
|
63
|
+
errorCode = loggedError.errorCode;
|
|
64
|
+
errorValue = loggedError.variableWord;
|
|
65
|
+
} else {
|
|
66
|
+
statusCode = error.errors[0].statusCode;
|
|
67
|
+
errorCode = error.errors[0].errorCode;
|
|
68
|
+
errorValue = error.errors[0].variableWord;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!message) {
|
|
74
|
+
if (i18nErrCodeExists(errorCode)) {
|
|
75
|
+
message = i18n.t(`error.${errorCode}`, { value: errorValue });
|
|
76
|
+
} else {
|
|
77
|
+
message = i18n.t('error.unknown_error');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (
|
|
82
|
+
getENVVariable('NODE_ENV') === 'development' &&
|
|
83
|
+
message === i18n.t('error.unknown_error')
|
|
84
|
+
) {
|
|
85
|
+
// Log for development so I can see what went wrong
|
|
86
|
+
console.warn(error);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const title = i18n.has(`error.${errorCode}_title`) ? i18n.t(`error.${errorCode}_title`) : i18n.t('error.error');
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
statusCode,
|
|
93
|
+
errorCode,
|
|
94
|
+
errorValue,
|
|
95
|
+
iconName,
|
|
96
|
+
title,
|
|
97
|
+
message,
|
|
98
|
+
};
|
|
99
|
+
}
|