fix: improve error handling in mail and password authentication

This commit is contained in:
twwu 2025-09-17 15:27:10 +08:00
parent c38a48027a
commit f89d6376dd
2 changed files with 49 additions and 49 deletions

View File

@ -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)
}

View File

@ -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<T>(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<ResponseError>
return bodyJson
}
const [parseErr, errRespData] = await asyncRunSafe<ResponseError>(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