Skip to content

Commit

Permalink
update buildMessage to include request id and status
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasassisrosa committed Aug 7, 2019
1 parent cdf1a26 commit 6d67e9b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions lib/Error.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ function _Error(raw) {
this.stack = (new Error(this.message)).stack;
}

function buildMessage(errors) {
return `${statusString(errors)}${idString(errors)}${message(errors)}${otherErrorsMessage(errors)}`;
function buildMessage(raw) {
return `${statusString(raw)}${idString(raw)}${message(raw.errors)}${otherErrorsMessage(raw.errors)}`;
}

function otherErrorsMessage(errors) {
var count = errorCount(errors);
if (count > 2) {
return `plus ${count} other errors.`;
return ` plus ${count} other errors.`;
} else if (count == 2) {
return 'plus 1 other error.';
return ' plus 1 other error.';
}

return '';
Expand All @@ -37,23 +37,23 @@ function errorCount(errors) {

function message(errors) {
if (Array.isArray(errors)) {
return errors[0] && (errors[0].title || errors[0].detail) + ' ';
return errors[0] && (` ${errors[0].title || errors[0].detail}`);
} else {
return errors;
return ` ${errors}`;
}
}

function statusString(raw) {
if (raw.statusCode) {
return `(Status ${raw.statusCode}) `;
return `(Status ${raw.statusCode})`;
} else {
return '';
}
}

function idString(raw) {
if (raw.requestId) {
return `(Request ${raw.requestId}) `;
return ` (Request ${raw.requestId})`;
} else {
return '';
}
Expand Down Expand Up @@ -81,7 +81,7 @@ var TelnyxError = _Error.TelnyxError = _Error.extend({
if (raw.message) {
message = raw.message;
} else {
message = buildMessage(raw.errors)
message = buildMessage(raw);
}
// Move from prototype def (so it appears in stringified obj)
this.type = this.type;
Expand Down
4 changes: 2 additions & 2 deletions lib/TelnyxResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ TelnyxResource.prototype = {

// For convenience, make Request-Id easily accessible on
// lastResponse.
res.requestId = headers['request-id'] || '';
res.requestId = headers['request-id'] || headers['x-request-id'] || '';

var requestDurationMs = Date.now() - req._requestStart;

Expand Down Expand Up @@ -158,7 +158,7 @@ TelnyxResource.prototype = {
message: 'Invalid JSON received from the Telnyx API',
response: response,
exception: e,
requestId: headers['request-id'],
requestId: res.requestId,
}),
null
);
Expand Down

0 comments on commit 6d67e9b

Please sign in to comment.