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.
Files changed (2) hide show
  1. package/index.js +30 -4
  2. 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 = R.omit(['config'], err.toJSON());
224
- console.log(`error in ${pluginName}`, args[0], errMsg);
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 R.pathOr(err, ['response', 'data', 'errorMessage'], err);
258
+ throw new Error(errMsg);
232
259
  });
233
- req.axios = axiosPlugin;
234
260
 
235
261
  next();
236
262
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ti2",
3
- "version": "1.0.57",
3
+ "version": "1.0.58",
4
4
  "description": "Tourist Industry Exchange (TI2)",
5
5
  "main": "index.js",
6
6
  "scripts": {