Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JSONSelection] Report better parsing error messages using extra: X field of LocatedSpan<T, X = ()> #6089

Merged
merged 2 commits into from
Nov 4, 2024

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Sep 30, 2024

The LocatedSpan<T, X = ()> type provided by the nom_locate crate (first introduced in PR #5987) supports an optional user-defined extra: X field, which we can use to smuggle meaningful custom error messages out of the parser.

It would be ideal if we could report multiple parsing errors and continue best-effort parsing after an error occurs, but that will have to wait for future PRs. Also, there are some cases where parsing fails because various alt(...) combinators ran out of options, and we can't (yet) determine which underlying error was "most" responsible for the failure, so the error message remains an opaque nom::error::ErrorKind code.

As a side benefit, I've attempted to stabilize the method signature of the public JSONSelection::parse method, so consumers don't depend on nom- and nom_locate-specific types, so we have the freedom to swap out implementation details in the future without breaking consumers. This primarily means continuing to accept a &str as input (rather than a LocatedSpan) and returning a custom JSONSelectionParseError type in case of failure.

While improving parsing error messages is still a work in progress, I think this PR meaningfully improves the situation, and is worth considering as a first step.

@router-perf
Copy link

router-perf bot commented Sep 30, 2024

CI performance tests

  • connectors-const - Connectors stress test that runs with a constant number of users
  • const - Basic stress test that runs with a constant number of users
  • demand-control-instrumented - A copy of the step test, but with demand control monitoring and metrics enabled
  • demand-control-uninstrumented - A copy of the step test, but with demand control monitoring enabled
  • enhanced-signature - Enhanced signature enabled
  • events - Stress test for events with a lot of users and deduplication ENABLED
  • events_big_cap_high_rate - Stress test for events with a lot of users, deduplication enabled and high rate event with a big queue capacity
  • events_big_cap_high_rate_callback - Stress test for events with a lot of users, deduplication enabled and high rate event with a big queue capacity using callback mode
  • events_callback - Stress test for events with a lot of users and deduplication ENABLED in callback mode
  • events_without_dedup - Stress test for events with a lot of users and deduplication DISABLED
  • events_without_dedup_callback - Stress test for events with a lot of users and deduplication DISABLED using callback mode
  • extended-reference-mode - Extended reference mode enabled
  • large-request - Stress test with a 1 MB request payload
  • no-tracing - Basic stress test, no tracing
  • reload - Reload test over a long period of time at a constant rate of users
  • step-jemalloc-tuning - Clone of the basic stress test for jemalloc tuning
  • step-local-metrics - Field stats that are generated from the router rather than FTV1
  • step-with-prometheus - A copy of the step test with the Prometheus metrics exporter enabled
  • step - Basic stress test that steps up the number of users over time
  • xlarge-request - Stress test with 10 MB request payload
  • xxlarge-request - Stress test with 100 MB request payload

@benjamn benjamn marked this pull request as ready for review October 1, 2024 14:57
@benjamn benjamn requested review from nicholascioli and a team as code owners October 1, 2024 14:57
@benjamn benjamn force-pushed the benjamn/JSONSelection-better-parse-error-messages branch from 39da235 to 4e30de5 Compare October 1, 2024 16:06
@benjamn benjamn force-pushed the benjamn/JSONSelection-better-parse-error-messages branch from 4e30de5 to 4d96927 Compare October 17, 2024 18:00
@svc-apollo-docs
Copy link
Collaborator

svc-apollo-docs commented Oct 17, 2024

✅ Docs Preview Ready

No new or changed pages found.

@benjamn benjamn force-pushed the benjamn/JSONSelection-better-parse-error-messages branch 2 times, most recently from 1cf229b to 506a282 Compare October 23, 2024 17:06
Copy link
Member

@dylan-apollo dylan-apollo left a comment

Choose a reason for hiding this comment

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

I finally tried this out and it works great! One suggestion on the API that will make using these improved errors a bit easier elsewhere (but requires updating consumers now since the API changes). I'm happy to make a PR into this which updates validation & the like if that'd be helpful.

The LocatedSpan<T, X = ()> type supports an optional user-defined
`extra: X` field, which we can use to smuggle meaningful custom error
messages out of the parser.
@benjamn benjamn force-pushed the benjamn/JSONSelection-better-parse-error-messages branch from 506a282 to 76949bd Compare November 1, 2024 14:40
Copy link
Member

@dylan-apollo dylan-apollo left a comment

Choose a reason for hiding this comment

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

Looks great, thanks! I'll get it wired up to show the offsets in the LSP in a follow-up.

Comment on lines +32 to +39
// ParseResult is the internal type returned by most ::parse methods, as it is
// convenient to use with nom's combinators. The top-level JSONSelection::parse
// method returns a slightly different IResult type that hides implementation
// details of the nom-specific types.
//
// TODO Consider switching the third IResult type parameter to VerboseError
// here, if error messages can be improved with additional context.
pub(super) type ParseResult<'a, T> = IResult<Span<'a>, T>;
Copy link
Member

Choose a reason for hiding this comment

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

In the future (not suggesting you need to go and change the ones in this PR), if you use triple-slash quotes right before an item, it sets that comment as the docs for the item. Docs also support Markdown syntax, so you can get nicely-rendered docs for things in your editor. Here's in RustRover, where that link to JSONSelection::parse actually brings you to the right code!

Screenshot 2024-11-01 at 10 59 47 AM
Screenshot 2024-11-01 at 11 00 41 AM

@benjamn benjamn merged commit e12af36 into next Nov 4, 2024
12 of 13 checks passed
@benjamn benjamn deleted the benjamn/JSONSelection-better-parse-error-messages branch November 4, 2024 20:03
@benjamn benjamn mentioned this pull request Nov 5, 2024
6 tasks
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.

4 participants