From f89d6376dd1ffc6503417783392937301ab7ad85 Mon Sep 17 00:00:00 2001 From: twwu Date: Wed, 17 Sep 2025 15:27:10 +0800 Subject: [PATCH] fix: improve error handling in mail and password authentication --- .../components/mail-and-password-auth.tsx | 16 ++-- web/service/base.ts | 82 +++++++++---------- 2 files changed, 49 insertions(+), 49 deletions(-) diff --git a/web/app/signin/components/mail-and-password-auth.tsx b/web/app/signin/components/mail-and-password-auth.tsx index ca8a481feb..cccbd3a6ee 100644 --- a/web/app/signin/components/mail-and-password-auth.tsx +++ b/web/app/signin/components/mail-and-password-auth.tsx @@ -11,6 +11,7 @@ import Input from '@/app/components/base/input' import I18NContext from '@/context/i18n' import { noop } from 'lodash-es' import { resolvePostLoginRedirect } from '../utils/post-login-redirect' +import type { ResponseError } from '@/service/fetch' type MailAndPasswordAuthProps = { isInvite: boolean @@ -73,12 +74,6 @@ export default function MailAndPasswordAuth({ isInvite, isEmailSetup, allowRegis router.replace(redirectUrl || '/apps') } } - else if (res.code === 'authentication_failed') { - Toast.notify({ - type: 'error', - message: t('login.error.invalidEmailOrPassword'), - }) - } else { Toast.notify({ type: 'error', @@ -86,7 +81,14 @@ export default function MailAndPasswordAuth({ isInvite, isEmailSetup, allowRegis }) } } - + catch (error) { + if ((error as ResponseError).code === 'authentication_failed') { + Toast.notify({ + type: 'error', + message: t('login.error.invalidEmailOrPassword'), + }) + } + } finally { setIsLoading(false) } diff --git a/web/service/base.ts b/web/service/base.ts index 87f1180c9e..be15af895d 100644 --- a/web/service/base.ts +++ b/web/service/base.ts @@ -464,42 +464,44 @@ export const ssePost = async ( } return } - return handleStream(res, (str: string, isFirstMessage: boolean, moreInfo: IOnDataMoreInfo) => { - if (moreInfo.errorMessage) { - onError?.(moreInfo.errorMessage, moreInfo.errorCode) - // TypeError: Cannot assign to read only property ... will happen in page leave, so it should be ignored. - if (moreInfo.errorMessage !== 'AbortError: The user aborted a request.' && !moreInfo.errorMessage.includes('TypeError: Cannot assign to read only property')) - Toast.notify({ type: 'error', message: moreInfo.errorMessage }) - return - } - onData?.(str, isFirstMessage, moreInfo) - }, - onCompleted, - onThought, - onMessageEnd, - onMessageReplace, - onFile, - onWorkflowStarted, - onWorkflowFinished, - onNodeStarted, - onNodeFinished, - onIterationStart, - onIterationNext, - onIterationFinish, - onLoopStart, - onLoopNext, - onLoopFinish, - onNodeRetry, - onParallelBranchStarted, - onParallelBranchFinished, - onTextChunk, - onTTSChunk, - onTTSEnd, - onTextReplace, - onAgentLog, - onDataSourceNodeProcessing, - onDataSourceNodeCompleted, - onDataSourceNodeError, + return handleStream( + res, + (str: string, isFirstMessage: boolean, moreInfo: IOnDataMoreInfo) => { + if (moreInfo.errorMessage) { + onError?.(moreInfo.errorMessage, moreInfo.errorCode) + // TypeError: Cannot assign to read only property ... will happen in page leave, so it should be ignored. + if (moreInfo.errorMessage !== 'AbortError: The user aborted a request.' && !moreInfo.errorMessage.includes('TypeError: Cannot assign to read only property')) + Toast.notify({ type: 'error', message: moreInfo.errorMessage }) + return + } + onData?.(str, isFirstMessage, moreInfo) + }, + onCompleted, + onThought, + onMessageEnd, + onMessageReplace, + onFile, + onWorkflowStarted, + onWorkflowFinished, + onNodeStarted, + onNodeFinished, + onIterationStart, + onIterationNext, + onIterationFinish, + onLoopStart, + onLoopNext, + onLoopFinish, + onNodeRetry, + onParallelBranchStarted, + onParallelBranchFinished, + onTextChunk, + onTTSChunk, + onTTSEnd, + onTextReplace, + onAgentLog, + onDataSourceNodeProcessing, + onDataSourceNodeCompleted, + onDataSourceNodeError, ) }).catch((e) => { if (e.toString() !== 'AbortError: The user aborted a request.' && !e.toString().includes('TypeError: Cannot assign to read only property')) @@ -517,18 +519,14 @@ export const request = async(url: string, options = {}, otherOptions?: IOther return resp const errResp: Response = err as any if (errResp.status === 401) { - if(/\/login/.test(url)) { - const clonedResponse = errResp.clone() - const bodyJson = await clonedResponse.json() as Promise - return bodyJson - } - const [parseErr, errRespData] = await asyncRunSafe(errResp.json()) const loginUrl = `${globalThis.location.origin}${basePath}/signin` if (parseErr) { globalThis.location.href = loginUrl return Promise.reject(err) } + if (/\/login/.test(url)) + return Promise.reject(errRespData) // special code const { code, message } = errRespData // webapp sso