11'use strict' ;
22
3+ // Helper to normalize axios error/response for consistent error handling
4+ function normalizeAxiosError ( resp ) {
5+ if ( ! resp ) return { statusText : 'Axios Error' , error : 'Unknown error' } ;
6+ if ( resp . status === 200 ) return null ;
7+ if ( typeof resp . data === 'string' ) {
8+ return { statusText : resp . statusText || 'Axios Error' , error : resp . data } ;
9+ }
10+ if ( typeof resp . data === 'object' && resp . data !== null ) {
11+ return Object . assign ( { } , resp . data , { statusText : resp . statusText || 'Axios Error' } ) ;
12+ }
13+ return { statusText : resp . statusText || 'Axios Error' , error : resp . data } ;
14+ }
15+
316const axios = require ( 'axios' ) ;
417const FormData = require ( 'form-data' ) ;
518
@@ -21,44 +34,57 @@ class RollbarAPI {
2134
2235 async deploy ( request , deployId ) {
2336 let resp ;
24- if ( deployId ) {
25- output . verbose ( '' , 'Update to an existing deploy with deploy_id: ' + deployId ) ;
26- resp = await this . axios . patch ( '/deploy/' + deployId , request ) ;
27- } else {
28- output . verbose ( '' , 'deploy_id not present so likely a new deploy' ) ;
29- resp = await this . axios . post ( '/deploy' , request ) ;
30- }
37+ try {
38+ if ( deployId ) {
39+ output . verbose ( '' , 'Update to an existing deploy with deploy_id: ' + deployId ) ;
40+ resp = await this . axios . patch ( '/deploy/' + deployId , request ) ;
41+ } else {
42+ output . verbose ( '' , 'deploy_id not present so likely a new deploy' ) ;
43+ resp = await this . axios . post ( '/deploy' , request ) ;
44+ }
3145
32- // Output deploy-id
33- if ( resp . status === 200 ) {
34- output . success ( '' , resp . data . data ) ;
46+ // Output deploy-id
47+ if ( resp . status === 200 ) {
48+ output . success ( '' , resp . data . data ) ;
49+ }
50+ return this . processResponse ( resp ) ;
51+ } catch ( error ) {
52+ output . verbose ( '' , 'axios threw error:' , error ) ;
53+ return this . processResponse ( error . response || { data : error . message , status : error . status || 500 , statusText : error . statusText || 'Axios Error' } ) ;
3554 }
36- return this . processResponse ( resp ) ;
3755 }
3856
3957 async sigendURLsourcemaps ( request ) {
40-
41- const resp = await this . axios . post (
42- '/signed_url/sourcemap_bundle' , { version : request . version , prefix_url : request . baseUrl }
43- ) ;
44- return this . processSignedURLResponse ( resp ) ;
58+ try {
59+ const resp = await this . axios . post (
60+ '/signed_url/sourcemap_bundle' , { version : request . version , prefix_url : request . baseUrl }
61+ ) ;
62+ return this . processSignedURLResponse ( resp ) ;
63+ } catch ( error ) {
64+ output . verbose ( '' , 'axios threw error:' , error ) ;
65+ return this . processSignedURLResponse ( error . response || { data : error . message , status : error . status || 500 , statusText : error . statusText || 'Axios Error' } ) ;
66+ }
4567 }
4668
4769 async sourcemaps ( request ) {
4870 output . verbose ( '' , 'minified_url: ' + request . minified_url ) ;
4971
5072 const form = this . convertRequestToForm ( request ) ;
51- const resp = await this . axios . post (
52- '/sourcemap' ,
53- form . getBuffer ( ) , // use buffer to prevent unwanted string escaping.
54- { headers : {
55- // axios needs some help with headers for form data.
56- 'Content-Type' : `multipart/form-data; boundary=${ form . getBoundary ( ) } ` ,
57- 'Content-Length' : form . getLengthSync ( )
58- } }
59- ) ;
60-
61- return this . processResponse ( resp ) ;
73+ try {
74+ const resp = await this . axios . post (
75+ '/sourcemap' ,
76+ form . getBuffer ( ) , // use buffer to prevent unwanted string escaping.
77+ { headers : {
78+ // axios needs some help with headers for form data.
79+ 'Content-Type' : `multipart/form-data; boundary=${ form . getBoundary ( ) } ` ,
80+ 'Content-Length' : form . getLengthSync ( )
81+ } }
82+ ) ;
83+ return this . processResponse ( resp ) ;
84+ } catch ( error ) {
85+ output . verbose ( '' , 'axios threw error:' , error ) ;
86+ return this . processResponse ( error . response || { data : error . message , status : error . status || 500 , statusText : error . statusText || 'Axios Error' } ) ;
87+ }
6288 }
6389
6490 convertRequestToForm ( request ) {
@@ -80,15 +106,15 @@ class RollbarAPI {
80106
81107 processSignedURLResponse ( resp ) {
82108 output . verbose ( '' , 'response:' , resp . data , resp . status , resp . statusText ) ;
83- return resp . data ;
109+ if ( resp . status === 200 ) {
110+ return resp . data ;
111+ }
112+ return normalizeAxiosError ( resp ) ;
84113 }
85114
86115 processResponse ( resp ) {
87116 output . verbose ( '' , 'response:' , resp . data , resp . status , resp . statusText ) ;
88- if ( resp . status === 200 ) {
89- return null ;
90- }
91- return resp . data ;
117+ return normalizeAxiosError ( resp ) ;
92118 }
93119}
94120
0 commit comments