mongodb/node-mongodb-native · error · MongoOIDCError
Attempted to get a refresh token when none exists.
Error message
Attempted to get a refresh token when none exists.
What it means
Internal invariant of TokenCache (token_cache.ts:34): getRefreshToken() was called while refreshToken is undefined. The human workflow checks hasRefreshToken before calling getRefreshToken, so this throw should be unreachable through the public API. Reaching it implies a cache-state bug in the driver or direct use of the @internal TokenCache.
Solutions
- File a driver bug if reached via the public API with a reproduction
- When using internal APIs, check cache.hasRefreshToken before cache.getRefreshToken()
- Upgrade the driver in case the regression is already fixed
Example fix
// before const rt = cache.getRefreshToken(); // after const rt = cache.hasRefreshToken ? cache.getRefreshToken() : undefined;
Defensive patterns
Strategy: type-guard
Type guard
function getRefreshTokenSafe(cache: TokenCache): string | undefined {
return cache.hasRefreshToken ? cache.getRefreshToken() : undefined;
} Prevention
- Use the public API; the workflows already guard getRefreshToken with hasRefreshToken
- In forks, always check hasRefreshToken before getRefreshToken
- Report regressions with a reproduction rather than working around the invariant
When it happens
Trigger: Calling the internal TokenCache.getRefreshToken() without the hasRefreshToken guard; or a regression where a workflow path calls getRefreshToken after removeRefreshToken cleared it.
Common situations: Misuse of internal APIs in a forked/custom auth provider. A driver regression in the human callback workflow's refresh-token branch.
Related errors
- Attempted to get an access token when none exists.
- Attempted to get IDP information when none exists.
- An unexpected error type
- AuthContext must contain a valid nonce property
- No AuthProvider for defined.
AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11).
Data as JSON: /api/errors/0255b3b73d7352a4.
Report an issue: GitHub.
Appendix: source
Thrown at src/cmap/auth/mongodb_oidc/token_cache.ts:34
get hasRefreshToken(): boolean {
return !!this.refreshToken;
}
get hasIdpInfo(): boolean {
return !!this.idpInfo;
}
getAccessToken(): string {
if (!this.accessToken) {
throw new MongoOIDCError('Attempted to get an access token when none exists.');
}
return this.accessToken;
}
getRefreshToken(): string {
if (!this.refreshToken) {
throw new MongoOIDCError('Attempted to get a refresh token when none exists.');
}
return this.refreshToken;
}
getIdpInfo(): IdPInfo {
if (!this.idpInfo) {
throw new MongoOIDCError('Attempted to get IDP information when none exists.');
}
return this.idpInfo;
}
put(response: OIDCResponse, idpInfo?: IdPInfo) {
this.accessToken = response.accessToken;
this.refreshToken = response.refreshToken;
this.expiresInSeconds = response.expiresInSeconds;
if (idpInfo) {
this.idpInfo = idpInfo;
}View on GitHub (pinned to dce7939f86)