0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

fix(ui): call error callback after async error handler (#3003)

This commit is contained in:
simeng-li 2023-01-28 15:58:25 +08:00 committed by GitHub
parent 53f49df621
commit b5e7350ee9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 19 deletions

View file

@ -46,12 +46,10 @@ const useContinueFlowCodeVerification = (
const verifyVerificationCodeErrorHandlers: ErrorHandlers = useMemo( const verifyVerificationCodeErrorHandlers: ErrorHandlers = useMemo(
() => ({ () => ({
'user.phone_already_in_use': () => { 'user.phone_already_in_use': async () =>
void identifierExistErrorHandler(SignInIdentifier.Phone, target); identifierExistErrorHandler(SignInIdentifier.Phone, target),
}, 'user.email_already_in_use': async () =>
'user.email_already_in_use': () => { identifierExistErrorHandler(SignInIdentifier.Email, target),
void identifierExistErrorHandler(SignInIdentifier.Email, target);
},
...requiredProfileErrorHandler, ...requiredProfileErrorHandler,
...generalVerificationCodeErrorHandlers, ...generalVerificationCodeErrorHandlers,
callback: errorCallback, callback: errorCallback,

View file

@ -24,9 +24,8 @@ const useForgotPasswordFlowCodeVerification = (
const errorHandlers: ErrorHandlers = useMemo( const errorHandlers: ErrorHandlers = useMemo(
() => ({ () => ({
'user.user_not_exist': () => { 'user.user_not_exist': async () =>
void identifierErrorHandler(IdentifierErrorType.IdentifierNotExist, method, target); identifierErrorHandler(IdentifierErrorType.IdentifierNotExist, method, target),
},
'user.new_password_required_in_profile': () => { 'user.new_password_required_in_profile': () => {
navigate(`/${UserFlow.forgotPassword}/reset`, { replace: true }); navigate(`/${UserFlow.forgotPassword}/reset`, { replace: true });
}, },

View file

@ -13,9 +13,9 @@ type UseApi<T extends unknown[], U> = {
}; };
export type ErrorHandlers = { export type ErrorHandlers = {
[key in LogtoErrorCode]?: (error: RequestErrorBody) => void; [key in LogtoErrorCode]?: (error: RequestErrorBody) => void | Promise<void>;
} & { } & {
global?: (error: RequestErrorBody) => void; // Overwrite default global error handle logic global?: (error: RequestErrorBody) => void | Promise<void>; // Overwrite default global error handle logic
callback?: (error: RequestErrorBody) => void; // Callback method callback?: (error: RequestErrorBody) => void; // Callback method
}; };
@ -77,15 +77,15 @@ function useApi<Args extends unknown[], Response>(
const { code, message } = error; const { code, message } = error;
const handler = errorHandlers?.[code] ?? errorHandlers?.global; const handler = errorHandlers?.[code] ?? errorHandlers?.global;
errorHandlers?.callback?.(error); (async () => {
if (handler) {
await handler(error);
} else {
setToast(message);
}
if (handler) { errorHandlers?.callback?.(error);
handler(error); })();
return;
}
setToast(message);
}, [error, errorHandlers, setToast, t]); }, [error, errorHandlers, setToast, t]);
return { return {