Skip to content

fix: handle invalid selector errors#1976

Open
alxckn wants to merge 6 commits into
open-feature:mainfrom
alxckn:handle_bad_selector_errors
Open

fix: handle invalid selector errors#1976
alxckn wants to merge 6 commits into
open-feature:mainfrom
alxckn:handle_bad_selector_errors

Conversation

@alxckn
Copy link
Copy Markdown
Member

@alxckn alxckn commented Jun 1, 2026

This PR

Invalid selector expressions in the flagd-selector header were causing unhandled errors that surfaced as 500 responses. This PR validates the selector at parse time and maps invalid input to the appropriate error code
(InvalidArgument / HTTP 400) across all service layers (flag evaluation, flag sync, OFRep).

As part of this fix, the Selector construction API has been redesigned to eliminate unchecked string-based construction entirely:

  • NewSelector(expr string) now returns an error for unknown keys
  • WithIndex is removed (breaking change) — replaced by typed methods WithSource and WithFlagSetId which cannot produce an invalid selector

Related Issues

Notes

Breaking change: (Selector).WithIndex(key, value string) has been removed from the public API. Callers should migrate to Selector{}.WithSource(source) or Selector{}.WithFlagSetId(id).

(with AI help)

@alxckn alxckn requested review from a team as code owners June 1, 2026 08:47
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jun 1, 2026
@netlify
Copy link
Copy Markdown

netlify Bot commented Jun 1, 2026

Deploy Preview for polite-licorice-3db33c canceled.

Name Link
🔨 Latest commit 9dac26e
🔍 Latest deploy log https://app.netlify.com/projects/polite-licorice-3db33c/deploys/6a1dfe659edc500008b2746b

@alxckn alxckn force-pushed the handle_bad_selector_errors branch from 8e8b5fd to d79d9be Compare June 1, 2026 08:48
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the Selector creation and manipulation. NewSelector now validates selector keys and returns an error for invalid keys, with corresponding error handling added across evaluation and sync services. Additionally, generic WithIndex calls are replaced with type-safe methods like WithSource and WithFlagSetId. The review feedback recommends two improvements: recording the error on the tracing span when startResolveV2 fails in flag_evaluator_v2.go, and setting exitReason = "error" when returning an invalid selector error in flag-sync/handler.go to ensure accurate metrics.

Comment thread flagd/pkg/service/flag-evaluation/flag_evaluator_v2.go
Comment thread flagd/pkg/service/flag-sync/handler.go
@alxckn alxckn force-pushed the handle_bad_selector_errors branch from 75e6aff to 9a39218 Compare June 1, 2026 11:50
@toddbaert toddbaert changed the title fix: Handle invalid selector errors fix: handle invalid selector errors Jun 1, 2026
@toddbaert
Copy link
Copy Markdown
Member

@alxckn unit test for NewSelector is good, but I'd like service-level coverage too; the whole point is "no more 500s on bad selectors". One test per surface asserting:

  • connect.CodeInvalidArgument on v0/v1/v2 eval + sync
  • HTTP 400 on OFREP (single + bulk)

Otherwise this can regress silently. WDYT?

Also FYI, some of the sonar issues should be fixed on main now; might be worth a rebase/merge before the next push.

alxckn added 5 commits June 1, 2026 23:33
Signed-off-by: Alexandre Chakroun <achakroun@macmail.fr>
Signed-off-by: Alexandre Chakroun <achakroun@macmail.fr>
Signed-off-by: Alexandre Chakroun <achakroun@macmail.fr>
Signed-off-by: Alexandre Chakroun <achakroun@macmail.fr>
Signed-off-by: Alexandre Chakroun <achakroun@macmail.fr>
@alxckn alxckn force-pushed the handle_bad_selector_errors branch from 9a39218 to b353b2e Compare June 1, 2026 21:33
Signed-off-by: Alexandre Chakroun <achakroun@macmail.fr>
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Jun 1, 2026

@alxckn
Copy link
Copy Markdown
Member Author

alxckn commented Jun 1, 2026

@toddbaert thanks for the review. I took care of comments, added integration tests to prevent regressions, and the sonar warnings seem resolved 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants