Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:adds details to rest exception #565

Merged
merged 3 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/base/RestException.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare class RestException extends Error {
message: string;
code: number;
moreInfo: string;
detail: string;
details: object;
}

export = RestException;
export = RestException;
4 changes: 2 additions & 2 deletions lib/base/RestException.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ class RestException extends Error {
constructor(response) {
super('[HTTP ' + response.statusCode + '] Failed to execute request');

const body = response.body === 'string' ? JSON.parse(response.body) : response.body;
const body = typeof response.body === 'string' ? JSON.parse(response.body) : response.body;
this.status = response.statusCode;
this.message = body.message;
this.code = body.code;
this.moreInfo = body.more_info; /* jshint ignore:line */
this.detail = body.detail;
this.details = body.details === undefined ? null : body.details;
childish-sambino marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
55 changes: 55 additions & 0 deletions spec/unit/base/RestException.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var RestException = require('../../../lib/base/RestException');

describe('exception gets created from string', function () {
it('should test serialize without details', function () {
const response = {
statusCode: 200,
body: '{"message":"test", "code":81022,"more_info": "https://www.twilio.com/docs/errors/81022"}',
};
const exception = new RestException(response);
expect(exception.status).toEqual(200);
expect(exception.message).toEqual('test');
expect(exception.code).toEqual(81022);
expect(exception.moreInfo).toEqual('https://www.twilio.com/docs/errors/81022');
});
});

describe('exception gets created from json error', function () {

it('should create exception without details', function () {
const response = {
statusCode: 200,
body: {
message: 'test',
code: 81022,
more_info: 'https://www.twilio.com/docs/errors/81022',
},
};

var exception = new RestException(response);
expect(exception.status).toEqual(200);
expect(exception.message).toEqual('test');
expect(exception.code).toEqual(81022);
expect(exception.moreInfo).toEqual('https://www.twilio.com/docs/errors/81022');
expect(exception.details).toEqual(null);
});

it('should create exception with details', function () {
const details = {
foo: 'bar',
};

const response = {
statusCode: 200,
body: {
message: 'test',
code: 81022,
more_info: 'https://www.twilio.com/docs/errors/81022',
details: details,
},
};

const exception = new RestException(response);
expect(exception.details).toEqual(details);
});
});