@@ -28,15 +28,15 @@ const localLogger = {
28
28
info : ( message , context ) => logger . info ( { component : 'SurveyMonkeyAPI' , context, message } )
29
29
}
30
30
31
- function getRemainingRequestCountMessge ( response ) {
31
+ function getRemainingRequestCountMessage ( response ) {
32
32
return `today has sent ${ response . header [ 'x-ratelimit-app-global-day-limit' ] - response . header [ 'x-ratelimit-app-global-day-remaining' ] } requests`
33
33
}
34
34
35
- function getErrorMessage ( e ) {
36
- return {
37
- errorCode : _ . get ( e , 'response.body.error.http_status_code ' , 400 ) ,
38
- errorMessage : _ . get ( e , 'response.body.error.message' , 'error message' )
39
- }
35
+ function enrichErrorMessage ( e ) {
36
+ e . code = _ . get ( e , 'response.body.error.http_status_code' )
37
+ e . message = _ . get ( e , 'response.body.error.message ' , e . toString ( ) )
38
+
39
+ return e
40
40
}
41
41
42
42
function getSingleItem ( lst , errorMessage ) {
@@ -72,12 +72,13 @@ async function searchCollector (collectorName) {
72
72
. set ( 'Content-Type' , 'application/json' )
73
73
. set ( 'Accept' , 'application/json' )
74
74
75
- localLogger . info ( `URL ${ url } , ${ getRemainingRequestCountMessge ( response ) } ` , 'searchCollector' )
75
+ localLogger . info ( `URL ${ url } , ${ getRemainingRequestCountMessage ( response ) } ` , 'searchCollector' )
76
76
77
77
return getSingleItem ( response . body . data , 'More than 1 collector found by name ' + collectorName )
78
78
} catch ( e ) {
79
- localLogger . error ( `URL ${ url } ${ getErrorMessage ( e ) } , ${ getRemainingRequestCountMessge ( e . response ) } ` , 'searchCollector' )
80
- throw getErrorMessage ( e )
79
+ const enrichedError = enrichErrorMessage ( e )
80
+ localLogger . error ( `URL ${ url } ERROR ${ enrichedError } , ${ getRemainingRequestCountMessage ( e . response ) } ` , 'searchCollector' )
81
+ throw enrichedError
81
82
}
82
83
}
83
84
@@ -110,11 +111,12 @@ async function cloneCollector () {
110
111
. set ( 'Content-Type' , 'application/json' )
111
112
. set ( 'Accept' , 'application/json' )
112
113
. send ( body )
113
- localLogger . info ( `URL ${ url } , ${ getRemainingRequestCountMessge ( response ) } ` , 'cloneCollector' )
114
+ localLogger . info ( `URL ${ url } , ${ getRemainingRequestCountMessage ( response ) } ` , 'cloneCollector' )
114
115
return response . body . id
115
116
} catch ( e ) {
116
- localLogger . error ( `URL ${ url } ${ JSON . stringify ( getErrorMessage ( e ) ) } , ${ getRemainingRequestCountMessge ( e . response ) } ` , 'cloneCollector' )
117
- throw getErrorMessage ( e )
117
+ const enrichedError = enrichErrorMessage ( e )
118
+ localLogger . error ( `URL ${ url } ERROR ${ enrichedError } , ${ getRemainingRequestCountMessage ( e . response ) } ` , 'cloneCollector' )
119
+ throw enrichedError
118
120
}
119
121
}
120
122
@@ -131,10 +133,11 @@ async function renameCollector (collectorId, name) {
131
133
. set ( 'Content-Type' , 'application/json' )
132
134
. set ( 'Accept' , 'application/json' )
133
135
. send ( body )
134
- localLogger . info ( `URL ${ url } , ${ getRemainingRequestCountMessge ( response ) } ` , 'renameCollector' )
136
+ localLogger . info ( `URL ${ url } , ${ getRemainingRequestCountMessage ( response ) } ` , 'renameCollector' )
135
137
} catch ( e ) {
136
- localLogger . error ( `URL ${ url } ${ JSON . stringify ( getErrorMessage ( e ) ) } , ${ getRemainingRequestCountMessge ( e . response ) } ` , 'renameCollector' )
137
- throw getErrorMessage ( e )
138
+ const enrichedError = enrichErrorMessage ( e )
139
+ localLogger . error ( `URL ${ url } ERROR ${ enrichedError } , ${ getRemainingRequestCountMessage ( e . response ) } ` , 'renameCollector' )
140
+ throw enrichedError
138
141
}
139
142
}
140
143
@@ -154,11 +157,12 @@ async function createMessage (collectorId) {
154
157
. set ( 'Content-Type' , 'application/json' )
155
158
. set ( 'Accept' , 'application/json' )
156
159
. send ( body )
157
- localLogger . info ( `URL ${ url } , ${ getRemainingRequestCountMessge ( response ) } ` , 'createMessage' )
160
+ localLogger . info ( `URL ${ url } , ${ getRemainingRequestCountMessage ( response ) } ` , 'createMessage' )
158
161
return response . body . id
159
162
} catch ( e ) {
160
- localLogger . error ( `URL ${ url } ${ JSON . stringify ( getErrorMessage ( e ) ) } , ${ getRemainingRequestCountMessge ( e . response ) } ` , 'createMessage' )
161
- throw getErrorMessage ( e )
163
+ const enrichedError = enrichErrorMessage ( e )
164
+ localLogger . error ( `URL ${ url } ERROR ${ enrichedError } , ${ getRemainingRequestCountMessage ( e . response ) } ` , 'createMessage' )
165
+ throw enrichedError
162
166
}
163
167
}
164
168
@@ -182,11 +186,12 @@ async function upsertContactInSurveyMonkey (list) {
182
186
. set ( 'Accept' , 'application/json' )
183
187
. send ( body )
184
188
185
- localLogger . info ( `URL ${ url } , ${ getRemainingRequestCountMessge ( response ) } ` , 'upsertContactInSurveyMonkey' )
189
+ localLogger . info ( `URL ${ url } , ${ getRemainingRequestCountMessage ( response ) } ` , 'upsertContactInSurveyMonkey' )
186
190
return _ . concat ( response . body . existing , response . body . succeeded )
187
191
} catch ( e ) {
188
- localLogger . error ( `URL ${ url } ${ JSON . stringify ( getErrorMessage ( e ) ) } , ${ getRemainingRequestCountMessge ( e . response ) } ` , 'createMessage' )
189
- throw getErrorMessage ( e )
192
+ const enrichedError = enrichErrorMessage ( e )
193
+ localLogger . error ( `URL ${ url } ERROR ${ enrichedError } , ${ getRemainingRequestCountMessage ( e . response ) } ` , 'createMessage' )
194
+ throw enrichedError
190
195
}
191
196
}
192
197
@@ -200,11 +205,12 @@ async function addContactsToSurvey (collectorId, messageId, contactIds) {
200
205
. set ( 'Content-Type' , 'application/json' )
201
206
. set ( 'Accept' , 'application/json' )
202
207
. send ( body )
203
- localLogger . info ( `URL ${ url } , ${ getRemainingRequestCountMessge ( response ) } ` , 'addContactsToSurvey' )
208
+ localLogger . info ( `URL ${ url } , ${ getRemainingRequestCountMessage ( response ) } ` , 'addContactsToSurvey' )
204
209
return response . body . id
205
210
} catch ( e ) {
206
- localLogger . error ( `URL ${ url } ${ JSON . stringify ( getErrorMessage ( e ) ) } , ${ getRemainingRequestCountMessge ( e . response ) } ` , 'addContactsToSurvey' )
207
- throw getErrorMessage ( e )
211
+ const enrichedError = enrichErrorMessage ( e )
212
+ localLogger . error ( `URL ${ url } ERROR ${ enrichedError } , ${ getRemainingRequestCountMessage ( e . response ) } ` , 'addContactsToSurvey' )
213
+ throw enrichedError
208
214
}
209
215
}
210
216
@@ -217,11 +223,12 @@ async function sendSurveyAPI (collectorId, messageId) {
217
223
. set ( 'Content-Type' , 'application/json' )
218
224
. set ( 'Accept' , 'application/json' )
219
225
. send ( { } )
220
- localLogger . info ( `URL ${ url } , ${ getRemainingRequestCountMessge ( response ) } ` , 'sendSurveyAPI' )
226
+ localLogger . info ( `URL ${ url } , ${ getRemainingRequestCountMessage ( response ) } ` , 'sendSurveyAPI' )
221
227
return response . body . id
222
228
} catch ( e ) {
223
- localLogger . error ( `URL ${ url } ${ JSON . stringify ( getErrorMessage ( e ) ) } , ${ getRemainingRequestCountMessge ( e . response ) } ` , 'sendSurveyAPI' )
224
- throw getErrorMessage ( e )
229
+ const enrichedError = enrichErrorMessage ( e )
230
+ localLogger . error ( `URL ${ url } ${ enrichedError } , ${ getRemainingRequestCountMessage ( e . response ) } ` , 'sendSurveyAPI' )
231
+ throw enrichedError
225
232
}
226
233
}
227
234
0 commit comments