Skip to content

Surface actual parse error in MergeYaml via Validated API#6818

Merged
timtebeek merged 5 commits intomainfrom
merge-yaml-surface-parse-error
Apr 1, 2026
Merged

Surface actual parse error in MergeYaml via Validated API#6818
timtebeek merged 5 commits intomainfrom
merge-yaml-surface-parse-error

Conversation

@timtebeek
Copy link
Copy Markdown
Member

@timtebeek timtebeek commented Feb 25, 2026

Summary

  • When MergeYaml encountered a YAML parsing failure, the old maybeParse method silently discarded the ParseError result (via a Yaml.Documents instanceof filter), producing only a generic "Must be valid YAML" message
  • Now extracts the actual error from the ParseExceptionResult marker and surfaces it through the Validated API, making failures diagnosable (e.g. SnakeYAML syntax errors, print-idempotence failures)
  • Introduces validateParseable() returning Validated<Yaml> so that validate() uses the Validated API instead of exception-based control flow
  • parse() delegates to validateParseable(), eliminating duplicated parsing logic

Test plan

  • invalidYaml() test updated to match new validation message
  • sourceNull() test still passes with existing "Must be valid YAML" message
  • All existing MergeYamlTest tests pass

When MergeYaml.parse() fails, the actual parsing error (stored in a
ParseExceptionResult marker on the ParseError) was being silently
discarded by the Yaml.Documents instanceof filter, resulting in
an unhelpful "Could not parse as YAML" message.

Now the ParseExceptionResult message is extracted and included in
the IllegalArgumentException, making it possible to diagnose the
root cause (e.g. SnakeYAML syntax errors, print-idempotence failures).
@timtebeek
Copy link
Copy Markdown
Member Author

We saw this somehow pass validation but fail during execution, so rewrote the logic to capture and forward any parse failure message.

@timtebeek timtebeek requested a review from MBoegers February 25, 2026 14:14
@timtebeek timtebeek added bug Something isn't working recipe Requested Recipe labels Feb 25, 2026
try {
incoming = MergeYaml.parse(yaml);
return true;
} catch (IllegalArgumentException e) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It feels strange to have an exception to signal failure. Failure is nothing special its expected in validation.
Also, creating an exception causes stack walking to fill the stacktrace and JIT deopt, maybe not very strong arguments in our case but still.

We could omit the exception and instead return the parsing error or erroneous and check with instaceof?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

or turn the parse into a validateParseable method and return a validation result insteadt?

Extract validateParseable() returning Validated<Yaml> so that
validate() no longer uses try/catch for control flow, addressing
PR review feedback.
@timtebeek timtebeek changed the title Surface actual parse error in MergeYaml Surface actual parse error in MergeYaml via Validated API Mar 24, 2026
@timtebeek timtebeek requested a review from MBoegers March 24, 2026 23:39
The validation message changed from "Must be valid YAML" to
"Could not parse as YAML: <details>" to surface the actual
parse error.
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Mar 26, 2026
@timtebeek timtebeek added the yaml label Mar 26, 2026
@timtebeek
Copy link
Copy Markdown
Member Author

@MBoegers any chance you could review? Or should I ask someone else?

Copy link
Copy Markdown
Contributor

@MBoegers MBoegers left a comment

Choose a reason for hiding this comment

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

Interesting style to use the Validated<> API. I like!

@timtebeek timtebeek merged commit a770d1b into main Apr 1, 2026
1 check passed
@timtebeek timtebeek deleted the merge-yaml-surface-parse-error branch April 1, 2026 10:20
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working recipe Requested Recipe yaml

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants