wanna_code_perl has asked for the wisdom of the Perl Monks concerning the following question:

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?