Repository navigation
Wrap exceptions thrown while reading a value in YamlException - #177
Merged
Merged
Conversation
A custom converter that threw while reading a root value, or any value through a source-generated context, surfaced its own exception type, while the reflection-based object converter already wrapped the same exception for its members. YamlSerializer now wraps such exceptions once, at the deserialize entry points, with the reader position and the original exception as InnerException. NotSupportedException (configuration errors) and IOException (source failures) propagate unchanged. The generated code does not grow. Fixes #168 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: CodeAlta <noreply@codealta.io>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #168.
Why
An exception thrown by a custom
YamlConverter<T>.Readreached the caller as aYamlExceptiononly when the reflection-based object converter read it as a member. At the root, in a root collection, and anywhere through a source-generated context, it kept its own type. So the same model failed with a different exception depending on whether it used reflection or a generated context, and acatch (YamlException)orTryDeserializedid not catch it.YamlExceptionYamlExceptionYamlExceptionYamlExceptionWhat
YamlSerializer's fourDeserializeCoreoverloads wrap an exception thrown byYamlTypeInfo.Readin aYamlExceptionwith the reader position (the value the converter failed on) and the original exception asInnerException, as the object converter already does. This is one try/catch per call, and the generated code does not change, so Generate one member reader per type and move merge-key handling into the runtime #172–Generate one member reader per member type instead of one per member #174's size reductions are kept.YamlExceptionpasses through as before, so a converter can still choose its own message and location.NotSupportedException(configuration errors such as a missing converter, or a context generated without readers by Add YamlSourceGenerationOptions.GenerationMode to skip writers or readers #173) andIOException(a failure of the source) propagate unchanged.InvalidOperationExceptionwhile reading (Populateon a read-only struct member) are now wrapped too. That is already the case in the reflection path when the error comes from a member. Three tests that expected the bareInvalidOperationExceptionnow check it asInnerException; the message is unchanged.error-handling.mdand a pointer from the converter docs.Tests
YamlConverterExceptionTests(12 tests): reflection root, root list, member andTextReader,TryDeserialize, generated member (with location), attribute-converter member, list element and constructor parameter, plus theYamlExceptionandNotSupportedExceptionpass-through. 9 of them fail without the fix.🤖 Generated with Claude Code
This pull request was created by CodeAlta, an AI coding agent.