title: How should one implement "is.error()" for R, to identify and parse errors?
body: <p>I am trying to test if objects are the results of errors.  The use case primarily arises via a <code>foreach()</code> loop that produces an error (although, for testing, it seems enough to just assign a <code>simpleError()</code> to a variable), and I'm puzzled about how to identify when that has occurred: how can I test that a given object is, in fact, an error?  Once I've determined that it is an error, what else can I extract, besides a message?  Perhaps I'm missing something about R's error handling facilities, as it seems necessary to write an error object testing function <em>de novo</em>.  </p>
  
  <p>Here are two examples, one using <code>foreach</code>, with the <code>.errorhandling</code> argument set to <code>pass</code>.  I have begun to use that as the default for large scale or unattended processing, in the event of an anomaly in a slice of data.  Such anomalies are rare, and not worth crashing the entire for loop (especially if that anomaly occurs at the end, which appears to be the default behavior of my <code>murphysListSortingAlgorithm()</code> ;-)). Instead, <em>post hoc</em> detection is desired.</p>
  
  <pre><code>library(foreach)
  library(doMC)
  registerDoMC(2)
  results = foreach(ix = 1:10, .errorhandling = "pass") %dopar%{
      if(ix == 6){
          stop("Perfect")
      } 
      if(ix == 7){
          stop("LuckyPrime")
      } else {
          return(ix)
      }
  }
  </code></pre>
  
  <p>For simplicity, here is a very simple error (by definition):</p>
  
  <pre><code>a = simpleError("SNAFU")
  </code></pre>
  
  <p>While there does not seem to be a command like <code>is.error()</code>, and commands like <code>typeof()</code> and <code>mode()</code> seem to be pointless, the best I've found is to use <code>class()</code> or <code>attributes()</code>, which give attributes that are indicative of an error.  How can I use these in a manner guaranteed to determine that I've got an error and to fully process that error?  For instance <code>a$message</code> returns <code>SNAFU</code>, but <code>a$call</code> is <code>NULL</code>.  Should I expect to be able to extract anything useful from, say, <code>res[[6]]$call</code>?</p>
  
  <hr>
  
  <p>Note 1: In case one doesn't have multicore functionality to reproduce the first example, I should point out that <code>results[[6]]</code> isn't the same as <code>simpleError("Perfect")</code>:</p>
  
  <pre><code>&gt; b = simpleError("Perfect")
  &gt; identical(results[[6]], b)
  [1] FALSE
  &gt; results[[6]]
  &lt;simpleError in eval(expr, envir, enclos): Perfect&gt;
  &gt; b
  &lt;simpleError: Perfect&gt;
  </code></pre>
  
  <p>This demonstrates why I can't (very naively) test if the list element is a vanilla <code>simpleError</code>.</p>
  
  <p>Note 2.  I am aware of <code>try</code> and <code>tryCatch</code>, and use these in some contexts.  However, I'm not entirely sure how I can use them to post-process the output of, say, a <code>foreach</code> loop.  For instance, the <code>results</code> object in the first example: it does not appear to me to make sense to process its elements with a <code>tryCatch</code> wrapper.  For the RHS of the operation, i.e. the <code>foreach()</code> loop, I'm not sure that <code>tryCatch</code> will do what I intend, either.  I can use it to catch an error, but I suppose I need to get the message and insert the processing at that point.  I see two issues: every loop would need to be wrapped with a <code>tryCatch()</code>, negating part of the <code>.errorhandling</code> argument, and I remain unable to later post-process the <code>results</code> object.  If that's the only way to do this processing, then it's the solution, but that implies that errors can't be identified and processed in a similar way to many other R objects, such as matrices, vectors, data frames, etc.</p>
  
  <hr>
  
  <p>Update 1.  I've added an additional stop trigger in the <code>foreach</code> loop, to give two different messages to identify and parse, in case this is helpful.</p>
  
  <p>Update 2.  I'm selecting Richie Cotton's answer.  It seems to be the most complete explanation of what I should look for, though a complete implementation requires several other bits of code (and a recent version of R). Most importantly, he points out that there are 2 types of errors we need to keep in mind, which is especially important in being thorough.  See also the comments and answers by others in order to fully develop your own <code>is.error()</code> test function; the answer I've given can be a useful start when looking for errors in a list of results, and the code by Richie is a good starting point for the test functions.</p>