I've been musing over how to report errors from a module I'm writing. Specifically, when to issue warnings and when to throw exceptions (
warn vs.
die, or better
Carp::carp vs.
Carp::croak). There are various ways of doing this:
- Everything is an exception - stick to the documented interface and give sane values, or we're outta here.
- Everything is a warning - Hey, we're easy man, you wanna try this, I'm cool, but I'm not playing along. Peace, brother.
- Mixing stuff - Hah, you thought this would be no problem: wrong, bozo. I'm outta here.
Each of these options has its disadvantages (somewhat exemplified by the italicised comments), be it that they are (respectively) too strict, not strict enough and confusing
So, I'm now making this behaviour configurable. Something like (for an OO inspired module):
package Foo;
# ...
do_tricky_stuff or $self->_complain("Tricky stuff failed");
do_unrecoverable_stuff or return $self->_complain("Grave problem");
# ...
sub _complain {
my $self=shift;
my $msg=shift;
croak($msg) if $self->fatal_warnings();
carp($msg);
return undef;
}
This allows for exceptions (everything is an exception) or recoverable / non recoverable warnings (non recoverable return undef), depending on the (user settable) value of an instance attribute.
I'm looking for some input on how other people handle this.
CU
Robartes-
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.