Skip to content

Commit

Permalink
fix(oidc): improve error handling (release) (#1403)
Browse files Browse the repository at this point in the history
Co-authored-by: mkrzempek <mkrzempek@guidewire.com>
  • Loading branch information
krzempekk and mkrzempek authored Jul 11, 2024
1 parent c56ceb8 commit 72a6373
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
7 changes: 5 additions & 2 deletions packages/oidc-client/src/oidc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,12 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
return await fetchFromIssuer(this.getFetch())(authority, this.configuration.authority_time_cache_wellknowurl_in_second ?? 60 * 60, storage, this.configuration.authority_timeout_wellknowurl_in_millisecond);
};
this.initPromise = localFuncAsync();
return this.initPromise.then((result) => {
return this.initPromise.finally(() => {
// in case if anything went wrong with the promise, we should reset the initPromise to null too
// otherwise client can't re-init the OIDC client
// as the promise is already fulfilled with rejected state, so could not ever reach this point again,
// so that leads to infinite loop of calls, when client tries to re-init the OIDC client after error
this.initPromise = null;
return result;
});
}

Expand Down
16 changes: 14 additions & 2 deletions packages/oidc-client/src/renewTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,19 @@ const synchroniseTokensAsync = (oidc:Oidc) => async (index = 0, forceRefresh = f
}
} catch (exception: any) {
console.error(exception);
oidc.publishEvent(eventNames.refreshTokensAsync_silent_error, { message: 'exception', exception: exception.message });
return synchroniseTokensAsync(oidc)(nextIndex, forceRefresh, extras, updateTokens);
oidc.publishEvent(eventNames.refreshTokensAsync_silent_error, {
message: 'exception',
exception: exception.message,
});
// we need to break the loop or errors, as direct call of synchroniseTokensAsync
// inside of synchroniseTokensAsync will cause an infinite loop and kill the browser stack
// so we need to brake calls chain and delay next call
return new Promise((resolve, reject) => {
setTimeout(() => {
synchroniseTokensAsync(oidc)(nextIndex, forceRefresh, extras, updateTokens)
.then(resolve)
.catch(reject);
}, 1000);
});
}
}

0 comments on commit 72a6373

Please sign in to comment.