Skip to content

Commit

Permalink
Handle failures to POST to authentication endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
wparad committed Jan 12, 2024
1 parent 534ee7a commit 31a49f3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const errorMessages = new Set([
'NetworkError when attempting to fetch resource.', // Firefox
'The Internet connection appears to be offline.', // Safari 16
'Network request failed', // `cross-fetch`
'fetch failed' // Undici (Node.js)
'fetch failed', // Undici (Node.js)
'<HTML DOCUMENT></HTML>' // Handle some HTML error page responses as well
]);

function isNetworkError(error) {
Expand Down
11 changes: 9 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,10 @@ class LoginClient {
userSessionResolver();
return true;
} catch (error) {
this.logger && this.logger.log({ title: 'Failed exchange authentication response for a token.', error });

// The code was expired, contaminated, or already exchanged.
if (error.data && error.data.error === 'invalid_request') {
this.logger && this.logger.log({ title: 'Failed exchange authentication response for a token.', error });
return false;
}
throw (error.data || error);
Expand Down Expand Up @@ -365,7 +366,9 @@ class LoginClient {
} catch (error) {
// On 400, 404, 409 we know that the session is no longer able to be continued.
if (error.status !== 400 && error.status !== 404 && error.status !== 409) {
this.logger && this.logger.log({ title: 'Failed attempting to check if the user has an existing authentication session', error });
this.logger && this.logger.log && this.logger.log({ title: 'User does not have an existing authentication session', error });
} else {
this.logger && this.logger.log && this.logger.log({ title: 'Failed attempting to check if the user has an existing authentication session', error });
}
}
const newUserData = this.getUserIdentity();
Expand Down Expand Up @@ -407,6 +410,7 @@ class LoginClient {

window.location.assign(requestOptions.data.authenticationUrl);
} catch (error) {
this.logger && this.logger.log && this.logger.log({ title: 'Failed to update extension authentication request', error });
if (error.status && error.status >= 400 && error.status < 500) {
const e = Error(error.data && (error.data.title || error.data.errorCode) || error.data || 'Unknown Error');
e.code = error.data && error.data.errorCode;
Expand Down Expand Up @@ -455,6 +459,7 @@ class LoginClient {
try {
await this.httpClient.delete(`/identities/${encodeURIComponent(identityId)}`, this.enableCredentials, headers);
} catch (error) {
this.logger && this.logger.log && this.logger.log({ title: 'Failed to unlink user identity', error });
if (error.status && error.status >= 400 && error.status < 500) {
const e = Error(error.data && (error.data.title || error.data.errorCode) || error.data || 'Unknown Error');
e.code = error.data && error.data.errorCode;
Expand Down Expand Up @@ -513,6 +518,7 @@ class LoginClient {
}, headers);
window.location.assign(requestOptions.data.authenticationUrl);
} catch (error) {
this.logger && this.logger.log && this.logger.log({ title: 'Failed to start user identity link', error });
if (error.status && error.status >= 400 && error.status < 500) {
const e = Error(error.data && (error.data.title || error.data.errorCode) || error.data || 'Unknown Error');
e.code = error.data && error.data.errorCode;
Expand Down Expand Up @@ -580,6 +586,7 @@ class LoginClient {
window.location.assign(authResponse.data.authenticationUrl);
}
} catch (error) {
this.logger && this.logger.log && this.logger.log({ title: 'Failed to start authentication for user', error });
if (error.status && error.status >= 400 && error.status < 500) {
const e = Error(error.data && (error.data.title || error.data.errorCode) || error.data || 'Unknown Error');
e.code = error.data && error.data.errorCode;
Expand Down

0 comments on commit 31a49f3

Please sign in to comment.