ti2 1.0.57 → 1.0.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js
CHANGED
|
@@ -15,10 +15,6 @@ const EventEmitter = require('eventemitter2');
|
|
|
15
15
|
const axios = require('axios');
|
|
16
16
|
const curlirize = require('axios-curlirize');
|
|
17
17
|
|
|
18
|
-
if (process.env.debug) {
|
|
19
|
-
curlirize(axios);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
18
|
const cacheSettings = {
|
|
23
19
|
'*': [
|
|
24
20
|
'getAffiliateAgents',
|
|
@@ -210,6 +206,10 @@ module.exports = async ({
|
|
|
210
206
|
body,
|
|
211
207
|
);
|
|
212
208
|
const userId = R.pathOr(undefined, ['params', 'userId'], body);
|
|
209
|
+
const plugin = plugins.find(({ name }) => name === pluginName);
|
|
210
|
+
const errorPathsAxiosErrors = R.pathOr(() => [], ['errorPathsAxiosErrors'], plugin)();
|
|
211
|
+
const errorPathsAxiosAny = R.pathOr(() => [], ['errorPathsAxiosAny'], plugin)();
|
|
212
|
+
|
|
213
213
|
axiosPlugin.interceptors.request.use(request => {
|
|
214
214
|
ti2Events.emit(`${pluginName}.axios.request`, { ...axiosSafeRequest(request), userId });
|
|
215
215
|
request.headers.common.requestId = req.requestId;
|
|
@@ -217,20 +217,45 @@ module.exports = async ({
|
|
|
217
217
|
});
|
|
218
218
|
axiosPlugin.interceptors.response.use(response => {
|
|
219
219
|
ti2Events.emit(`${pluginName}.axios.response`, { ...axiosSafeResponse(response), userId });
|
|
220
|
+
if (errorPathsAxiosAny.length > 0) {
|
|
221
|
+
const pathMatch = errorPathsAxiosAny.find(errorPath => R.path(errorPath, response));
|
|
222
|
+
if (pathMatch) {
|
|
223
|
+
const errMsg = R.path(pathMatch, response);
|
|
224
|
+
if (process.env.debug) console.error(`error in ${pluginName}`, errMsg);
|
|
225
|
+
if (ti2Events.events) {
|
|
226
|
+
ti2Events.events.emit(`${pluginName}.axios.error`, {
|
|
227
|
+
response: axiosSafeResponse(response),
|
|
228
|
+
err: errMsg,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
throw new Eror(errMsg);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
220
234
|
return response;
|
|
221
235
|
});
|
|
236
|
+
if (process.env.debug) {
|
|
237
|
+
curlirize(axiosPlugin);
|
|
238
|
+
}
|
|
222
239
|
req.axios = async (...args) => axiosPlugin(...args).catch(err => {
|
|
223
|
-
const errMsg =
|
|
224
|
-
|
|
240
|
+
const errMsg = (() => {
|
|
241
|
+
const defaultErr = R.omit(['config'], err.toJSON()); // default error
|
|
242
|
+
if (errorPathsAxiosErrors.length > 0) {
|
|
243
|
+
const pathMatch = errorPathsAxiosErrors.find(errorPath => R.path(errorPath, err));
|
|
244
|
+
if (pathMatch) {
|
|
245
|
+
return R.path(pathMatch, err);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return defaultErr;
|
|
249
|
+
})();
|
|
250
|
+
if (process.env.debug) console.error(`error in ${pluginName}`, args[0], errMsg);
|
|
225
251
|
if (ti2Events.events) {
|
|
226
252
|
ti2Events.events.emit(`${pluginName}.axios.error`, {
|
|
227
253
|
request: args[0],
|
|
228
254
|
err: errMsg,
|
|
229
255
|
});
|
|
230
256
|
}
|
|
231
|
-
throw
|
|
257
|
+
throw new Error(errMsg);
|
|
232
258
|
});
|
|
233
|
-
req.axios = axiosPlugin;
|
|
234
259
|
|
|
235
260
|
next();
|
|
236
261
|
});
|
|
@@ -284,7 +309,7 @@ module.exports = async ({
|
|
|
284
309
|
return next(err);
|
|
285
310
|
}
|
|
286
311
|
if (process.env.CONSOLE_ERRORS || process.env.JEST_WORKER_ID) {
|
|
287
|
-
console.error(R.path(['response', 'data'], err)
|
|
312
|
+
console.error(R.path(['response', 'data'], err));
|
|
288
313
|
}
|
|
289
314
|
return res.status((() => {
|
|
290
315
|
if (!isNumber(err.status)) return 500;
|