Skip to content

Wrap exceptions thrown while reading a value in YamlException - #177

Merged
xoofx merged 1 commit into
mainfrom
fix/168-wrap-converter-exceptions
Nov 4, 2026
Merged

xoofx merged 1 commit into
mainfrom
fix/168-wrap-converter-exceptions

Conversation

@xoofx

@xoofx xoofx commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Fixes #168.

Why

An exception thrown by a custom YamlConverter<T>.Read reached the caller as a YamlException only 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 a catch (YamlException) or TryDeserialize did not catch it.

Path Before After
Reflection, member / ctor / element under a member YamlException YamlException
Reflection, root value or root collection original type YamlException
Source-generated, any position original type YamlException

What

  • YamlSerializer's four DeserializeCore overloads wrap an exception thrown by YamlTypeInfo.Read in a YamlException with the reader position (the value the converter failed on) and the original exception as InnerException, 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.
  • YamlException passes 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) and IOException (a failure of the source) propagate unchanged.
  • Configuration errors thrown as InvalidOperationException while reading (Populate on 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 bare InvalidOperationException now check it as InnerException; the message is unchanged.
  • Docs: a section in error-handling.md and a pointer from the converter docs.

Tests

YamlConverterExceptionTests (12 tests): reflection root, root list, member and TextReader, TryDeserialize, generated member (with location), attribute-converter member, list element and constructor parameter, plus the YamlException and NotSupportedException pass-through. 9 of them fail without the fix.

cd src
dotnet test -c Release

🤖 Generated with Claude Code

This pull request was created by CodeAlta, an AI coding agent.

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>
@xoofx
xoofx merged commit e7caed1 into main Nov 4, 2026
2 checks passed
@xoofx
xoofx deleted the fix/168-wrap-converter-exceptions branch November 4, 2026 20:09
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.

Exceptions thrown from a custom YamlConverter<T>.Read are not wrapped in YamlException

1 participant