Repository: auth0/auth0.js
Pull request #395 (2017-03-21 16:35:33)

Fixing error handling for when the error comes as a successful response from WinChan
fix https://github.com/auth0/lock/issues/938


Patch:
diff --git a/src/helper/response-handler.js b/src/helper/response-handler.js
index 7862add..c532291 100644
--- a/src/helper/response-handler.js
+++ b/src/helper/response-handler.js
@@ -17,6 +17,11 @@ function wrapCallback(cb, options) {
       data = null;
     }
 
+    if (!err && data.error) {
+      err = data;
+      data = null;
+    }
+
     if (err) {
       errObj = {
         original: err
@@ -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;


Test patch:
diff --git a/test/helper/response-handler.test.js b/test/helper/response-handler.test.js
index 575b721..0cbed9a 100644
--- a/test/helper/response-handler.test.js
+++ b/test/helper/response-handler.test.js
@@ -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',
@@ -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,
@@ -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,
@@ -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,
@@ -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,
@@ -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,
@@ -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: {
@@ -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',
@@ -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();