@@ -85,6 +85,22 @@ export class URLTokenBaseHTTPClient implements BaseHTTPClient {
8585 return res ;
8686 }
8787
88+ /**
89+ * Make a superagent error more readable. For more info, see https://github.com/visionmedia/superagent/issues/1074
90+ */
91+ private static formatSuperagentError ( err : any ) : Error {
92+ if ( err . response ) {
93+ try {
94+ const decoded = JSON . parse ( Buffer . from ( err . response . body ) . toString ( ) ) ;
95+ // eslint-disable-next-line no-param-reassign
96+ err . message = `Network request error. Received status ${ err . response . status } : ${ decoded . message } ` ;
97+ } catch ( err2 ) {
98+ // ignore any error that happened while we are formatting the original error
99+ }
100+ }
101+ return err ;
102+ }
103+
88104 async get (
89105 relativePath : string ,
90106 query ?: Query < string > ,
@@ -98,8 +114,12 @@ export class URLTokenBaseHTTPClient implements BaseHTTPClient {
98114 . responseType ( 'arraybuffer' )
99115 . query ( query ) ;
100116
101- const res = await r ;
102- return URLTokenBaseHTTPClient . superagentToHTTPClientResponse ( res ) ;
117+ try {
118+ const res = await r ;
119+ return URLTokenBaseHTTPClient . superagentToHTTPClientResponse ( res ) ;
120+ } catch ( err ) {
121+ throw URLTokenBaseHTTPClient . formatSuperagentError ( err ) ;
122+ }
103123 }
104124
105125 async post (
@@ -118,8 +138,12 @@ export class URLTokenBaseHTTPClient implements BaseHTTPClient {
118138 . responseType ( 'arraybuffer' )
119139 . send ( Buffer . from ( data ) ) ; // Buffer.from necessary for superagent
120140
121- const res = await r ;
122- return URLTokenBaseHTTPClient . superagentToHTTPClientResponse ( res ) ;
141+ try {
142+ const res = await r ;
143+ return URLTokenBaseHTTPClient . superagentToHTTPClientResponse ( res ) ;
144+ } catch ( err ) {
145+ throw URLTokenBaseHTTPClient . formatSuperagentError ( err ) ;
146+ }
123147 }
124148
125149 async delete (
@@ -138,7 +162,11 @@ export class URLTokenBaseHTTPClient implements BaseHTTPClient {
138162 . responseType ( 'arraybuffer' )
139163 . send ( Buffer . from ( data ) ) ; // Buffer.from necessary for superagent
140164
141- const res = await r ;
142- return URLTokenBaseHTTPClient . superagentToHTTPClientResponse ( res ) ;
165+ try {
166+ const res = await r ;
167+ return URLTokenBaseHTTPClient . superagentToHTTPClientResponse ( res ) ;
168+ } catch ( err ) {
169+ throw URLTokenBaseHTTPClient . formatSuperagentError ( err ) ;
170+ }
143171 }
144172}
0 commit comments