title: Less verbose way to log errors in Objective-C
body: <p>Many Cocoa methods take an optional <code>NSError **</code> argument that they use to report errors. Frequently, I find myself using such methods even in circumstances where the only way that an error could occur is through programming error on my part, rather than unexpected runtime conditions. As such, I don't want to write any error-handling code that will do any user-visible things. All I really want to do upon error is log the error (and perhaps crash), while keeping my code as concise and readable as possible.</p>
  
  <p>The trouble is that the 'keep the code concise' and 'log the error' objectives are in tension with each other. I frequently have a choice between these two approaches, both of which I dislike:</p>
  
  <p><strong>1. Pass NULL for the error pointer argument.</strong></p>
  
  <pre><code>[managedObjectContext save:NULL];
  </code></pre>
  
  <ul>
  <li>Advantages: Succinct, readable, and makes clear that errors are not expected. Perfectly fine as long as I was correct in my belief that an error here is logically impossible.</li>
  <li>Disadvantages: If I've screwed up and an error <em>does</em> happen, it won't be logged and my debugging will be harder. In some circumstances, I might not even notice the error happened.</li>
  </ul>
  
  <p><strong>2. Pass an <code>NSError **</code> and log the resulting error, with the same boilerplate code, every single time.</strong></p>
  
  <pre><code>NSError *error;
  [managedObjectContext save:&amp;error];
  if (error) {
      NSLog(@"Error while saving: %@", error);
  }
  </code></pre>
  
  <ul>
  <li>Advantages: Error don't pass silently - I'm alerted to them and given debugging information.</li>
  <li>Disadvantages: It's horribly verbose. It's slower to write, slower to read, and when it's nested within some levels of indentation already, I feel that it makes the code less readable. Routinely doing this just for logging errors, and getting used to skipping over the boilerplate while reading, also makes me sometimes fail to notice when some code I'm reading actually has significant error handling blocks for errors that are expected to happen at runtime.</li>
  </ul>
  
  <p>Coming from a background of languages like Python, Java, PHP and Javascript, I find it kind of cumbersome to have to write 4 extra lines of boilerplate to get notified of the kind of errors that, in the languages I'm used to, I'd find out about through an exception or warning without ever having to write any code that explicitly checks for errors.</p>
  
  <p>What I'd ideally like is some cunning hack that I can use to automatically log the errors created by these methods without needing to write the boilerplate on every method call, thus giving me the benefits of both the lazy NULL-passing approach and the error-logging boilerplate. In other words, I'd like to write this:</p>
  
  <pre><code>[managedObjectContext save:&amp;magicAutologgingError];
  </code></pre>
  
  <p>and know that if the method created an <code>NSError</code>, it would somehow, magically, be logged.</p>
  
  <p>I'm not too sure how to go about this. I considered using an <code>NSError</code> subclass that logs itself upon <code>dealloc</code>, but realised that since I'm not responsible for instantiating the error objects that Cocoa's methods create, my subclass wouldn't be used anyway. I considered using method swizzling to make <em>all</em> <code>NSError</code>s log themselves on <code>dealloc</code> like this, but I'm not sure if that would actually be desirable. I thought about using some sort of observer class that watches a given constant space in memory that I could use for the <code>NSError</code> pointers that I want to log, but as far as I know there's no way to do anything like KVO for observing arbitrary spaces in memory, so I can't see an approach for implementing this other than having a thread that repeatedly checks for errors to log.</p>
  
  <p>Can anyone see a way to achieve this?</p>