Skip to content

Commit

Permalink
enhancement: better error handing for remote models when there's no i…
Browse files Browse the repository at this point in the history
…nternet connection (#4252)

* enhance: better handling failed to fetch

* chore: remove console

* chore: checking engine showing error failed to fetch

* chore: fix linter

* chore: fix linter error missing assistant
  • Loading branch information
urmauur authored Dec 10, 2024
1 parent 5d9332f commit c15bb9e
Showing 1 changed file with 55 additions and 32 deletions.
87 changes: 55 additions & 32 deletions web/containers/ErrorMessage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import ModalTroubleShooting, {

import { MainViewState } from '@/constants/screens'

import { isLocalEngine } from '@/utils/modelEngine'

import { mainViewStateAtom } from '@/helpers/atoms/App.atom'

import { selectedSettingAtom } from '@/helpers/atoms/Setting.atom'
Expand All @@ -25,64 +27,85 @@ const ErrorMessage = ({ message }: { message: ThreadMessage }) => {
const setSelectedSettingScreen = useSetAtom(selectedSettingAtom)
const activeThread = useAtomValue(activeThreadAtom)

const defaultDesc = () => {
return (
<>
<p>
{`Something's wrong.`} Access&nbsp;
<span
className="cursor-pointer text-[hsla(var(--app-link))] underline"
onClick={() => setModalTroubleShooting(true)}
>
troubleshooting assistance
</span>
&nbsp;now.
</p>
<ModalTroubleShooting />
</>
)
}

const getEngine = () => {
const engineName = activeThread?.assistants?.[0]?.model?.engine
return engineName ? EngineManager.instance().get(engineName) : null
}

const getErrorTitle = () => {
const engine = getEngine()

switch (message.error_code) {
case ErrorCode.InvalidApiKey:
case ErrorCode.AuthenticationError:
return (
<span data-testid="invalid-API-key-error">
Invalid API key. Please check your API key from{' '}
<button
className="font-medium text-[hsla(var(--app-link))] underline"
onClick={() => {
setMainState(MainViewState.Settings)

if (activeThread?.assistants[0]?.model.engine) {
const engine = EngineManager.instance().get(
activeThread.assistants[0].model.engine
)
<>
<span data-testid="invalid-API-key-error">
Invalid API key. Please check your API key from{' '}
<button
className="font-medium text-[hsla(var(--app-link))] underline"
onClick={() => {
setMainState(MainViewState.Settings)
engine?.name && setSelectedSettingScreen(engine.name)
}
}}
>
Settings
</button>{' '}
and try again.
</span>
}}
>
Settings
</button>{' '}
and try again.
</span>
{defaultDesc()}
</>
)
default:
return (
<p
data-testid="passthrough-error-message"
className="first-letter:uppercase"
>
{message.content[0]?.text?.value && (
<AutoLink text={message.content[0].text.value} />
{message.content[0]?.text?.value === 'Failed to fetch' &&
engine &&
!isLocalEngine(String(engine?.name)) ? (
<span>
No internet connection. <br /> Switch to an on-device model or
check connection.
</span>
) : (
<>
<AutoLink text={message.content[0].text.value} />
{defaultDesc()}
</>
)}
</p>
)
}
}

return (
<div className="mx-auto mt-10 max-w-[700px]">
<div className="mx-auto my-6 max-w-[700px]">
{message.status === MessageStatus.Error && (
<div
key={message.id}
className="mx-6 flex flex-col items-center space-y-2 text-center font-medium text-[hsla(var(--text-secondary))]"
>
{getErrorTitle()}
<p>
{`Something's wrong.`} Access&nbsp;
<span
className="cursor-pointer text-[hsla(var(--app-link))] underline"
onClick={() => setModalTroubleShooting(true)}
>
troubleshooting assistance
</span>
&nbsp;now.
</p>
<ModalTroubleShooting />
</div>
)}
</div>
Expand Down

0 comments on commit c15bb9e

Please sign in to comment.