playov2-js-utilities 0.3.45 → 0.3.47

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/lib/index.js CHANGED
@@ -12,5 +12,6 @@ module.exports = {
12
12
  Cron: require('./cron'),
13
13
  MessagePublisher: require('./message_publisher'),
14
14
  middleware: require('./middleware'),
15
- profanityFilter: require("./profanityFilter/profanityFilter")
15
+ profanityFilter: require("./profanityFilter/profanityFilter"),
16
+ playoResponseHandler: require("./responseHandler/responseHandler")
16
17
  };
@@ -0,0 +1,53 @@
1
+ const Logger = require("../logger");
2
+
3
+ /**
4
+ * Axios response handler
5
+ * @param {Response} resp
6
+ * resp structure |
7
+ * on success {
8
+ * status: HTTP status code returned by the service
9
+ * data,
10
+ * config: request config
11
+ * }
12
+ * on fail : {
13
+ * code: error code | http errorcode like ECONNREFUSED, ETIMEOUT etc
14
+ * message: error message | description of the error code
15
+ * config: request config
16
+ * }
17
+ * @param {String} requestId
18
+ * @returns
19
+ */
20
+ const responseHandler = (resp, requestId) => {
21
+ if (resp.isAxiosError) {
22
+ //if axios error is triggered then axios throws HTTP code for timeout or other forms of unsuccessful errors
23
+ const {
24
+ code = "UNCAUGHT_ERROR",
25
+ message = "encountered uncaught error",
26
+ config = { url: '' },
27
+ response = { data: { requestStatus: 0 } }
28
+ } = resp;
29
+ const { url } = config;
30
+ const { data } = response;
31
+ data["message"] = message;
32
+ Logger.prepareErrorLog(requestId, { code, message }, `Api call to url to ${url} failed`)
33
+ throw (data);
34
+ }
35
+
36
+ const { status, data, config = { url: '' } } = resp;
37
+ const { url = '' } = config;
38
+ if (status === 200) {
39
+ const { requestStatus, message = 'success' } = data;
40
+ if (!requestStatus && requestStatus != 0) { //if the service does not provide request status
41
+ data["requestStatus"] = 1;
42
+ }
43
+ Logger.prepareInfoLog(requestId, {requestStatus: data.requestStatus, message}, `Api call to url to ${url} succeeded with status ${status}`)
44
+ return data
45
+ }
46
+
47
+ else { // ideally this should not get triggered unless there is fault in calling lib itself
48
+ Logger.prepareErrorLog(requestId, err.stack, "axios error");
49
+ throw ({ requestStatus: 0, message: "Failed to handle response" })
50
+ }
51
+ }
52
+
53
+ module.exports = { responseHandler }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playov2-js-utilities",
3
- "version": "0.3.45",
3
+ "version": "0.3.47",
4
4
  "description": "Private package for JS utility functions",
5
5
  "main": "index.js",
6
6
  "scripts": {