Skip to content

Commit

Permalink
Merge 6d67e9b into c526ad4
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasassisrosa committed Aug 7, 2019
2 parents c526ad4 + 6d67e9b commit 618c603
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
20 changes: 11 additions & 9 deletions lib/Error.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ 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 '';
}

function errorCount(errors) {
Expand All @@ -35,23 +37,23 @@ function errorCount(errors) {

function message(errors) {
if (Array.isArray(errors)) {
return errors[0] && errors[0].title + ' ';
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 @@ -79,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 618c603

Please sign in to comment.