kbilsted / StatePrinter

Automating unit testing and ToString() coding
Apache License 2.0
93 stars 32 forks source link

Properties that throw exceptions are not handled #45

Open Arithmomaniac opened 8 years ago

Arithmomaniac commented 8 years ago

If PublicFieldsAndPropertiesHarvester is used, and a property access attempt throws an exception, the exception bubbles up to the top and cannot be handled, as in the code below.

It would be better to at least allow the user to have the exception returned as the value for that property instead.

System.Reflection.TargetInvocationException occurred
  HResult=-2146232828
  Message=Exception has been thrown by the target of an invocation.
  Source=mscorlib
  StackTrace:
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
       at StatePrinter.FieldHarvesters.HarvestHelper.<>c__DisplayClass9.<GetFieldsAndProperties>b__6(Object o)
       at StatePrinter.Introspection.IntroSpector.IntrospectComplexType(Object source, Field field, Type sourceType)
       at StatePrinter.Introspection.IntroSpector.Introspect(Object source, Field field)
       at StatePrinter.Introspection.IntroSpector.PrintObject(Object objectToPrint, String rootname)
       at StatePrinter.Stateprinter.PrintObject(Object objectToPrint, String rootname)
  InnerException: 
       HResult=-2146233088
       Message=An identifier field is not present in this artifact's field collection.
       Source=kCura.Relativity.Client
       StackTrace:
            at MyApplication.DTOs.Artifact.get_TextIdentifier()
       InnerException: 
kbilsted commented 8 years ago

Thanks for the report. It is a situation a have not even considered could happen:) feel free to submit a pr

kbilsted commented 5 years ago

Have you given any thoughts on what to present in the output?

perhaps a general exception filter can be provided so you have a fighting chance of either failing (as the current behavior) or implement your own kind of output given knowledge of the field type.

Your thoughts are welcome