Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://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/Util.pm 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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found