title: How can I get structured exceptions from Moose?
body: <p>Consider this simple class:</p>
  
  <pre><code>package Foo;
  use Moose;
  has foo =&gt; ( is =&gt; 'rw', isa =&gt; 'Int' );
  </code></pre>
  
  <p>And then this code:</p>
  
  <pre><code>use Try::Tiny;
  use Foo;
  my $f = try {
      Foo-&gt;new( foo =&gt; 'Not an Int' );
  }
  catch {
      warn $_;
  };
  </code></pre>
  
  <p>The code dies with a nice big error message about type constraints failing.</p>
  
  <p>I'd like to be able to extract what attribute failed (<code>foo</code>), what the reason was (failed type constraint) and what the value passed was (<code>Not an Int</code>) without having to parse an error string to get the info. </p>
  
  <p>Something like this:</p>
  
  <pre><code>catch {
      if( $_-&gt;isa( 'MooseX::Exception::TypeConstraint' ) ) {
           my $attrib = $_-&gt;attribute;
           my $type   = $_-&gt;type;
           my $value  = $_-&gt;bad_value;
  
           warn "'$value' is an illegal value for '$attrib'.  It should be a $type\n"; 
      }
      else {
           warn $_;
      }
  };
  </code></pre>
  
  <p>Is this possible?  Is there a MooseX distribution that can make this happen? Better yet, is there some Moose feature that I missed that will make this possible?</p>
  
  <p><strong>Update:</strong> I am particularly interested in type constraints, but other Moose errors would be very good as well.  I am also aware that I can throw objects with <code>die</code>.  So, structuring exceptions in code I write is relatively easy.</p>