-
Notifications
You must be signed in to change notification settings - Fork 27
/
global.js
106 lines (95 loc) · 3.21 KB
/
global.js
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
import isNumber from 'lodash/isNumber';
import { addNotification } from '@commercetools-frontend/notifications';
import {
HIDE_ALL_PAGE_NOTIFICATIONS,
DOMAINS,
STATUS_CODES,
LOGOUT_REASONS,
} from '@commercetools-frontend/constants';
import browserHistory from '@commercetools-frontend/browser-history';
export function showApiErrorNotification({ errors, source }) {
return showNotification(
{
kind: 'api-error',
values: {
source,
// NOTE: Some sources or errors (e.g. GraphQL) return an array or object.
// The cast into an array happens here so that consumers can pass both types.
errors: Array.isArray(errors) ? errors : [errors],
},
domain: DOMAINS.PAGE,
},
{
dismissAfter: 0,
}
);
}
export function showUnexpectedErrorNotification({ error, source, errorId }) {
return showNotification(
{
kind: 'unexpected-error',
values: {
source,
errorId,
message: error.message,
body:
error instanceof Error
? `${error.toString()}\n${error.stack}`
: error,
},
domain: DOMAINS.PAGE,
},
{
dismissAfter: 0,
error,
}
);
}
export function showNotification(notification, meta = {}) {
if (process.env.NODE_ENV !== 'production')
if (notification.domain) {
if (!Object.values(DOMAINS).includes(notification.domain))
// eslint-disable-next-line no-console
console.warn(
`Unknown notification domain "${notification.domain}"`,
notification
);
}
// eslint-disable-next-line no-console
else console.warn('Notification is missing domain', notification);
let dismissAfter = meta.dismissAfter;
if (!isNumber(dismissAfter))
dismissAfter = notification.kind === 'success' ? 5000 : 0;
return addNotification(notification, { ...meta, dismissAfter });
}
export function hideAllPageNotifications() {
return { type: HIDE_ALL_PAGE_NOTIFICATIONS };
}
export const handleActionError = (error, source) => dispatch => {
// On production we send the errors to Sentry.
// eslint-disable-next-line no-console
if (window.app.env !== 'production') console.error(error, error.stack);
// logout when unauthorized
if (error.statusCode === STATUS_CODES.UNAUTHORIZED) {
browserHistory.push(`/logout?reason=${LOGOUT_REASONS.UNAUTHORIZED}`);
}
// We need to do the return, because if not we see an error notification
// The error is handled with the handleUnavailableResource HoC to show the PageNotFound component
// when the api returns 404
if (error.statusCode === STATUS_CODES.NOT_FOUND) return null;
// All native errors that might occur within a Promise handler,
// are caught here as well. In this case we dispatch an unexpected
// error notification.
if (!error.statusCode)
return dispatch(showUnexpectedErrorNotification({ source, error }));
const hasListOfErrors = error.body.errors && Array.isArray(error.body.errors);
return dispatch(
showApiErrorNotification({
source,
errors: hasListOfErrors
? error.body.errors
: // Pass a fallback error so that our error components can handle it
[{ message: error.body.message }],
})
);
};