Skip to content

Commit

Permalink
feat: error boundary
Browse files Browse the repository at this point in the history
work in progress, requires graasp/graasp-ui#756
  • Loading branch information
swouf committed Mar 13, 2024
1 parent 3d720a6 commit 338270c
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 39 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"license": "AGPL-3.0-only",
"author": "Graasp",
"contributors": [
"Basile Spaenlehauer"
"Basile Spaenlehauer",
"Jérémy La Scala"
],
"homepage": ".",
"type": "module",
Expand All @@ -13,7 +14,7 @@
"@emotion/styled": "11.11.0",
"@graasp/apps-query-client": "3.4.8",
"@graasp/sdk": "4.1.0",
"@graasp/ui": "4.9.0",
"@graasp/ui": "graasp/graasp-ui#755-error-fallback",
"@mui/icons-material": "5.15.12",
"@mui/lab": "5.0.0-alpha.167",
"@mui/material": "5.15.12",
Expand Down
17 changes: 16 additions & 1 deletion src/langs/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
{
"translations": {
"Welcome to the Graasp App Starter Kit": "Welcome to the Graasp App Starter Kit"
"Welcome to the Graasp App Starter Kit": "Welcome to the Graasp App Starter Kit",
"ERROR_BOUNDARY": {
"FALLBACK": {
"MESSAGE_TITLE": "Sorry, something went wrong with this application",
"MESSAGE_FEEDBACK": "Our team has been notified. If you would like to help, please, tell us what happened below.",
"ERROR_DETAILS": "Details of the error",
"NAME_LABEL": "Name",
"NAME_HELPER": "Provide your name (optional)",
"EMAIL_LABEL": "Email",
"EMAIL_HELPER": "Provide your email (optional)",
"COMMENT_LABEL": "Comment",
"COMMENT_HELPER": "Tell us what happened (optional)",
"THANKS_FOR_FEEDBACK": "Thank you for your feedback!",
"SEND": "Send your feedback"
}
}
}
}
40 changes: 40 additions & 0 deletions src/modules/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { FC, ReactNode } from 'react';
import { useTranslation } from 'react-i18next';

import { ErrorFallback } from '@graasp/ui/apps';

import * as Sentry from '@sentry/react';

const ErrorBoundary: FC<{ children?: ReactNode }> = ({ children }) => {
const { t: tFallback } = useTranslation('translations', {
keyPrefix: 'ERROR_BOUNDARY.FALLBACK',
});
return (
<Sentry.ErrorBoundary
// eslint-disable-next-line react/no-unstable-nested-components
fallback={({ error, componentStack, eventId }) => (
<ErrorFallback
error={error}
componentStack={componentStack}
eventId={eventId}
captureUserFeedback={Sentry.captureUserFeedback}
title={tFallback('MESSAGE_TITLE')}
formTitle={tFallback('MESSAGE_FEEDBACK')}
nameLabel={tFallback('NAME_LABEL')}
nameHelper={tFallback('NAME_HELPER')}
emailLabel={tFallback('EMAIL_LABEL')}
emailHelper={tFallback('EMAIL_HELPER')}
commentLabel={tFallback('COMMENT_LABEL')}
commentHelper={tFallback('COMMENT_HELPER')}
thanksMessage={tFallback('THANKS_FOR_FEEDBACK')}
sendButtonLabel={tFallback('SEND')}
errorDetailsLabel={tFallback('ERROR_DETAILS')}
/>
)}
>
{children}
</Sentry.ErrorBoundary>
);
};

export default ErrorBoundary;
65 changes: 35 additions & 30 deletions src/modules/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { defaultMockContext, mockMembers } from '@/mocks/db';
import Loader from '@/modules/common/Loader';
import { useObjectState } from '@/utils/hooks';

import ErrorBoundary from './ErrorBoundary';
import App from './main/App';

// declare the module to enable theme modification
Expand Down Expand Up @@ -80,42 +81,46 @@ const Root: FC = () => {
<ThemeProvider theme={theme}>
<CssBaseline enableColorScheme />
<I18nextProvider i18n={i18nConfig}>
<QueryClientProvider client={queryClient}>
<ToastContainer />
<WithLocalContext
defaultValue={window.Cypress ? window.appContext : mockContext}
LoadingComponent={<Loader />}
useGetLocalContext={hooks.useGetLocalContext}
useAutoResize={hooks.useAutoResize}
onError={() => {
console.error(
'An error occurred while fetching the context.',
);
}}
>
<WithTokenContext
<ErrorBoundary>
<QueryClientProvider client={queryClient}>
<ToastContainer />
<WithLocalContext
defaultValue={
window.Cypress ? window.appContext : mockContext
}
LoadingComponent={<Loader />}
useAuthToken={hooks.useAuthToken}
useGetLocalContext={hooks.useGetLocalContext}
useAutoResize={hooks.useAutoResize}
onError={() => {
console.error(
'An error occurred while requesting the token.',
'An error occurred while fetching the context.',
);
}}
>
<App />
{import.meta.env.DEV && (
<GraaspContextDevTool
members={mockMembers}
context={mockContext}
setContext={setMockContext}
/>
)}
</WithTokenContext>
</WithLocalContext>
{import.meta.env.DEV && (
<ReactQueryDevtools position="bottom-left" />
)}
</QueryClientProvider>
<WithTokenContext
LoadingComponent={<Loader />}
useAuthToken={hooks.useAuthToken}
onError={() => {
console.error(
'An error occurred while requesting the token.',
);
}}
>
<App />
{import.meta.env.DEV && (
<GraaspContextDevTool
members={mockMembers}
context={mockContext}
setContext={setMockContext}
/>
)}
</WithTokenContext>
</WithLocalContext>
{import.meta.env.DEV && (
<ReactQueryDevtools position="bottom-left" />
)}
</QueryClientProvider>
</ErrorBoundary>
</I18nextProvider>
</ThemeProvider>
</StyledEngineProvider>
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"moduleResolution": "Bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2589,9 +2589,9 @@ __metadata:
languageName: node
linkType: hard

"@graasp/ui@npm:4.9.0":
version: 4.9.0
resolution: "@graasp/ui@npm:4.9.0"
"@graasp/ui@graasp/graasp-ui#755-error-fallback":
version: 4.9.3
resolution: "@graasp/ui@https://github.com/graasp/graasp-ui.git#commit=968b2ed66b1f54a1f317b738d3bec086d45b685c"
dependencies:
"@ag-grid-community/client-side-row-model": "npm:31.1.1"
"@ag-grid-community/react": "npm:^31.1.1"
Expand Down Expand Up @@ -2623,7 +2623,7 @@ __metadata:
react-router-dom: ^6.11.0
stylis: ^4.1.3
stylis-plugin-rtl: ^2.1.1
checksum: 10/e4c3fc8497e35ad28e91098d475029479174cf9e7090bf9061700e5b7f04ce2a9fc584fa9d1ce29e13b03184bf513a54ee4bed7b682a070f76ba378deb056c57
checksum: 10/a1118319dc56b62f9d85887d3e78cd8b6ffba3b106bfac4eb76df8e82ab2ea3a4854e907e96e447dc831cd1ca8fc307256202a33d444b89e5bdba3f6b93093c1
languageName: node
linkType: hard

Expand Down Expand Up @@ -7922,7 +7922,7 @@ __metadata:
"@emotion/styled": "npm:11.11.0"
"@graasp/apps-query-client": "npm:3.4.8"
"@graasp/sdk": "npm:4.1.0"
"@graasp/ui": "npm:4.9.0"
"@graasp/ui": "graasp/graasp-ui#755-error-fallback"
"@mui/icons-material": "npm:5.15.12"
"@mui/lab": "npm:5.0.0-alpha.167"
"@mui/material": "npm:5.15.12"
Expand Down

0 comments on commit 338270c

Please sign in to comment.