Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const renderApp = (
ui,
{
// react-intl
locale = 'en',
// Apollo
mocks = [],
addTypename = false,
// react-router
route = '/',
history = createEnhancedHistory(
createMemoryHistory({ initialEntries: [route] })
),
// flopflip
adapter = memoryAdapter,
flags = {},
// application-context
environment,
user,
project,
permissions, // <-- deprecated option, use `{ project: { allAppliedPermissions } }`
actionRights, // <-- deprecated option, use `{ project: { allAppliedActionRights } }`
dataFences, // <-- deprecated option, use `{ project: { allAppliedDataFences } }`
dataLocale = 'en',
ApolloProviderComponent = MockedApolloProvider,
// gtm-context
gtmTracking = defaultGtmTracking,
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({
return (
dispatch: Dispatch<
| ReturnType
| ReturnType
>
) => {
// On production we send the errors to Sentry.
// eslint-disable-next-line no-console
if (window.app.env !== 'production')
console.error(error, error instanceof Error && error.stack);
if (!isApiError(error)) return dispatch(showUnexpectedErrorNotification());
// 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;
return dispatch(
showApiErrorNotification({
errors: error.body.errors || [
// Pass a fallback error so that our error components can handle it
{ message: error.body.message },
],
})
);
};
({ graphQLErrors, networkError, operation, forward }) => {
if (networkError && networkError.statusCode === STATUS_CODES.UNAUTHORIZED) {
history.push(`/logout?reason=${LOGOUT_REASONS.UNAUTHORIZED}`);
}
// In case of graphql errors, we want to retry unauthenticated requests by
// forcing our API to fetch a new token, using the `X-Force-Token` header.
// https://www.apollographql.com/docs/link/links/error/#retrying-failed-requests
// We need to do this as the `token-retry-link` only works for network errors.
// https://www.apollographql.com/docs/link/links/retry/
if (graphQLErrors) {
for (const err of graphQLErrors) {
if (err.extensions && err.extensions.code === 'UNAUTHENTICATED') {
operation.setContext(({ headers }) => ({
headers: {
...headers,
'X-Force-Token': true,
},
}));
// retry the request, returning the new observable
const renderApp = (ui, options = {}) => {
const initialRoute = options.route || '/';
const testHistory = createEnhancedHistory(
createMemoryHistory({ initialEntries: [initialRoute] })
);
ApplicationShellProvider.history = testHistory;
const testApolloClient = createApolloClient();
ApplicationShellProvider.apolloClient = testApolloClient;
const defaultProps = createTestProps();
const props = {
...defaultProps,
environment: { ...defaultProps.environment, ...options.environment },
};
const rendered = render(
ui || <p>{'OK'}</p>} />
);
return { ...rendered, history: testHistory };
};