Skip to content

fix!: onError not called if cacheFlags is true and api is not reachable#365

Merged
talissoncosta merged 3 commits intomainfrom
fix/onerror-with-cache-flags-348
Dec 9, 2025
Merged

fix!: onError not called if cacheFlags is true and api is not reachable#365
talissoncosta merged 3 commits intomainfrom
fix/onerror-with-cache-flags-348

Conversation

@talissoncosta
Copy link
Copy Markdown
Contributor

@talissoncosta talissoncosta commented Dec 8, 2025

Summary

Problem

When cacheFlags: true is set but no cache exists yet (first-time user), and the API fails, the onError callback was never called. The error was silently swallowed in the catch block at flagsmith-core.ts:517-519.

Before this fix:

  • init() resolves successfully (even though API failed)
  • onError is NOT called
  • User has no way to know initialization failed

After this fix:

  • init() rejects with the API error
  • onError IS called with the error
  • Behavior now matches when cacheFlags: false and API fails

Breaking Change

This is a behavior change for users with cacheFlags: true when:

  1. No cache exists yet (first initialization)
  2. API request fails

Previously init() would silently succeed; now it properly rejects.

Migration: Users relying on silent failure should add error handling to their init() call or use defaultFlags as a fallback.

Test plan

  • Added test: "should call onError when the API cannot be reached with cacheFlags enabled but no cache exists"
  • Verified test fails without the fix (TDD)
  • Verified all 69 tests pass with the fix

Fixes #348

talissoncosta and others added 2 commits December 8, 2025 10:09
…ache

Reproduces issue #348 where onError is not called when cacheFlags is true,
no cache exists, and the API fails.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
When cacheFlags is true but no cache exists, API errors were silently
swallowed. Now properly calls onError and rejects init() when there
are no fallback flags available.

BREAKING CHANGE: When cacheFlags is true and no cache exists, init()
will now reject if the API fails and no defaultFlags are provided.
Previously, errors were silently swallowed.

Fixes #348

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@talissoncosta talissoncosta marked this pull request as ready for review December 8, 2025 14:15
@talissoncosta talissoncosta requested a review from a team as a code owner December 8, 2025 14:15
@talissoncosta talissoncosta requested review from kyle-ssg and removed request for a team December 8, 2025 14:15
@talissoncosta talissoncosta changed the title fix: onError not called if cacheFlags is true and api is not reachable fix!: onError not called if cacheFlags is true and api is not reachable Dec 9, 2025
@jhoermann
Copy link
Copy Markdown
Contributor

@talissoncosta Does this really have to be a breaking change release? Normally fixes are considered patch releases.

@talissoncosta
Copy link
Copy Markdown
Contributor Author

@jhoermann Good question! Normally yes, but @kyle-ssg raised a valid concern: this fix changes behavior in a way that could break existing apps.

Before: When cacheFlags: true and API fails with no cache, init() silently succeeds and onError is never called.
After: init() rejects and onError is called.

The issue is that users might have onError callbacks that throw or have side effects they didn't expect to trigger in this scenario. Since their apps currently "work" (even if incorrectly), this fix could cause unexpected crashes. We decided to err on the side of caution with a major bump. Happy to discuss if you think a patch is more appropriate!

@jhoermann
Copy link
Copy Markdown
Contributor

@jhoermann Good question! Normally yes, but @kyle-ssg raised a valid concern: this fix changes behavior in a way that could break existing apps.

Before: When cacheFlags: true and API fails with no cache, init() silently succeeds and onError is never called. After: init() rejects and onError is called.

The issue is that users might have onError callbacks that throw or have side effects they didn't expect to trigger in this scenario. Since their apps currently "work" (even if incorrectly), this fix could cause unexpected crashes. We decided to err on the side of caution with a major bump. Happy to discuss if you think a patch is more appropriate!

Ok got it 👍

@talissoncosta talissoncosta merged commit 1411b85 into main Dec 9, 2025
1 check passed
@talissoncosta talissoncosta deleted the fix/onerror-with-cache-flags-348 branch December 9, 2025 13:34
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.

onError not called if cacheFlags is true and api is not reachable

3 participants