Skip to content

Commit

Permalink
feat(error-message): Make error message on proxy call to be clearer
Browse files Browse the repository at this point in the history
- Remove 'Unknown error' as generic message for other uncategorised error
- Add more descriptive message and its error object to clarify it
  • Loading branch information
mamaz authored Sep 23, 2020
2 parents 1cea3d6 + f91e6fa commit e16b776
Showing 7 changed files with 288 additions and 281 deletions.
45 changes: 27 additions & 18 deletions lib/api_async.js
Original file line number Diff line number Diff line change
@@ -2,14 +2,13 @@

"use strict";
const Promise = require("bluebird");
const camelCase = require("to-camel-case");
const snakeCase = require("to-snake-case");
const request = require("request-promise");
const sleep = require("sleep-promise");
const { convertToString } = require("./print_util");

module.exports = function(info, options, logger) {
return Promise.coroutine(function*(methodName, args) {
let ret = null;
let ret;
let uri =
(info.secure ? "https://" : "http://") +
info.uri +
@@ -18,26 +17,36 @@ module.exports = function(info, options, logger) {
"/" +
snakeCase(methodName);

const requestParams = {
uri: uri,
method: "POST",
headers: {
Authorization: options.secret ? "Bearer " + options.secret : "",
},
body: { params: args, source: options.source },
json: true,
timeout: options.timeout,
socketTimeout: options.timeout,
};

try {
ret = yield request({
uri: uri,
method: "POST",
headers: {
Authorization: options.secret ? "Bearer " + options.secret : "",
},
body: { params: args, source: options.source },
json: true,
timeout: options.timeout,
socketTimeout: options.timeout,
});
ret = yield request(requestParams);
if (ret && ret.status === "ok") return ret.result;
} catch (e) {
logger.warn(`Error at proxy call:`, e);
// handles error on request-promise related
const errMessage = `Caught uncategorised error at proxy call, request params ${convertToString(requestParams)}, error: ${e}`;
logger.warn(errMessage);
throw new Error(errMessage);
}

if (ret && ret.status == "ok") {
return ret.result;
// handles error comes from the other services
let errMsg = "";
if (ret && ret.error) {
errMsg = `Error at proxy call ${convertToString(ret.error)}, with result: ${convertToString(ret)}, request params: ${convertToString(requestParams)}`;
} else {
throw new Error(ret ? ret.error : "Unknown error");
errMsg = `Error with unknown reason at proxy call, with result: ${convertToString(ret)}, request params: ${convertToString(requestParams)}`
}
logger.warn(errMsg);
throw new Error(errMsg);
});
};
32 changes: 32 additions & 0 deletions lib/print_util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Return toString for any given value
* @param {*} value any value, can be array, object, number, string, undefined, or null
*/
const convertToString = (value) => {
if (value === null) return 'null';

if(typeof value !== 'object')
return String(value);

if (Array.isArray(value)) {
if (value.length == 0) return "[]";

let res = "[";
for (const val of value) {
if (typeof val === 'number') {
res += (String(val) + ",")
} else if (typeof val === 'object'){
res += `${convertToString(val)},`;
} else {
res += `"${convertToString(val)}",`;
}
}
res = res.slice(0, res.length-1);
res += "]"
return res;
}

return JSON.stringify(value);
}

module.exports = { convertToString };
3 changes: 1 addition & 2 deletions lib/proxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const camelCase = require("to-camel-case");
const snakeCase = require("to-snake-case");

function defaults(info, options, logger) {
logger = logger || console;
@@ -23,6 +22,6 @@ module.exports = function(impl, info, options, logger, proxy) {
let methodName = camelCase(info.api[options.version][i]);
proxy[methodName] = (...args) => callApi(methodName, args);
}

return proxy;
};
Loading

0 comments on commit e16b776

Please sign in to comment.