@@ -10,6 +10,7 @@ import {
1010 useContext ,
1111} from "react" ;
1212import { toast } from "sonner" ;
13+ import { getErrorMessage } from "@/lib/error-utils" ;
1314
1415interface Message {
1516 id : number ;
@@ -34,6 +35,22 @@ interface StatusChangeEvent {
3435 status : string ;
3536}
3637
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+
3754function isDraftMessage ( message : Message | DraftMessage ) : boolean {
3855 return message . id === undefined ;
3956}
@@ -235,34 +252,27 @@ export function ChatProvider({ children }: PropsWithChildren) {
235252 } ) ;
236253
237254 if ( ! response . ok ) {
238- const errorData = await response . json ( ) ;
255+ const errorData = await response . json ( ) as APIErrorModel ;
239256 console . error ( "Failed to send message:" , errorData ) ;
240257 const detail = errorData . detail ;
241258 const messages =
242259 "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 ( ", " )
245262 : "" ;
246263
247264 const fullDetail = `${ detail } : ${ messages } ` ;
248265 toast . error ( `Failed to send message` , {
249266 description : fullDetail ,
250267 } ) ;
251268 }
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- : "" ;
261269
262- const fullDetail = `${ detail } : ${ messages } ` ;
270+ } catch ( error ) {
271+ console . error ( "Error sending message:" , error ) ;
272+ const message = getErrorMessage ( error )
263273
264274 toast . error ( `Error sending message` , {
265- description : fullDetail ,
275+ description : message ,
266276 } ) ;
267277 } finally {
268278 if ( type === "user" ) {
@@ -285,13 +295,13 @@ export function ChatProvider({ children }: PropsWithChildren) {
285295
286296 if ( ! response . ok ) {
287297 result . ok = false ;
288- const errorData = await response . json ( ) ;
298+ const errorData = await response . json ( ) as APIErrorModel ;
289299 console . error ( "Failed to send message:" , errorData ) ;
290300 const detail = errorData . detail ;
291301 const messages =
292302 "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 ( ", " )
295305 : "" ;
296306
297307 const fullDetail = `${ detail } : ${ messages } ` ;
@@ -301,21 +311,14 @@ export function ChatProvider({ children }: PropsWithChildren) {
301311 } else {
302312 result = ( await response . json ( ) ) as FileUploadResponse ;
303313 }
304- // eslint-disable-next-line @typescript-eslint/no-explicit-any
305- } catch ( error : any ) {
314+
315+ } catch ( error ) {
306316 result . ok = false ;
307317 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 )
316319
317320 toast . error ( `Error uploading files` , {
318- description : fullDetail ,
321+ description : message ,
319322 } ) ;
320323 }
321324 return result ;
0 commit comments