aaubry / YamlDotNet

YamlDotNet is a .NET library for YAML
MIT License
2.54k stars 477 forks source link

Handle TargetInvocationException when reading a property #879

Open alexeykuptsov opened 9 months ago

alexeykuptsov commented 9 months ago

In my project I use YamlDotNet to serialize arbitrary objects. I can't mark properties of the objects with [YamlIgnore] but I want serializer not to fail in cases when an erroneous property is met. Could you add an ability to serialize such properties as YAML strings literals with a simple error message?

alexeykuptsov commented 9 months ago

I'm sorry for this mess of closing and reopening. I'm getting used to contributing to OSS 😅

EdwardCooke commented 8 months ago

Can also add this to the static serializer pieces of yamldotnet?

alexeykuptsov commented 8 months ago

I've added test StaticSerializationHandlesTargetInvocationException but I don't understand the static serializer output. The serializer doesn't throw and returns empty object output {}. Can you help me with binding field StaticSerializerBuilder.handleTargetInvocationExceptions with the part of code where it reads from an input object property?

EdwardCooke commented 5 months ago

Just looked this over. One other comment, it may be better to bring the exception handling logic out of the object descriptor, instead pass in a method that would get called in the catch. Takes 3 parameters, exception, object and property. Default of that method would be throw. Then people can handle any different exceptions differently based on their needs. For example, dump out the exception message.

alexeykuptsov commented 5 months ago

@EdwardCooke Hi Edward, I've implemented the support of custom exception handler function. But I haven't implemented default method "throw". I don't like this idea because try/catch block may affect performance in simple cases like serialization of plain objects with only auto-properties (POCO).