Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I'm converting a small (~200 SLOC) OO module to use Mouse, for some comparative benchmarking and other educational tomfoolery. I wrote it originally with manual OO. Nothing fancy. But I'm not sure how to implement my existing error handling mechanism with Mouse::Util::TypeConstraints. My error handler is configurable to either carp(), or set $obj->error string, or both. As far as I can tell, M::U::TypeConstraints croak()s on everything and that's it. So, going from this:

# Previous (manual OO) code: sub error_mode { + my ($s, $mode) = @_; + if ($mode) { + $s->_err('Invalid error_mode', $s->{error_mode}) + unless $mode =~ /^(carp|error|both)$/; + $s->{error_mode} = $mode; + } + + $s->{error_mode}; + }

... to this:

# Converted to Mouse: enum 'ErrorMode' => qw<carp error both>; has 'error_mode' => ( is => 'rw', isa => 'ErrorMode', default => 'erro +r' );

... this works in the sense that $obj->error_mode($new_mode) correctly sets the mode, but if $new_mode is invalid, it outputs a fatal mess like this:

Attribute (error_mode) does not pass the type constraint because: Vali +dation failed for 'ErrorMode' with value at /usr/local/lib/x86_64-li +nux-gnu/perl/5.26.1/Mouse/ line 395. Mouse::Util::throw_error(Mouse::Meta::Attribute=HASH(0x56165c0 +b5528), "Attribute (error_mode) does not pass the type constraint bec +a"..., "data", "", "depth", -1) called at ./t/05-errors.t line 16

I know I can use message to have a slightly more readable error message, but the main problem is of course the fatal exceptions with no way that I can see to implement any other kind of error handling. Of course I know I don't have to use Mouse::Util::TypeConstraints, but then it starts to look identical to my manual OO code. That or I guess I could wrap the accessor/mutator with a method that does eval { $self->_error_mode(@_) } and handles the errors from there, but that's, well, gross, especially given there are half a dozen methods to do this with. Is there a middle ground?

In reply to Non-fatal error handling with Mouse types? by wanna_code_perl

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (6)
    As of 2021-01-16 11:58 GMT
    Find Nodes?
      Voting Booth?