@@ -75,22 +75,37 @@ export const closeSession = () => {
7575 session = undefined ;
7676} ;
7777
78+ function createRequestError ( error : unknown , requestUrl : string ) {
79+ const message =
80+ typeof error === 'string'
81+ ? error
82+ : error instanceof Error
83+ ? error . message
84+ : String ( error ) ;
85+ return new Error ( `${ message } \nURL: ${ requestUrl } ` ) ;
86+ }
87+
7888async function query ( url : string , options : fetch . RequestInit ) {
7989 const baseUrl = await getBaseUrl ;
8090 const fullUrl = `${ baseUrl } ${ url } ` ;
81- const resp = await fetch ( fullUrl , options ) ;
91+ let resp : fetch . Response ;
92+ try {
93+ resp = await fetch ( fullUrl , options ) ;
94+ } catch ( error ) {
95+ throw createRequestError ( error , fullUrl ) ;
96+ }
8297 const text = await resp . text ( ) ;
8398 let json : any ;
8499 try {
85100 json = JSON . parse ( text ) ;
86101 } catch ( e ) { }
87102
88103 if ( resp . status !== 200 ) {
89- const message = json ?. message || resp . statusText ;
104+ const message = json ?. message || resp . statusText || `HTTP ${ resp . status } ` ;
90105 if ( resp . status === 401 ) {
91- throw new Error ( t ( 'loginExpired' ) ) ;
106+ throw createRequestError ( t ( 'loginExpired' ) , fullUrl ) ;
92107 }
93- throw new Error ( message ) ;
108+ throw createRequestError ( message , fullUrl ) ;
94109 }
95110 return json ;
96111}
@@ -195,13 +210,21 @@ export async function uploadFile(fn: string, key?: string) {
195210 // contentType: 'application/octet-stream',
196211 // });
197212
198- const res = await fetch ( realUrl , {
199- method : 'POST' ,
200- body : form ,
201- } ) ;
213+ let res : fetch . Response ;
214+ try {
215+ res = await fetch ( realUrl , {
216+ method : 'POST' ,
217+ body : form ,
218+ } ) ;
219+ } catch ( error ) {
220+ throw createRequestError ( error , realUrl ) ;
221+ }
202222
203223 if ( res . status > 299 ) {
204- throw new Error ( `${ res . status } : ${ res . statusText } ` ) ;
224+ throw createRequestError (
225+ `${ res . status } : ${ res . statusText || 'Upload failed' } ` ,
226+ realUrl ,
227+ ) ;
205228 }
206229
207230 // const body = await response.json();
0 commit comments