@@ -10,6 +10,7 @@ import {
10
10
useContext ,
11
11
} from "react" ;
12
12
import { toast } from "sonner" ;
13
+ import { getErrorMessage } from "@/lib/error-utils" ;
13
14
14
15
interface Message {
15
16
id : number ;
@@ -34,6 +35,22 @@ interface StatusChangeEvent {
34
35
status : string ;
35
36
}
36
37
38
+ interface APIErrorDetail {
39
+ location : string ;
40
+ message : string ;
41
+ value : null | string | number | boolean | object ;
42
+ }
43
+
44
+ interface APIErrorModel {
45
+ $schema : string ;
46
+ detail : string ;
47
+ errors : APIErrorDetail [ ] ;
48
+ instance : string ;
49
+ status : number ;
50
+ title : string ;
51
+ type : string ;
52
+ }
53
+
37
54
function isDraftMessage ( message : Message | DraftMessage ) : boolean {
38
55
return message . id === undefined ;
39
56
}
@@ -235,34 +252,27 @@ export function ChatProvider({ children }: PropsWithChildren) {
235
252
} ) ;
236
253
237
254
if ( ! response . ok ) {
238
- const errorData = await response . json ( ) ;
255
+ const errorData = await response . json ( ) as APIErrorModel ;
239
256
console . error ( "Failed to send message:" , errorData ) ;
240
257
const detail = errorData . detail ;
241
258
const messages =
242
259
"errors" in errorData
243
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
244
- errorData . errors . map ( ( e : any ) => e . message ) . join ( ", " )
260
+ ?
261
+ errorData . errors . map ( ( e : APIErrorDetail ) => e . message ) . join ( ", " )
245
262
: "" ;
246
263
247
264
const fullDetail = `${ detail } : ${ messages } ` ;
248
265
toast . error ( `Failed to send message` , {
249
266
description : fullDetail ,
250
267
} ) ;
251
268
}
252
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
253
- } catch ( error : any ) {
254
- console . error ( "Error sending message:" , error ) ;
255
- const detail = error . detail ;
256
- const messages =
257
- "errors" in error
258
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
259
- error . errors . map ( ( e : any ) => e . message ) . join ( "\n" )
260
- : "" ;
261
269
262
- const fullDetail = `${ detail } : ${ messages } ` ;
270
+ } catch ( error ) {
271
+ console . error ( "Error sending message:" , error ) ;
272
+ const message = getErrorMessage ( error )
263
273
264
274
toast . error ( `Error sending message` , {
265
- description : fullDetail ,
275
+ description : message ,
266
276
} ) ;
267
277
} finally {
268
278
if ( type === "user" ) {
@@ -285,13 +295,13 @@ export function ChatProvider({ children }: PropsWithChildren) {
285
295
286
296
if ( ! response . ok ) {
287
297
result . ok = false ;
288
- const errorData = await response . json ( ) ;
298
+ const errorData = await response . json ( ) as APIErrorModel ;
289
299
console . error ( "Failed to send message:" , errorData ) ;
290
300
const detail = errorData . detail ;
291
301
const messages =
292
302
"errors" in errorData
293
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
294
- errorData . errors . map ( ( e : any ) => e . message ) . join ( ", " )
303
+ ?
304
+ errorData . errors . map ( ( e : APIErrorDetail ) => e . message ) . join ( ", " )
295
305
: "" ;
296
306
297
307
const fullDetail = `${ detail } : ${ messages } ` ;
@@ -301,21 +311,14 @@ export function ChatProvider({ children }: PropsWithChildren) {
301
311
} else {
302
312
result = ( await response . json ( ) ) as FileUploadResponse ;
303
313
}
304
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
305
- } catch ( error : any ) {
314
+
315
+ } catch ( error ) {
306
316
result . ok = false ;
307
317
console . error ( "Error uploading files:" , error ) ;
308
- const detail = error . detail ;
309
- const messages =
310
- "errors" in error
311
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
312
- error . errors . map ( ( e : any ) => e . message ) . join ( "\n" )
313
- : "" ;
314
-
315
- const fullDetail = `${ detail } : ${ messages } ` ;
318
+ const message = getErrorMessage ( error )
316
319
317
320
toast . error ( `Error uploading files` , {
318
- description : fullDetail ,
321
+ description : message ,
319
322
} ) ;
320
323
}
321
324
return result ;
0 commit comments