ti2 1.0.57 → 1.0.58
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 +30 -4
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -210,6 +210,10 @@ module.exports = async ({
|
|
|
210
210
|
body,
|
|
211
211
|
);
|
|
212
212
|
const userId = R.pathOr(undefined, ['params', 'userId'], body);
|
|
213
|
+
const plugin = plugins.find(({ name }) => name === pluginName);
|
|
214
|
+
const errorPathsAxiosErrors = R.pathOr(() => [], ['errorPathsAxiosErrors'], plugin)();
|
|
215
|
+
const errorPathsAxiosAny = R.pathOr(() => [], ['errorPathsAxiosAny'], plugin)();
|
|
216
|
+
|
|
213
217
|
axiosPlugin.interceptors.request.use(request => {
|
|
214
218
|
ti2Events.emit(`${pluginName}.axios.request`, { ...axiosSafeRequest(request), userId });
|
|
215
219
|
request.headers.common.requestId = req.requestId;
|
|
@@ -217,20 +221,42 @@ module.exports = async ({
|
|
|
217
221
|
});
|
|
218
222
|
axiosPlugin.interceptors.response.use(response => {
|
|
219
223
|
ti2Events.emit(`${pluginName}.axios.response`, { ...axiosSafeResponse(response), userId });
|
|
224
|
+
if (errorPathsAxiosAny.length > 0) {
|
|
225
|
+
const pathMatch = errorPathsAxiosAny.find(errorPath => R.path(errorPath, response));
|
|
226
|
+
if (pathMatch) {
|
|
227
|
+
const errMsg = R.path(pathMatch, response);
|
|
228
|
+
if (process.env.debug) console.error(`error in ${pluginName}`, errMsg);
|
|
229
|
+
if (ti2Events.events) {
|
|
230
|
+
ti2Events.events.emit(`${pluginName}.axios.error`, {
|
|
231
|
+
response: axiosSafeResponse(response),
|
|
232
|
+
err: errMsg,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
throw new Eror(errMsg);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
220
238
|
return response;
|
|
221
239
|
});
|
|
222
240
|
req.axios = async (...args) => axiosPlugin(...args).catch(err => {
|
|
223
|
-
const errMsg =
|
|
224
|
-
|
|
241
|
+
const errMsg = (() => {
|
|
242
|
+
const defaultErr = R.omit(['config'], err.toJSON()); // default error
|
|
243
|
+
if (errorPathsAxiosErrors.length > 0) {
|
|
244
|
+
const pathMatch = errorPathsAxiosErrors.find(errorPath => R.path(errorPath, err));
|
|
245
|
+
if (pathMatch) {
|
|
246
|
+
return R.path(pathMatch, err);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return defaultErr;
|
|
250
|
+
})();
|
|
251
|
+
if (console.env.debug) console.error(`error in ${pluginName}`, args[0], errMsg);
|
|
225
252
|
if (ti2Events.events) {
|
|
226
253
|
ti2Events.events.emit(`${pluginName}.axios.error`, {
|
|
227
254
|
request: args[0],
|
|
228
255
|
err: errMsg,
|
|
229
256
|
});
|
|
230
257
|
}
|
|
231
|
-
throw
|
|
258
|
+
throw new Error(errMsg);
|
|
232
259
|
});
|
|
233
|
-
req.axios = axiosPlugin;
|
|
234
260
|
|
|
235
261
|
next();
|
|
236
262
|
});
|