Skip to content

Commit

Permalink
[data-studio] handle network errors on global-lang (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmyb authored Aug 24, 2023
1 parent 59ae493 commit 542c70e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ function ApolloHandler({children}: IApolloHandlerProps): JSX.Element {
}

if (networkError) {
// Check if error response is JSON
try {
JSON.parse(networkError.message);
} catch (e) {
// If not replace parsing error message with real one
networkError.message = t('error.network_error_occured_details');
}

const errorContent = t('error.network_error_occurred');

const info: IInfo = {
Expand Down
1 change: 1 addition & 0 deletions apps/data-studio/src/locales/en/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@
"access_denied_details": "You are not allowed to access this element, please contact an administrator",
"graphql_error_occurred": "An error ocurred: {{error}}",
"network_error_occurred": "An network error occurred",
"network_error_occured_details": "Unable to connect to server. Please check your Internet connection.",
"PERMISSION_ERROR": "You are not allowed to perform this action",
"INTERNAL_ERROR": "An internal error occurred",
"page_not_found": "Page not found"
Expand Down
1 change: 1 addition & 0 deletions apps/data-studio/src/locales/fr/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@
"access_denied_details": "Vous n'êtes pas autorisé à accéder à cet élément, veuillez contacter un administrateur.",
"graphql_error_occurred": "Une erreur est survenue: {{error}}",
"network_error_occurred": "Une erreur de connexion est survenue",
"network_error_occured_details": "Impossible de se connecter au serveur. Veuillez vérifier votre connexion Internet.",
"PERMISSION_ERROR": "Vous n'êtes pas autorisé à effectuer cette action",
"INTERNAL_ERROR": "Une erreur interne est survenue",
"page_not_found": "La page demandée n'existe pas"
Expand Down
4 changes: 2 additions & 2 deletions libs/ui/src/components/ErrorDisplay/ErrorDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function ErrorDisplay({
showActionButton = true,
type = ErrorDisplayTypes.ERROR
}: IErrorProps): JSX.Element {
const {t} = useTranslation();
const {t, i18n} = useTranslation();
const history = useHistory();

const _handleBackHomeClick = () => history.replace('/');
Expand All @@ -39,7 +39,7 @@ function ErrorDisplay({

const errorByType = {
[ErrorDisplayTypes.ERROR]: {
title: t('error.error_occurred'),
title: i18n.isInitialized ? t('error.error_occurred') : 'An error occurred',
icon: <CloseCircleFilled color="red" />,
message: '',
actionButton: null
Expand Down
13 changes: 12 additions & 1 deletion libs/ui/src/hooks/useAppLang/useAppLang.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ export default function useAppLang() {
const _fetchLang = async () => {
try {
const res = await fetch('/global-lang', {method: 'GET'});

// make the promise be rejected if we didn't get a 2xx response
if (!res.ok) {
throw new Error(
res.status === 404
? 'Unable to connect to server. Please check your Internet connection.'
: res.statusText,
{cause: res}
);
}

const resContent = await res.text();
setLang(resContent);
} catch (err) {
setError(String(err));
setError(err.message);
} finally {
setIsLoading(false);
}
Expand Down

0 comments on commit 542c70e

Please sign in to comment.