mirror of
https://github.com/logto-io/logto.git
synced 2025-01-27 21:39:16 -05:00
Merge pull request #2205 from logto-io/charles-log-4430-the-unknown-server-error-failed-to-show-i18n-content
refactor: use hook for toastError in order to get type support
This commit is contained in:
commit
64b47a0801
1 changed files with 16 additions and 10 deletions
|
@ -1,7 +1,6 @@
|
||||||
import { useLogto } from '@logto/react';
|
import { useLogto } from '@logto/react';
|
||||||
import { RequestErrorBody } from '@logto/schemas';
|
import { RequestErrorBody } from '@logto/schemas';
|
||||||
import { managementResource } from '@logto/schemas/lib/seeds';
|
import { managementResource } from '@logto/schemas/lib/seeds';
|
||||||
import { t } from 'i18next';
|
|
||||||
import ky from 'ky';
|
import ky from 'ky';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { toast } from 'react-hot-toast';
|
import { toast } from 'react-hot-toast';
|
||||||
|
@ -20,15 +19,21 @@ export class RequestError extends Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const toastError = async (response: Response) => {
|
const useToastError = () => {
|
||||||
const fallbackErrorMessage = t('admin_console.errors.unknown_server_error');
|
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
|
||||||
|
|
||||||
try {
|
const toastError = async (response: Response) => {
|
||||||
const data = await response.json<RequestErrorBody>();
|
const fallbackErrorMessage = t('errors.unknown_server_error');
|
||||||
toast.error([data.message, data.details].join('\n') || fallbackErrorMessage);
|
|
||||||
} catch {
|
try {
|
||||||
toast.error(fallbackErrorMessage);
|
const data = await response.json<RequestErrorBody>();
|
||||||
}
|
toast.error([data.message, data.details].join('\n') || fallbackErrorMessage);
|
||||||
|
} catch {
|
||||||
|
toast.error(fallbackErrorMessage);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return toastError;
|
||||||
};
|
};
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
|
@ -38,6 +43,7 @@ type Props = {
|
||||||
const useApi = ({ hideErrorToast }: Props = {}) => {
|
const useApi = ({ hideErrorToast }: Props = {}) => {
|
||||||
const { isAuthenticated, getAccessToken } = useLogto();
|
const { isAuthenticated, getAccessToken } = useLogto();
|
||||||
const { i18n } = useTranslation();
|
const { i18n } = useTranslation();
|
||||||
|
const toastError = useToastError();
|
||||||
|
|
||||||
const api = useMemo(
|
const api = useMemo(
|
||||||
() =>
|
() =>
|
||||||
|
@ -64,7 +70,7 @@ const useApi = ({ hideErrorToast }: Props = {}) => {
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
[hideErrorToast, isAuthenticated, getAccessToken, i18n.language]
|
[hideErrorToast, toastError, isAuthenticated, getAccessToken, i18n.language]
|
||||||
);
|
);
|
||||||
|
|
||||||
return api;
|
return api;
|
||||||
|
|
Loading…
Add table
Reference in a new issue