Skip to content

fix: preserve CacheError errorCode through cache save path and convert to BrowserAuthError#8598

Open
hectormmg wants to merge 3 commits into
devfrom
fix/cache-error-wrapping
Open

fix: preserve CacheError errorCode through cache save path and convert to BrowserAuthError#8598
hectormmg wants to merge 3 commits into
devfrom
fix/cache-error-wrapping

Conversation

@hectormmg
Copy link
Copy Markdown
Member

@hectormmg hectormmg commented May 21, 2026

Problem

Client applications were seeing e.setCorrelationId is not a function errors. Investigation revealed two issues in the 3P layer:

  1. CacheManager.saveCacheRecord double-wraps CacheError: When setAccessTokenCredential throws CacheError('cache_quota_exceeded'), the catch block called createCacheError(e) which used e.name ('CacheError') as the errorCode — producing CacheError('CacheError') and losing the original errorCode.

  2. BrowserCacheManager.saveCacheRecord re-throws CacheError as-is: CacheError extends Error, not AuthError, so callers that call e.setCorrelationId() crash because the method doesn't exist.

Fix

  • CacheManager.saveCacheRecord: Re-throw existing CacheErrors without re-wrapping, preserving the original errorCode.
  • BrowserCacheManager.saveCacheRecord: Convert CacheError to BrowserAuthError (which extends AuthError and has setCorrelationId) before re-throwing.

Testing

Added failing tests first (TDD), then applied fixes:

  • CacheManager.spec.ts: asserts errorCode is preserved as cache_quota_exceeded
  • BrowserCacheManager.spec.ts: asserts thrown error is instanceof BrowserAuthError with setCorrelationId as a function

Related

…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>
Copilot AI review requested due to automatic review settings May 21, 2026 00:38
@hectormmg hectormmg requested a review from a team as a code owner May 21, 2026 00:38
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-wrapping CacheError in CacheManager.saveCacheRecord, preserving the original errorCode.
  • msal-browser: Convert CacheError thrown by saveCacheRecord into BrowserAuthError before 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants