feat: improve error messages (#1006)
This commit is contained in:
parent
9127e1be88
commit
8a7d3e9950
13 changed files with 370 additions and 174 deletions
|
|
@ -32,6 +32,7 @@ import type { ArtifactKind } from '@/components/artifact';
|
|||
import { generateUUID } from '../utils';
|
||||
import { generateHashedPassword } from './utils';
|
||||
import type { VisibilityType } from '@/components/visibility-selector';
|
||||
import { ChatSDKError } from '../errors';
|
||||
|
||||
// Optionally, if not using email/pass login, you can
|
||||
// use the Drizzle adapter for Auth.js / NextAuth
|
||||
|
|
@ -45,8 +46,10 @@ export async function getUser(email: string): Promise<Array<User>> {
|
|||
try {
|
||||
return await db.select().from(user).where(eq(user.email, email));
|
||||
} catch (error) {
|
||||
console.error('Failed to get user from database');
|
||||
throw error;
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get user by email',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -56,8 +59,7 @@ export async function createUser(email: string, password: string) {
|
|||
try {
|
||||
return await db.insert(user).values({ email, password: hashedPassword });
|
||||
} catch (error) {
|
||||
console.error('Failed to create user in database');
|
||||
throw error;
|
||||
throw new ChatSDKError('bad_request:database', 'Failed to create user');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -71,8 +73,10 @@ export async function createGuestUser() {
|
|||
email: user.email,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to create guest user in database');
|
||||
throw error;
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to create guest user',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -96,8 +100,7 @@ export async function saveChat({
|
|||
visibility,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to save chat in database');
|
||||
throw error;
|
||||
throw new ChatSDKError('bad_request:database', 'Failed to save chat');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -113,8 +116,10 @@ export async function deleteChatById({ id }: { id: string }) {
|
|||
.returning();
|
||||
return chatsDeleted;
|
||||
} catch (error) {
|
||||
console.error('Failed to delete chat by id from database');
|
||||
throw error;
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to delete chat by id',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -154,7 +159,10 @@ export async function getChatsByUserId({
|
|||
.limit(1);
|
||||
|
||||
if (!selectedChat) {
|
||||
throw new Error(`Chat with id ${startingAfter} not found`);
|
||||
throw new ChatSDKError(
|
||||
'not_found:database',
|
||||
`Chat with id ${startingAfter} not found`,
|
||||
);
|
||||
}
|
||||
|
||||
filteredChats = await query(gt(chat.createdAt, selectedChat.createdAt));
|
||||
|
|
@ -166,7 +174,10 @@ export async function getChatsByUserId({
|
|||
.limit(1);
|
||||
|
||||
if (!selectedChat) {
|
||||
throw new Error(`Chat with id ${endingBefore} not found`);
|
||||
throw new ChatSDKError(
|
||||
'not_found:database',
|
||||
`Chat with id ${endingBefore} not found`,
|
||||
);
|
||||
}
|
||||
|
||||
filteredChats = await query(lt(chat.createdAt, selectedChat.createdAt));
|
||||
|
|
@ -181,8 +192,10 @@ export async function getChatsByUserId({
|
|||
hasMore,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Failed to get chats by user from database');
|
||||
throw error;
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get chats by user id',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -191,8 +204,7 @@ export async function getChatById({ id }: { id: string }) {
|
|||
const [selectedChat] = await db.select().from(chat).where(eq(chat.id, id));
|
||||
return selectedChat;
|
||||
} catch (error) {
|
||||
console.error('Failed to get chat by id from database');
|
||||
throw error;
|
||||
throw new ChatSDKError('bad_request:database', 'Failed to get chat by id');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -204,8 +216,7 @@ export async function saveMessages({
|
|||
try {
|
||||
return await db.insert(message).values(messages);
|
||||
} catch (error) {
|
||||
console.error('Failed to save messages in database', error);
|
||||
throw error;
|
||||
throw new ChatSDKError('bad_request:database', 'Failed to save messages');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -217,8 +228,10 @@ export async function getMessagesByChatId({ id }: { id: string }) {
|
|||
.where(eq(message.chatId, id))
|
||||
.orderBy(asc(message.createdAt));
|
||||
} catch (error) {
|
||||
console.error('Failed to get messages by chat id from database', error);
|
||||
throw error;
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get messages by chat id',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -249,8 +262,7 @@ export async function voteMessage({
|
|||
isUpvoted: type === 'up',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to upvote message in database', error);
|
||||
throw error;
|
||||
throw new ChatSDKError('bad_request:database', 'Failed to vote message');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -258,8 +270,10 @@ export async function getVotesByChatId({ id }: { id: string }) {
|
|||
try {
|
||||
return await db.select().from(vote).where(eq(vote.chatId, id));
|
||||
} catch (error) {
|
||||
console.error('Failed to get votes by chat id from database', error);
|
||||
throw error;
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get votes by chat id',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -289,8 +303,7 @@ export async function saveDocument({
|
|||
})
|
||||
.returning();
|
||||
} catch (error) {
|
||||
console.error('Failed to save document in database');
|
||||
throw error;
|
||||
throw new ChatSDKError('bad_request:database', 'Failed to save document');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -304,8 +317,10 @@ export async function getDocumentsById({ id }: { id: string }) {
|
|||
|
||||
return documents;
|
||||
} catch (error) {
|
||||
console.error('Failed to get document by id from database');
|
||||
throw error;
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get documents by id',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -319,8 +334,10 @@ export async function getDocumentById({ id }: { id: string }) {
|
|||
|
||||
return selectedDocument;
|
||||
} catch (error) {
|
||||
console.error('Failed to get document by id from database');
|
||||
throw error;
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get document by id',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -346,10 +363,10 @@ export async function deleteDocumentsByIdAfterTimestamp({
|
|||
.where(and(eq(document.id, id), gt(document.createdAt, timestamp)))
|
||||
.returning();
|
||||
} catch (error) {
|
||||
console.error(
|
||||
'Failed to delete documents by id after timestamp from database',
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to delete documents by id after timestamp',
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -361,8 +378,10 @@ export async function saveSuggestions({
|
|||
try {
|
||||
return await db.insert(suggestion).values(suggestions);
|
||||
} catch (error) {
|
||||
console.error('Failed to save suggestions in database');
|
||||
throw error;
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to save suggestions',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -377,10 +396,10 @@ export async function getSuggestionsByDocumentId({
|
|||
.from(suggestion)
|
||||
.where(and(eq(suggestion.documentId, documentId)));
|
||||
} catch (error) {
|
||||
console.error(
|
||||
'Failed to get suggestions by document version from database',
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get suggestions by document id',
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -388,8 +407,10 @@ export async function getMessageById({ id }: { id: string }) {
|
|||
try {
|
||||
return await db.select().from(message).where(eq(message.id, id));
|
||||
} catch (error) {
|
||||
console.error('Failed to get message by id from database');
|
||||
throw error;
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get message by id',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -424,10 +445,10 @@ export async function deleteMessagesByChatIdAfterTimestamp({
|
|||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(
|
||||
'Failed to delete messages by id after timestamp from database',
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to delete messages by chat id after timestamp',
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -441,8 +462,10 @@ export async function updateChatVisiblityById({
|
|||
try {
|
||||
return await db.update(chat).set({ visibility }).where(eq(chat.id, chatId));
|
||||
} catch (error) {
|
||||
console.error('Failed to update chat visibility in database');
|
||||
throw error;
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to update chat visibility by id',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -470,10 +493,10 @@ export async function getMessageCountByUserId({
|
|||
|
||||
return stats?.count ?? 0;
|
||||
} catch (error) {
|
||||
console.error(
|
||||
'Failed to get message count by user id for the last 24 hours from database',
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get message count by user id',
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -489,8 +512,10 @@ export async function createStreamId({
|
|||
.insert(stream)
|
||||
.values({ id: streamId, chatId, createdAt: new Date() });
|
||||
} catch (error) {
|
||||
console.error('Failed to create stream id in database');
|
||||
throw error;
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to create stream id',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -505,7 +530,9 @@ export async function getStreamIdsByChatId({ chatId }: { chatId: string }) {
|
|||
|
||||
return streamIds.map(({ id }) => id);
|
||||
} catch (error) {
|
||||
console.error('Failed to get stream ids by chat id from database');
|
||||
throw error;
|
||||
throw new ChatSDKError(
|
||||
'bad_request:database',
|
||||
'Failed to get stream ids by chat id',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue