-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Reapply "Implement a global lazyLoading error handling (#7227)" (#7242) This reverts commit fc42926. * put the global context inside the globalStyleProvider * GlobalErrorDialog without custom components * dev logging * Revert "dev logging" This reverts commit 172b67a. * update emotion package, fix imports, serve:dev script
- Loading branch information
1 parent
fc42926
commit 14c64ed
Showing
19 changed files
with
1,725 additions
and
296 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React, { createContext, useContext, useState, ReactNode } from 'react'; | ||
|
||
type GlobalErrorContextType = { | ||
error: Error | null; | ||
setError: (error: Error | null) => void; | ||
}; | ||
|
||
// Global function to set error (to be initialized by provider) | ||
let setGlobalError: ((error: Error | null) => void) | null = null; | ||
|
||
export const useGlobalError = (): GlobalErrorContextType => { | ||
const context = useContext(GlobalErrorContext); | ||
if (!context) { | ||
throw new Error('useGlobalError must be used within a GlobalErrorProvider'); | ||
} | ||
return context; | ||
}; | ||
|
||
const GlobalErrorContext = createContext<GlobalErrorContextType | undefined>(undefined); | ||
|
||
// GlobalErrorProvider but used only for LazyLoading ATM | ||
// see SentryErrorBoundary for global error handling | ||
export const GlobalErrorProvider: React.FC<{ children: ReactNode }> = ({ children }) => { | ||
const [error, setError] = useState<Error | null>(null); | ||
|
||
// Set the global error function during initialization | ||
setGlobalError = setError; | ||
|
||
return <GlobalErrorContext.Provider value={{ error, setError }}>{children}</GlobalErrorContext.Provider>; | ||
}; | ||
|
||
// the global error setter | ||
export const getGlobalErrorSetter = (): ((error: Error | null) => void) => { | ||
if (!setGlobalError) { | ||
throw new Error('GlobalErrorProvider is not initialized.'); | ||
} | ||
return setGlobalError; | ||
}; |
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,52 @@ | ||
import React from 'react'; | ||
import { Trans, useTranslation } from 'react-i18next'; | ||
import { Box, Button, Dialog, DialogContent, DialogTitle } from '@mui/material'; | ||
import { useGlobalError } from './GlobalErrorContext'; | ||
import { LazyLoadError } from './lazyWithGlobalErrorHandler'; | ||
import TranslationKey from '@/core/i18n/utils/TranslationKey'; | ||
|
||
const ErrorTranslationMappings = (error: Error): TranslationKey => { | ||
if (error instanceof LazyLoadError) { | ||
return 'pages.error.errors.LazyLoadError'; | ||
} | ||
return 'pages.error.errors.unknown'; | ||
}; | ||
|
||
export const GlobalErrorDialog: React.FC = () => { | ||
const { t } = useTranslation(); | ||
const { error, setError } = useGlobalError(); | ||
|
||
if (!error) return null; | ||
|
||
return ( | ||
<Dialog open={!!error} aria-labelledby="global-error-dialog" onClose={() => setError(null)}> | ||
<DialogTitle sx={{ m: 0, p: 2 }} id="customized-dialog-title"> | ||
{t('pages.error.title')} | ||
</DialogTitle> | ||
<DialogContent> | ||
<Box> | ||
<Trans | ||
i18nKey="pages.error.line1" | ||
values={{ | ||
message: t(ErrorTranslationMappings(error)), | ||
}} | ||
components={{ | ||
italic: <i />, | ||
}} | ||
/> | ||
</Box> | ||
<Box sx={{ display: 'flex', justifyContent: 'flex-end', marginTop: '20px' }}> | ||
<Button | ||
variant="contained" | ||
onClick={() => { | ||
setError(null); | ||
window.location.reload(); | ||
}} | ||
> | ||
{t('pages.error.buttons.reload')} | ||
</Button> | ||
</Box> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
}; |
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,49 @@ | ||
import React from 'react'; | ||
import { getGlobalErrorSetter } from './GlobalErrorContext'; | ||
|
||
type ImportFunc<T> = () => Promise<{ default: React.ComponentType<T> }>; | ||
|
||
export class LazyLoadError extends Error { | ||
constructor(originalError: Error) { | ||
super(originalError.message); | ||
this.name = 'LazyLoadError'; | ||
Object.setPrototypeOf(this, LazyLoadError.prototype); | ||
} | ||
} | ||
|
||
export const lazyWithGlobalErrorHandler = <T>( | ||
importFunc: ImportFunc<T> | ||
): React.LazyExoticComponent<React.ComponentType<T>> => { | ||
return React.lazy(async () => { | ||
try { | ||
return await importFunc(); | ||
} catch (error) { | ||
const setError = getGlobalErrorSetter(); | ||
const originalError: Error = | ||
error instanceof Error ? error : error?.['message'] ? new Error(error['message']) : new Error('Unknown error'); | ||
|
||
setError(new LazyLoadError(originalError)); | ||
|
||
// it looks like this error is already logged by the useErrorLoggerLink (network error) | ||
|
||
// Instead of throwing, return a fallback component to prevent | ||
// catching it in the ErrorBoundary | ||
return { | ||
default: () => null, | ||
}; | ||
} | ||
}); | ||
}; | ||
|
||
export const lazyImportWithErrorHandler = async <T>(importFunc: () => Promise<T>): Promise<T> => { | ||
try { | ||
return await importFunc(); | ||
} catch (error) { | ||
const setError = getGlobalErrorSetter(); | ||
const originalError: Error = | ||
error instanceof Error ? error : error?.['message'] ? new Error(error['message']) : new Error('Unknown error'); | ||
|
||
setError(new LazyLoadError(originalError)); | ||
throw new LazyLoadError(originalError); | ||
} | ||
}; |
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
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
Oops, something went wrong.