Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui): handle request error in admin panel #3589

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions ee/tabby-ui/app/(dashboard)/system/components/cluster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Input } from '@/components/ui/input'
import { Separator } from '@/components/ui/separator'
import { Skeleton } from '@/components/ui/skeleton'
import { CopyButton } from '@/components/copy-button'
import { ErrorView } from '@/components/error-view'
import LoadingWrapper from '@/components/loading-wrapper'

import WorkerCard from './worker-card'
Expand Down Expand Up @@ -48,8 +49,8 @@ function toBadgeString(str: string) {
}

export default function Workers() {
const { data: healthInfo } = useHealth()
const { data: workers, fetching } = useWorkers()
const { data: healthInfo, error: healthError } = useHealth()
const { data: workers, isLoading, error: workersError } = useWorkers()
const [{ data: registrationTokenRes }, reexecuteQuery] = useQuery({
query: getRegistrationTokenDocument
})
Expand All @@ -60,6 +61,12 @@ export default function Workers() {
}
})

const error = healthError || workersError

if (error) {
return <ErrorView title={error?.message} />
}

if (!healthInfo) return

return (
Expand All @@ -84,7 +91,7 @@ export default function Workers() {
<Usage />
<Separator />
<LoadingWrapper
loading={fetching}
loading={isLoading}
fallback={<Skeleton className="mt-3 h-32 w-full lg:w-2/3" />}
>
<>
Expand Down
63 changes: 63 additions & 0 deletions ee/tabby-ui/components/error-view.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use client'

import { ReactNode } from 'react'

import { cn } from '@/lib/utils'
import { Button } from '@/components/ui/button'

export function ErrorView({
title,
description,
className,
children,
statusText,
hideChildren
}: {
title?: string
description?: string
className?: string
hideChildren?: boolean
children?: ReactNode
statusText?: string
}) {
let defaultTitle = 'Something went wrong'
let defaultDescription = 'Oops! Please try again later.'
let displayTitle = ''
let displayDescription = description || defaultDescription

switch (statusText) {
case 'Too Many Requests':
displayTitle = 'Too Many Requests'
break
case 'Bad Request':
displayTitle = 'Bad Request'
break
default:
displayTitle = title || defaultTitle
}

return (
<div className={cn('mx-auto mt-8 max-w-md text-center', className)}>
<h1 className="text-2xl font-bold tracking-tight text-foreground sm:text-3xl">
{displayTitle}
</h1>
{!!displayDescription && (
<p className="mt-4 text-muted-foreground">{displayDescription}</p>
)}
{!hideChildren && (
<div>
{children ? (
children
) : (
<Button
className={cn('mt-6')}
onClick={e => window.location.reload()}
>
Refresh
</Button>
)}
</div>
)}
</div>
)
}
4 changes: 2 additions & 2 deletions ee/tabby-ui/lib/hooks/use-health.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export function useHealth(): SWRResponse<HealthInfo> {
'/v1/health',
(url: string) => {
return fetcher(url, {
errorHandler: () => {
throw new Error('Unhealth')
errorHandler: response => {
throw new Error(response?.statusText.toString() || 'Unhealth')
}
})
},
Expand Down
4 changes: 2 additions & 2 deletions ee/tabby-ui/lib/hooks/use-workers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function transformHealthInfoToChatWorker(healthInfo: HealthInfo) {
}

function useWorkers() {
const { data: healthInfo, isLoading: fetching } = useHealth()
const { data: healthInfo, isLoading, error } = useHealth()

const groupedWorkers = React.useMemo(() => {
const workers = []
Expand All @@ -45,7 +45,7 @@ function useWorkers() {
return groupBy(workers, 'kind')
}, [healthInfo])

return { data: groupedWorkers, fetching }
return { data: groupedWorkers, isLoading, error }
}

export { useWorkers }
Loading