@@ -9,7 +9,7 @@ import * as schema from "@/db/schema";
99import type { ChatThreadStatus } from "@/lib/chat/thread-status" ;
1010import { AppError } from "@/lib/core/errors" ;
1111import { getProjectByIdForUser } from "@/lib/data/projects.server" ;
12- import { isUndefinedTableError } from "@/lib/db/postgres-errors" ;
12+ import { maybeWrapDbNotMigrated } from "@/lib/db/postgres-errors" ;
1313
1414/** Chat thread status values shared with chat data DTOs and update helpers. */
1515export type { ChatThreadStatus } from "@/lib/chat/thread-status" ;
@@ -92,24 +92,27 @@ export async function ensureChatThreadForWorkflowRun(
9292 . onConflictDoNothing ( { target : schema . chatThreadsTable . workflowRunId } )
9393 . returning ( ) ;
9494 } catch ( error ) {
95- if ( isUndefinedTableError ( error ) ) {
96- throw new AppError (
97- "db_not_migrated" ,
98- 500 ,
99- "Database is not migrated. Run migrations and try again." ,
100- error ,
101- ) ;
102- }
103- throw error ;
95+ throw maybeWrapDbNotMigrated (
96+ error ,
97+ "Database is not migrated. Run migrations and try again." ,
98+ ) ;
10499 }
105100
106101 if ( row ) {
107102 return toChatThreadDto ( row ) ;
108103 }
109104
110- const existing = await db . query . chatThreadsTable . findFirst ( {
111- where : eq ( schema . chatThreadsTable . workflowRunId , input . workflowRunId ) ,
112- } ) ;
105+ let existing : ChatThreadRow | undefined ;
106+ try {
107+ existing = await db . query . chatThreadsTable . findFirst ( {
108+ where : eq ( schema . chatThreadsTable . workflowRunId , input . workflowRunId ) ,
109+ } ) ;
110+ } catch ( error ) {
111+ throw maybeWrapDbNotMigrated (
112+ error ,
113+ "Database is not migrated. Run migrations and try again." ,
114+ ) ;
115+ }
113116
114117 if ( ! existing ) {
115118 throw new AppError (
@@ -138,15 +141,7 @@ export const getChatThreadByWorkflowRunId = cache(
138141 } ) ;
139142 return row ? toChatThreadDto ( row ) : null ;
140143 } catch ( error ) {
141- if ( isUndefinedTableError ( error ) ) {
142- throw new AppError (
143- "db_not_migrated" ,
144- 500 ,
145- "Database is not migrated. Run migrations and refresh the page." ,
146- error ,
147- ) ;
148- }
149- throw error ;
144+ throw maybeWrapDbNotMigrated ( error ) ;
150145 }
151146 } ,
152147) ;
@@ -171,15 +166,7 @@ export const getLatestChatThreadByProjectId = cache(
171166 } ) ;
172167 return row ? toChatThreadDto ( row ) : null ;
173168 } catch ( error ) {
174- if ( isUndefinedTableError ( error ) ) {
175- throw new AppError (
176- "db_not_migrated" ,
177- 500 ,
178- "Database is not migrated. Run migrations and refresh the page." ,
179- error ,
180- ) ;
181- }
182- throw error ;
169+ throw maybeWrapDbNotMigrated ( error ) ;
183170 }
184171 } ,
185172) ;
@@ -212,15 +199,7 @@ export const listChatThreadsByProjectId = cache(
212199 } ) ;
213200 return rows . map ( toChatThreadDto ) ;
214201 } catch ( error ) {
215- if ( isUndefinedTableError ( error ) ) {
216- throw new AppError (
217- "db_not_migrated" ,
218- 500 ,
219- "Database is not migrated. Run migrations and refresh the page." ,
220- error ,
221- ) ;
222- }
223- throw error ;
202+ throw maybeWrapDbNotMigrated ( error ) ;
224203 }
225204 } ,
226205) ;
@@ -245,15 +224,7 @@ export const getChatThreadById = cache(
245224 await assertProjectAccess ( row . projectId , userId ) ;
246225 return toChatThreadDto ( row ) ;
247226 } catch ( error ) {
248- if ( isUndefinedTableError ( error ) ) {
249- throw new AppError (
250- "db_not_migrated" ,
251- 500 ,
252- "Database is not migrated. Run migrations and refresh the page." ,
253- error ,
254- ) ;
255- }
256- throw error ;
227+ throw maybeWrapDbNotMigrated ( error ) ;
257228 }
258229 } ,
259230) ;
@@ -345,15 +316,7 @@ export async function appendChatMessages(
345316 ] ,
346317 } ) ;
347318 } catch ( error ) {
348- if ( isUndefinedTableError ( error ) ) {
349- throw new AppError (
350- "db_not_migrated" ,
351- 500 ,
352- "Database is not migrated. Run migrations and refresh the page." ,
353- error ,
354- ) ;
355- }
356- throw error ;
319+ throw maybeWrapDbNotMigrated ( error ) ;
357320 }
358321}
359322
@@ -389,15 +352,7 @@ export const listChatMessagesByThreadId = cache(
389352 } ) ;
390353 return rows . map ( toChatMessageDto ) ;
391354 } catch ( error ) {
392- if ( isUndefinedTableError ( error ) ) {
393- throw new AppError (
394- "db_not_migrated" ,
395- 500 ,
396- "Database is not migrated. Run migrations and refresh the page." ,
397- error ,
398- ) ;
399- }
400- throw error ;
355+ throw maybeWrapDbNotMigrated ( error ) ;
401356 }
402357 } ,
403358) ;
@@ -435,14 +390,9 @@ export async function updateChatThreadByWorkflowRunId(
435390 . set ( next )
436391 . where ( eq ( schema . chatThreadsTable . workflowRunId , workflowRunId ) ) ;
437392 } catch ( error ) {
438- if ( isUndefinedTableError ( error ) ) {
439- throw new AppError (
440- "db_not_migrated" ,
441- 500 ,
442- "Database is not migrated. Run migrations and try again." ,
443- error ,
444- ) ;
445- }
446- throw error ;
393+ throw maybeWrapDbNotMigrated (
394+ error ,
395+ "Database is not migrated. Run migrations and try again." ,
396+ ) ;
447397 }
448398}
0 commit comments