Skip to content

Commit

Permalink
Merge pull request #71 from auth0/connection-error
Browse files Browse the repository at this point in the history
Added Internet connection error reporting
  • Loading branch information
pose committed Mar 19, 2015
2 parents 615b43d + 68aea5d commit 8fa3149
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 41 deletions.
6 changes: 4 additions & 2 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ <h2>Signup Database Connection</h2>
auth0.login({
connection: 'tests',
username: $('.login-username').val(),
password: $('.login-password').val()
password: $('.login-password').val(),
sso: false
}, function (err, profile, id_token, access_token) {
if (err) return alert('Something went wrong: ' + err.message);
alert('hello ' + profile.name);
Expand All @@ -111,7 +112,8 @@ <h2>Signup Database Connection</h2>
auth0.signup({
connection: 'tests',
username: $('.signup-username').val(),
password: $('.signup-password').val()
password: $('.signup-password').val(),
sso: false
}, function (err, profile, id_token, access_token) {
if (err) return alert('Something went wrong: ' + err.message);
alert('hello ' + profile.name);
Expand Down
6 changes: 4 additions & 2 deletions lib/LoginError.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ function LoginError(status, details) {
err.details = obj;

if (status === 0) {
err.code = "Unknown";
err.message = "Unknown error.";
if (!err.code || err.code !== 'offline') {
err.code = 'Unknown';
err.message = 'Unknown error.';
}
}

return err;
Expand Down
80 changes: 43 additions & 37 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var use_jsonp = require('./use_jsonp');
var LoginError = require('./LoginError');
var json_parse = require('./json-parse');


/**
* Stringify popup options object into
* `window.open` string options format
Expand Down Expand Up @@ -55,6 +56,33 @@ function checkIfSet(obj, key) {
return !!(obj && obj[key] != null);
}

function handleRequestError(err, callback) {
var er = err;

var onLine = !!window.navigator.onLine;

// http://stackoverflow.com/questions/23229723/ie-10-11-cors-status-0
// XXX IE10 when a request fails in CORS returns status code 0
// See: http://caniuse.com/#search=navigator.onLine
if ((!er.status || er.status === 0) && !onLine ) {
er = {};
er.status = 0;
er.responseText = {
code: 'offline'
};
} else if (er.status === 0) {
er = {};
er.status = 401;
er.responseText = {
code: 'invalid_user_password'
};
} else {
er.responseText = err;
}
var error = new LoginError(er.status, er.responseText);
callback(error);
}

/**
* Create an `Auth0` instance with `options`
*
Expand Down Expand Up @@ -958,19 +986,7 @@ Auth0.prototype.loginWithResourceOwner = function (options, callback) {
enrichGetProfile(resp, callback);
},
error: function (err) {
var er = err;
if (!er.status || er.status === 0) { //ie10 trick
er = {};
er.status = 401;
er.responseText = {
code: 'invalid_user_password'
};
}
else {
er.responseText = err;
}
var error = new LoginError(er.status, er.responseText);
callback(error);
handleRequestError(err, callback);
}
});
};
Expand Down Expand Up @@ -1022,19 +1038,7 @@ Auth0.prototype.loginWithSocialAccessToken = function (options, callback) {
enrichGetProfile(resp, callback);
},
error: function (err) {
var er = err;
if (!er.status || er.status === 0) { //ie10 trick
er = {};
er.status = 401;
er.responseText = {
code: 'invalid_request'
};
}
else {
er.responseText = err;
}
var error = new LoginError(er.status, er.responseText);
callback(error);
handleRequestError(err, callback);
}
});
};
Expand Down Expand Up @@ -1159,19 +1163,10 @@ Auth0.prototype.loginWithUsernamePassword = function (options, callback) {
self._renderAndSubmitWSFedForm(options, resp);
},
error: function (err) {
var er = err;
if (popup && popup.kill) {
popup.kill();
}
if (!er.status || er.status === 0) { //ie10 trick
er = {};
er.status = 401;
er.responseText = {
code: 'invalid_user_password'
};
}
var error = new LoginError(er.status, er.responseText);
return return_error(error);
handleRequestError(err, return_error);
}
});
};
Expand Down Expand Up @@ -1277,12 +1272,23 @@ Auth0.prototype.getDelegationToken = function (options, callback) {
}
catch (e) {
var er = err;
if (!er.status || er.status === 0) { //ie10 trick
// http://stackoverflow.com/questions/23229723/ie-10-11-cors-status-0
// XXX IE10 when a request fails in CORS returns status code 0
// XXX This is not handled by handleRequestError as the errors are different
// See: http://caniuse.com/#search=navigator.onLine
if ((!er.status || er.status === 0) && window.navigator.onLine) {
er = {};
er.status = 401;
er.responseText = {
code: 'invalid_operation'
};
} else {
er = {};
er.status = 0;
er.responseText = {
code: 'offline'
};

}
callback(new LoginError(er.status, er.responseText));
}
Expand Down

0 comments on commit 8fa3149

Please sign in to comment.