forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandled-error.js
More file actions
84 lines (74 loc) · 2 KB
/
Copy pathhandled-error.js
File metadata and controls
84 lines (74 loc) · 2 KB
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
import { has } from 'lodash';
import standardErrorMessage from './standardErrorMessage';
import reportedErrorMessage from './reportedErrorMessage';
import { reportClientSideError } from './report-error';
export const handledErrorSymbol = Symbol('handledError');
export function isHandledError(err) {
return has(err, handledErrorSymbol);
}
export function unwrapHandledError(err) {
return handledErrorSymbol in err ? err[handledErrorSymbol] : {};
}
export function wrapHandledError(err, { type, message, redirectTo }) {
err[handledErrorSymbol] = { type, message, redirectTo };
return err;
}
export function handle400Error(e, options = { redirectTo: '/' }) {
const {
response: { status }
} = e;
let { redirectTo } = options;
let flash = { ...standardErrorMessage, redirectTo };
switch (status) {
case 401:
case 403: {
return {
...flash,
type: 'warn',
message: 'You are not authorised to continue on this route'
};
}
case 404: {
return {
...flash,
type: 'info',
message:
"We couldn't find what you were looking for. " +
'Please check and try again'
};
}
default: {
return flash;
}
}
}
export function handle500Error(
e,
options = {
redirectTo: '/'
},
_reportClientSideError = reportClientSideError
) {
const { redirectTo } = options;
_reportClientSideError(e, 'We just handled a 5** error on the client');
return { ...reportedErrorMessage, redirectTo };
}
export function handleAPIError(
e,
options,
_reportClientSideError = reportClientSideError
) {
const { response: { status = 0 } = {} } = e;
if (status >= 400 && status < 500) {
return handle400Error(e, options);
}
if (status >= 500) {
return handle500Error(e, options, _reportClientSideError);
}
const { redirectTo } = options;
_reportClientSideError(
e,
'We just handled an api error on the client without an error status code'
);
return { ...reportedErrorMessage, redirectTo };
}