@@ -84,6 +84,36 @@ class ProcessingError extends KnowledgeUploadError {
8484 }
8585}
8686
87+ /**
88+ * Reads a failed `Response`'s JSON body and produces a user-facing error
89+ * string that combines the top-level `error`/`message` with any Zod
90+ * `details[].message` entries. Falls back to `statusText` then status code
91+ * when the body is unreadable.
92+ */
93+ async function readResponseError (
94+ response : Response ,
95+ fallback = 'Unknown error'
96+ ) : Promise < { message : string ; body : any } > {
97+ let body : any = null
98+ try {
99+ body = await response . json ( )
100+ } catch {
101+ body = null
102+ }
103+ const base =
104+ body ?. error || body ?. message || response . statusText || `HTTP ${ response . status } ` || fallback
105+ const detailMessages = Array . isArray ( body ?. details )
106+ ? body . details
107+ . map ( ( d : any ) => ( typeof d ?. message === 'string' ? d . message : null ) )
108+ . filter ( ( m : string | null ) : m is string => Boolean ( m ) )
109+ . join ( ', ' )
110+ : ''
111+ return {
112+ message : detailMessages ? `${ base } : ${ detailMessages } ` : base ,
113+ body,
114+ }
115+ }
116+
87117/**
88118 * Configuration constants for file upload operations
89119 */
@@ -293,22 +323,12 @@ const getPresignedData = async (
293323 } )
294324
295325 if ( ! presignedResponse . ok ) {
296- let errorDetails : any = null
297- try {
298- errorDetails = await presignedResponse . json ( )
299- } catch {
300- errorDetails = null
301- }
302-
326+ const { message, body } = await readResponseError ( presignedResponse )
303327 logger . error ( 'Presigned URL request failed' , {
304328 status : presignedResponse . status ,
305329 fileSize : file . size ,
306330 } )
307-
308- throw new PresignedUrlError (
309- `Failed to get presigned URL for ${ file . name } : ${ presignedResponse . status } ${ presignedResponse . statusText } ` ,
310- errorDetails
311- )
331+ throw new PresignedUrlError ( `Failed to get presigned URL for ${ file . name } : ${ message } ` , body )
312332 }
313333
314334 const presignedData = await presignedResponse . json ( )
@@ -786,17 +806,8 @@ export function useKnowledgeUpload(options: UseKnowledgeUploadOptions = {}) {
786806 } )
787807
788808 if ( ! uploadResponse . ok ) {
789- let errorData : any = null
790- try {
791- errorData = await uploadResponse . json ( )
792- } catch {
793- errorData = null
794- }
795-
796- throw new DirectUploadError (
797- `Failed to upload ${ file . name } : ${ errorData ?. message || errorData ?. error || 'Unknown error' } ` ,
798- errorData
799- )
809+ const { message, body } = await readResponseError ( uploadResponse )
810+ throw new DirectUploadError ( `Failed to upload ${ file . name } : ${ message } ` , body )
800811 }
801812
802813 const uploadResult = await uploadResponse . json ( )
@@ -878,9 +889,8 @@ export function useKnowledgeUpload(options: UseKnowledgeUploadOptions = {}) {
878889 } )
879890
880891 if ( ! batchResponse . ok ) {
881- throw new Error (
882- `Batch ${ batchIndex + 1 } presigned URL generation failed: ${ batchResponse . statusText } `
883- )
892+ const { message } = await readResponseError ( batchResponse )
893+ throw new Error ( `Batch ${ batchIndex + 1 } presigned URL generation failed: ${ message } ` )
884894 }
885895
886896 const { files : presignedData } = await batchResponse . json ( )
@@ -1022,28 +1032,18 @@ export function useKnowledgeUpload(options: UseKnowledgeUploadOptions = {}) {
10221032 } )
10231033
10241034 if ( ! processResponse . ok ) {
1025- let errorData : any = null
1026- try {
1027- errorData = await processResponse . json ( )
1028- } catch {
1029- errorData = null
1030- }
1031-
1035+ const { message, body } = await readResponseError ( processResponse )
10321036 logger . error ( 'Document processing failed:' , {
10331037 status : processResponse . status ,
1034- error : errorData ,
1038+ error : body ,
10351039 uploadedFiles : uploadedFiles . map ( ( f ) => ( {
10361040 filename : f . filename ,
10371041 fileUrl : f . fileUrl ,
10381042 fileSize : f . fileSize ,
10391043 mimeType : f . mimeType ,
10401044 } ) ) ,
10411045 } )
1042-
1043- throw new ProcessingError (
1044- `Failed to start document processing: ${ errorData ?. error || errorData ?. message || 'Unknown error' } ` ,
1045- errorData
1046- )
1046+ throw new ProcessingError ( `Failed to start document processing: ${ message } ` , body )
10471047 }
10481048
10491049 const processResult = await processResponse . json ( )
0 commit comments