File tree Expand file tree Collapse file tree 1 file changed +15
-8
lines changed Expand file tree Collapse file tree 1 file changed +15
-8
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,11 @@ export type ErrorResponse = {
77  message : string 
88} 
99
10+ const  isErrorResponse  =  ( data : unknown ) : data  is ErrorResponse  =>  { 
11+   const  isObject  =  typeof  data  ===  'object'  &&  data  !==  null 
12+   return  isObject  &&  'code'  in  data  &&  'message'  in  data 
13+ } 
14+ 
1015function  replaceParam ( str : string ,  key : string ,  value : string ) : string  { 
1116  return  str . replace ( new  RegExp ( `\\{${ key }  ,  'g' ) ,  value ) 
1217} 
@@ -51,16 +56,18 @@ export async function fetchData<T>(url: string, body?: unknown): Promise<T> {
5156  } 
5257
5358  const  resp  =  await  fetch ( url ,  options ) 
54-   const  json   =   await   resp . json ( ) 
59+   let  json 
5560
56-   if  ( ! resp . ok )  { 
57-     let  errTxt  =  '' 
58-     try  { 
59-       const  err  =  json  as  ErrorResponse 
60-       errTxt  =  `${ err . code } ${ err . message }  
61-     }  catch  ( e )  { 
62-       errTxt  =  resp . statusText 
61+   try  { 
62+     json  =  await  resp . json ( ) 
63+   }  catch  { 
64+     if  ( resp . headers  &&  resp . headers . get ( 'content-length' )  !==  '0' )  { 
65+       throw  new  Error ( `Invalid response content: ${ resp . statusText }  ) 
6366    } 
67+   } 
68+ 
69+   if  ( ! resp . ok )  { 
70+     const  errTxt  =  isErrorResponse ( json )  ? `${ json . code } ${ json . message }   : resp . statusText 
6471    throw  new  Error ( errTxt ) 
6572  } 
6673
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments