title: Design pattern for including errors with return values
body: <p>I'm writing an add-in for another piece of software through its API. The classes returned by the API can only be access through the native software and the API. So I am writing my own stand alone POCO/DTO objects which map to the API classes. I'm working on a feature which will read in a native file, and return a collection of these POCO objects which I can stole elsewhere. Currently I'm using JSON.NET to serialize these classes to JSON if that matters.</p>
  
  <p>For example I might have a DTO like this</p>
  
  <pre><code>public class MyPersonDTO
  {
      public string Name {get; set;}
      public string Age {get; set;}
      public string Address {get; set;}       
  }
  </code></pre>
  
  <p>..and a method like this to read the native "Persons" into my DTO objects</p>
  
  <pre><code>public static class MyDocReader
  {
      public static IList&lt;MyPersonDTO&gt; GetPersons(NativeDocument doc)
      {
          //Code to read Persons from doc and return MyPersonDTOs
      }
  }
  </code></pre>
  
  <p>I have unit tests setup with a test file, however I keep running into unexpected problems when running my export on other files. Sometimes native objects will have unexpected values, or there will be flat out bugs in the API which throw exceptions when there is no reason to.</p>
  
  <p>Currently when something "exceptional" happens I just log the exception and the export fails. But I've decided that I'd rather export what I can, and record the errors somewhere. </p>
  
  <p>The easiest option would be to just log and swallow the exceptions and return what I can, however then there would be no way for my calling code to know when there was a problem.</p>
  
  <p>One option I'm considering is returning a dictionary of errors as a separate out parameter. The key would identify the property which could not be read, and the value would contain the details of the exception/error.</p>
  
  <pre><code>public static class MyDocReader
  {
      public static IList&lt;MyPersonDTO&gt; persons GetPersons(NativeDocument doc, out IDictionary&lt;string, string&gt; errors)
      {
          //Code to read persons from doc
      }
  }
  </code></pre>
  
  <p>Alternatively I was also considering just storing the errors in the return object itself. This inflates the size of my object, but has the added benefit of storing the errors directly with my objects. So later if someone's export generates an error, I don't have to worry about tracking down the correct log file on their computer.</p>
  
  <pre><code>public class MyPersonDTO
  {
      public string Name {get; set;}
      public string Age {get; set;}
      public string Address {get; set;}
  
      public IDictionary&lt;string, string&gt; Errors {get; set;}   
  }
  </code></pre>
  
  <p>How is this typically handled? Is there another option for reporting the errors along with the return values that I'm not considering?</p>