Fix unhandled promise rejection warnings when access token refreshing fails

This commit is contained in:
Thomas Goyne 2017-08-31 10:39:48 -07:00
parent 9e4313c416
commit 6d5fec23e2
1 changed files with 12 additions and 11 deletions

View File

@ -111,16 +111,17 @@ function refreshAccessToken(user, localRealmPath, realmUrl) {
print_error(`Unhandled session token refresh error: could not look up session at path ${localRealmPath}`); print_error(`Unhandled session token refresh error: could not look up session at path ${localRealmPath}`);
} }
} }
}); })
.catch((e) => print_error(e));
} }
/** /**
* The base authentication method. It fires a JSON POST to the server parameter plus the auth url * The base authentication method. It fires a JSON POST to the server parameter plus the auth url
* For example, if the server parameter is `http://myapp.com`, this url will post to `http://myapp.com/auth` * For example, if the server parameter is `http://myapp.com`, this url will post to `http://myapp.com/auth`
* @param {object} userConstructor * @param {object} userConstructor
* @param {string} server the http or https server url * @param {string} server the http or https server url
* @param {object} json the json to post to the auth endpoint * @param {object} json the json to post to the auth endpoint
* @param {Function} callback an optional callback with an error and user parameter * @param {Function} callback an optional callback with an error and user parameter
* @returns {Promise} only returns a promise if the callback parameter was omitted * @returns {Promise} only returns a promise if the callback parameter was omitted
*/ */
function _authenticate(userConstructor, server, json, callback) { function _authenticate(userConstructor, server, json, callback) {
@ -149,8 +150,8 @@ function _authenticate(userConstructor, server, json, callback) {
}); });
if (callback) { if (callback) {
promise.then(user => { promise.then(user => {
callback(null, user); callback(null, user);
}) })
.catch(err => { .catch(err => {
callback(err); callback(err);
@ -189,12 +190,12 @@ const staticMethods = {
user_info: { password: password, register: true }, user_info: { password: password, register: true },
data: username data: username
}; };
if (callback) { if (callback) {
const message = "register(..., callback) is now deprecated in favor of register(): Promise<User>. This function argument will be removed in future versions."; const message = "register(..., callback) is now deprecated in favor of register(): Promise<User>. This function argument will be removed in future versions.";
(console.warn || console.log).call(console, message); (console.warn || console.log).call(console, message);
} }
return _authenticate(this, server, json, callback); return _authenticate(this, server, json, callback);
}, },
@ -205,7 +206,7 @@ const staticMethods = {
user_info: { password: password }, user_info: { password: password },
data: username data: username
}; };
if (callback) { if (callback) {
const message = "login(..., callback) is now deprecated in favor of login(): Promise<User>. This function argument will be removed in future versions."; const message = "login(..., callback) is now deprecated in favor of login(): Promise<User>. This function argument will be removed in future versions.";
(console.warn || console.log).call(console, message); (console.warn || console.log).call(console, message);
@ -216,7 +217,7 @@ const staticMethods = {
registerWithProvider(server, options, callback) { registerWithProvider(server, options, callback) {
// Compatibility with previous signature: // Compatibility with previous signature:
// registerWithProvider(server, provider, providerToken, callback) // registerWithProvider(server, provider, providerToken, callback)
if (arguments.length === 4) { if (arguments.length === 4) {
checkTypes(arguments, ['string', 'string', 'string', 'function']); checkTypes(arguments, ['string', 'string', 'string', 'function']);
@ -242,7 +243,7 @@ const staticMethods = {
const message = "registerWithProvider(..., callback) is now deprecated in favor of registerWithProvider(): Promise<User>. This function argument will be removed in future versions."; const message = "registerWithProvider(..., callback) is now deprecated in favor of registerWithProvider(): Promise<User>. This function argument will be removed in future versions.";
(console.warn || console.log).call(console, message); (console.warn || console.log).call(console, message);
} }
return _authenticate(this, server, json, callback); return _authenticate(this, server, json, callback);
}, },