feat: improve error messages (#1006)
This commit is contained in:
parent
9127e1be88
commit
8a7d3e9950
13 changed files with 370 additions and 174 deletions
42
lib/utils.ts
42
lib/utils.ts
|
|
@ -2,32 +2,44 @@ import type { CoreAssistantMessage, CoreToolMessage, UIMessage } from 'ai';
|
|||
import { type ClassValue, clsx } from 'clsx';
|
||||
import { twMerge } from 'tailwind-merge';
|
||||
import type { Document } from '@/lib/db/schema';
|
||||
import { ChatSDKError, type ErrorCode } from './errors';
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
|
||||
interface ApplicationError extends Error {
|
||||
info: string;
|
||||
status: number;
|
||||
}
|
||||
|
||||
export const fetcher = async (url: string) => {
|
||||
const res = await fetch(url);
|
||||
const response = await fetch(url);
|
||||
|
||||
if (!res.ok) {
|
||||
const error = new Error(
|
||||
'An error occurred while fetching the data.',
|
||||
) as ApplicationError;
|
||||
if (!response.ok) {
|
||||
const { code, cause } = await response.json();
|
||||
throw new ChatSDKError(code as ErrorCode, cause);
|
||||
}
|
||||
|
||||
error.info = await res.json();
|
||||
error.status = res.status;
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export async function fetchWithErrorHandlers(
|
||||
input: RequestInfo | URL,
|
||||
init?: RequestInit,
|
||||
) {
|
||||
try {
|
||||
const response = await fetch(input, init);
|
||||
|
||||
if (!response.ok) {
|
||||
const { code, cause } = await response.json();
|
||||
throw new ChatSDKError(code as ErrorCode, cause);
|
||||
}
|
||||
|
||||
return response;
|
||||
} catch (error: unknown) {
|
||||
if (typeof navigator !== 'undefined' && !navigator.onLine) {
|
||||
throw new ChatSDKError('offline:chat');
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
return res.json();
|
||||
};
|
||||
}
|
||||
|
||||
export function getLocalStorage(key: string) {
|
||||
if (typeof window !== 'undefined') {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue