-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a generic error handling mechanism (#10865)
* Implement a generic error handling mechanism * Cleanup ToastContainer settings * Fix query client instance & unit tests * Add unit tests for react-query callbacks and error boundaries * Fix render error test to pass CodeQL * Replace query client instances with query client configurations * Add TODO comment * Fixes after cr * Revert useMemo to useState
- Loading branch information
Showing
12 changed files
with
204 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
frontend/packages/shared/src/components/ErrorBoundaryFallback.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.container { | ||
padding: var(--fds-spacing-10); | ||
width: auto; | ||
} | ||
|
||
.alert > [class*='Alert-module_content'] { | ||
display: flex; | ||
gap: 1rem; | ||
} |
29 changes: 29 additions & 0 deletions
29
frontend/packages/shared/src/components/ErrorBoundaryFallback.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react'; | ||
import { useErrorBoundary } from 'react-error-boundary'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { _useIsProdHack } from 'app-shared/utils/_useIsProdHack'; | ||
import { Trans } from 'react-i18next'; | ||
import { Alert, Button, ErrorMessage, Link, Paragraph } from '@digdir/design-system-react'; | ||
import { Center } from './Center'; | ||
import classes from './ErrorBoundaryFallback.module.css'; | ||
|
||
export type ErrorBoundaryFallbackProps = { | ||
error: Error; | ||
} | ||
|
||
export const ErrorBoundaryFallback = ({ error }: ErrorBoundaryFallbackProps) => { | ||
const { t } = useTranslation(); | ||
const { resetBoundary } = useErrorBoundary(); | ||
|
||
return ( | ||
<Center className={classes.container}> | ||
<Alert severity='danger' className={classes.alert}> | ||
<Paragraph><Trans i18nKey={'general.error_message'} components={{ a: <Link>Slack</Link> }}/></Paragraph> | ||
{!_useIsProdHack && <ErrorMessage>{error.message}</ErrorMessage>} | ||
<Center> | ||
<Button onClick={resetBoundary} size='small'>{t('general.try_again')}</Button> | ||
</Center> | ||
</Alert> | ||
</Center> | ||
); | ||
}; |
35 changes: 35 additions & 0 deletions
35
frontend/packages/shared/src/contexts/ServicesContext.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react'; | ||
import { render, renderHook, screen, waitFor } from '@testing-library/react'; | ||
import { ServicesContextProvider } from './ServicesContext'; | ||
import { queriesMock } from 'app-shared/mocks/queriesMock'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { textMock } from '../../../../testing/mocks/i18nMock'; | ||
|
||
const wrapper = ({ children }) => ( | ||
<ServicesContextProvider {...queriesMock}> | ||
{children} | ||
</ServicesContextProvider> | ||
); | ||
|
||
describe('ServicesContext', () => { | ||
it('displays a default error message if an API call fails', async () => { | ||
const mockConsoleError = jest.spyOn(console, 'error').mockImplementation(); | ||
const { result } = renderHook(() => useQuery(['fetchData'],() => Promise.reject(), { retry: false }), { wrapper }); | ||
|
||
await waitFor(() => result.current.isError); | ||
|
||
expect(await screen.findByText(textMock('general.error_message'))).toBeInTheDocument(); | ||
expect(mockConsoleError).toHaveBeenCalled(); | ||
}); | ||
|
||
it('displays a default error message if a component throws an error while rendering', () => { | ||
const mockConsoleError = jest.spyOn(console, 'error').mockImplementation(); | ||
|
||
const ErrorComponent = () => { throw new Error('Intentional render error'); }; | ||
render(<ErrorComponent />, { wrapper }); | ||
|
||
expect(screen.getByText(textMock('general.error_message'))).toBeInTheDocument(); | ||
expect(screen.getByText(textMock('general.try_again'))).toBeInTheDocument(); | ||
expect(mockConsoleError).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
:root { | ||
--toastify-color-info: var(--fds-semantic-surface-action-primary-default); | ||
--toastify-color-success: var(--fds-semantic-surface-success-default); | ||
--toastify-color-warning: var(--fds-semantic-surface-warning-default); | ||
--toastify-color-error: var(--fds-semantic-surface-danger-default); | ||
--toastify-toast-width: 400px; | ||
} | ||
|
||
.Toastify__toast { | ||
line-height: 1.5rem; | ||
padding: var(--fds-spacing-3) var(--fds-spacing-3) var(--fds-spacing-4) var(--fds-spacing-3); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters