@@ -343,7 +343,48 @@ class Connection {
343
343
break ;
344
344
}
345
345
346
- return axios ( options ) ;
346
+ return axios ( options ) . catch ( error => {
347
+ return new Promise ( ( resolve , reject ) => {
348
+ if ( error . response !== null && typeof error . response === 'object' && error . response . data !== null && typeof error . response . data === 'object' && typeof error . response . data . type === 'string' && error . response . data . type . indexOf ( '/json' ) !== - 1 ) {
349
+ // JSON error responses are Blobs and streams if responseType is set as such, so convert to JSON if required.
350
+ // See: https://github.com/axios/axios/issues/815
351
+ try {
352
+ switch ( options . responseType ) {
353
+ case 'blob' :
354
+ const fileReader = new FileReader ( ) ;
355
+ fileReader . onerror = ( ) => {
356
+ fileReader . abort ( ) ;
357
+ reject ( error ) ;
358
+ } ;
359
+ fileReader . onload = ( ) => {
360
+ reject ( JSON . parse ( fileReader . result ) ) ;
361
+ } ;
362
+ fileReader . readAsText ( error . response . data ) ;
363
+ break ;
364
+ case 'stream' :
365
+ const chunks = "" ;
366
+ error . response . data . on ( "data" , chunk => {
367
+ chunks . push ( chunk ) ;
368
+ } ) ;
369
+ readStream . on ( "error" , ( ) => {
370
+ reject ( error ) ;
371
+ } ) ;
372
+ readStream . on ( "end" , ( ) => {
373
+ reject ( JSON . parse ( Buffer . concat ( chunks ) . toString ( ) ) ) ;
374
+ } ) ;
375
+ break ;
376
+ default :
377
+ reject ( error ) ;
378
+ }
379
+ } catch ( exception ) {
380
+ reject ( error ) ;
381
+ }
382
+ }
383
+ else {
384
+ reject ( error ) ;
385
+ }
386
+ } ) ;
387
+ } ) ;
347
388
}
348
389
349
390
_resetAuth ( ) {
@@ -755,7 +796,7 @@ class File extends BaseEntity {
755
796
}
756
797
} ;
757
798
if ( typeof statusCallback === 'function' ) {
758
- options . onUploadProgress = function ( progressEvent ) {
799
+ options . onUploadProgress = ( progressEvent ) => {
759
800
var percentCompleted = Math . round ( ( progressEvent . loaded * 100 ) / progressEvent . total ) ;
760
801
statusCallback ( percentCompleted ) ;
761
802
} ;
0 commit comments