fix: preserve CacheError errorCode through cache save path and convert to BrowserAuthError#8598
Open
hectormmg wants to merge 3 commits into
Open
fix: preserve CacheError errorCode through cache save path and convert to BrowserAuthError#8598hectormmg wants to merge 3 commits into
hectormmg wants to merge 3 commits into
Conversation
…t to BrowserAuthError
- CacheManager.saveCacheRecord: re-throw CacheError without wrapping, preserving original errorCode
- BrowserCacheManager.saveCacheRecord: convert CacheError to BrowserAuthError so callers can call setCorrelationId
- Add tests for both behaviors
Fixes SharePoint telemetry showing 'e.setCorrelationId is not a function' because
CacheError does not extend AuthError. The errorCode was also being lost due to
double-wrapping (CacheError('cacheQuotaExceeded') -> CacheError('CacheError')).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes cache-save error handling so (1) CacheError.errorCode is preserved when thrown from the cache save path, and (2) msal-browser rethrows cache-save failures as an AuthError-derived type (BrowserAuthError) so callers can safely call .setCorrelationId().
Changes:
msal-common: Avoid double-wrappingCacheErrorinCacheManager.saveCacheRecord, preserving the originalerrorCode.msal-browser: ConvertCacheErrorthrown bysaveCacheRecordintoBrowserAuthErrorbefore rethrowing, maintaining compatibility with.setCorrelationId().- Added/updated unit tests to lock in both behaviors.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/msal-common/test/cache/CacheManager.spec.ts | Adds a test asserting CacheError.errorCode is preserved when setAccessTokenCredential rejects with CacheError. |
| lib/msal-common/src/cache/CacheManager.ts | Re-throws existing CacheError instances instead of wrapping via createCacheError, preserving errorCode. |
| lib/msal-browser/test/cache/BrowserCacheManager.spec.ts | Updates test to expect BrowserAuthError (with setCorrelationId) and preserved errorCode when cache-save rejects with CacheError. |
| lib/msal-browser/src/cache/BrowserCacheManager.ts | Converts CacheError to BrowserAuthError in saveCacheRecord before rethrowing, while still emitting cache telemetry fields. |
| ); | ||
| } catch (e) {} | ||
| } | ||
| throw createBrowserAuthError(e.errorCode); |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Client applications were seeing
e.setCorrelationId is not a functionerrors. Investigation revealed two issues in the 3P layer:CacheManager.saveCacheRecorddouble-wrapsCacheError: WhensetAccessTokenCredentialthrowsCacheError('cache_quota_exceeded'), the catch block calledcreateCacheError(e)which usede.name('CacheError') as the errorCode — producingCacheError('CacheError')and losing the original errorCode.BrowserCacheManager.saveCacheRecordre-throwsCacheErroras-is:CacheError extends Error, notAuthError, so callers that calle.setCorrelationId()crash because the method doesn't exist.Fix
CacheManager.saveCacheRecord: Re-throw existingCacheErrors without re-wrapping, preserving the original errorCode.BrowserCacheManager.saveCacheRecord: ConvertCacheErrortoBrowserAuthError(which extendsAuthErrorand hassetCorrelationId) before re-throwing.Testing
Added failing tests first (TDD), then applied fixes:
CacheManager.spec.ts: asserts errorCode is preserved ascache_quota_exceededBrowserCacheManager.spec.ts: asserts thrown error isinstanceof BrowserAuthErrorwithsetCorrelationIdas a functionRelated