Skip to content

Commit

Permalink
feat:adds details to rest exception (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashish-s authored Apr 27, 2020
1 parent 4097475 commit 377f7c3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
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;
}
}

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(undefined);
});

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);
});
});

0 comments on commit 377f7c3

Please sign in to comment.