Skip to content

Commit

Permalink
Fixing error handling for when the error comes as a successful respon…
Browse files Browse the repository at this point in the history
…se from WinChan
  • Loading branch information
luisrudge committed Mar 21, 2017
1 parent e0f8a2a commit 502dee9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/helper/response-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ function wrapCallback(cb, options) {
data = null;
}

if (!err && data.error) {
err = data;
data = null;
}

if (err) {
errObj = {
original: err
Expand All @@ -39,7 +44,7 @@ function wrapCallback(cb, options) {
}

errObj.code = err.error || err.code || err.error_code || err.status || null;
errObj.description = err.error_description || err.description || err.error || err.details || err.err || null;
errObj.description = err.errorDescription || err.error_description || err.description || err.error || err.details || err.err || null;

if (err.name) {
errObj.name = err.name;
Expand Down
33 changes: 25 additions & 8 deletions test/helper/response-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('helpers responseHandler', function () {

it('should return default error', function (done) {

responseHandler(function(err, data) {
responseHandler(function (err, data) {
expect(data).to.be(undefined);
expect(err).to.eql({
error: 'generic_error',
Expand All @@ -28,7 +28,7 @@ describe('helpers responseHandler', function () {
name: 'SomeName'
};

responseHandler(function(err, data) {
responseHandler(function (err, data) {
expect(data).to.be(undefined);
expect(err).to.eql({
original: assert_err,
Expand All @@ -51,7 +51,7 @@ describe('helpers responseHandler', function () {
description: 'The error description.'
};

responseHandler(function(err, data) {
responseHandler(function (err, data) {
expect(data).to.be(undefined);
expect(err).to.eql({
original: assert_err,
Expand All @@ -68,7 +68,7 @@ describe('helpers responseHandler', function () {
assert_err.response.body = {
};

responseHandler(function(err, data) {
responseHandler(function (err, data) {
expect(data).to.be(undefined);
expect(err).to.eql({
original: assert_err,
Expand All @@ -87,7 +87,7 @@ describe('helpers responseHandler', function () {
error_description: 'The error description.'
};

responseHandler(function(err, data) {
responseHandler(function (err, data) {
expect(data).to.be(undefined);
expect(err).to.eql({
original: assert_err,
Expand All @@ -106,7 +106,7 @@ describe('helpers responseHandler', function () {
err: 'The error description.'
};

responseHandler(function(err, data) {
responseHandler(function (err, data) {
expect(data).to.be(undefined);
expect(err).to.eql({
original: assert_err,
Expand All @@ -117,6 +117,23 @@ describe('helpers responseHandler', function () {
})(assert_err, null);
});

it('should return normalized format 5 (error comes from data)', function (done) {
var assert_err = {
error: 'the_error_code',
errorDescription: 'The error description.'
};

responseHandler(function (err, data) {
expect(data).to.be(undefined);
expect(err).to.eql({
original: assert_err,
code: 'the_error_code',
description: 'The error description.'
});
done();
})(null, assert_err);
});

it('should return the data', function (done) {
var assert_data = {
body: {
Expand All @@ -125,7 +142,7 @@ describe('helpers responseHandler', function () {
}
}

responseHandler(function(err, data) {
responseHandler(function (err, data) {
expect(err).to.be(null);
expect(data).to.eql({
attr1: 'attribute 1',
Expand All @@ -141,7 +158,7 @@ describe('helpers responseHandler', function () {
type: 'text/html'
}

responseHandler(function(err, data) {
responseHandler(function (err, data) {
expect(err).to.be(null);
expect(data).to.eql('The reponse message');
done();
Expand Down

0 comments on commit 502dee9

Please sign in to comment.