import * as React from 'react'; import { AlertCircle } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { cn } from '@/lib/utils'; /** * 错误展示组件属性 * 用于统一显示加载失败、请求失败等错误状态 */ export interface ErrorDisplayProps { title?: string; message?: string; error?: Error | null; onRetry?: () => void; retryText?: string; icon?: React.ComponentType<{ className?: string }>; className?: string; showStack?: boolean; } /** * 错误展示组件 * 用于统一显示加载失败、请求失败等错误状态 * * @example * ```tsx * // 基础用法 * * * // 带重试按钮 * refetch()} * /> * * // 自定义样式 * * ``` * @param {ErrorDisplayProps} props - 错误展示组件属性 * @returns {React.ReactNode} 错误展示组件 */ export function ErrorDisplay({ title = '加载失败', message, error, onRetry, retryText = '重试', icon: Icon = AlertCircle, className, showStack = false, }: ErrorDisplayProps) { const errorMessage = message || error?.message || '发生未知错误,请稍后重试'; return ( {title} {errorMessage} {onRetry && ( {retryText} )} {showStack && error?.stack && process.env.NODE_ENV === 'development' && ( 查看详细错误信息 {error.stack} )} ); } /** * 区域级错误组件 * 适合在较小的区域显示错误 * * @example * ```tsx * refetch()} * /> * ``` */ export function ErrorInline({ message, error, onRetry, className, }: Omit) { const errorMessage = message || error?.message || '发生未知错误'; return ( {errorMessage} {onRetry && ( 重试 )} ); } /** * 页面级错误组件 * 适合作为页面主要内容的错误状态 * * @example * ```tsx * refetch()} * /> * ``` */ export function ErrorPage(props: ErrorDisplayProps) { return ( ); }
{errorMessage}
{error.stack}