O, wise ones!

I have worked long and hard (well, for about 3 hours) and climbed high mountains (this technopark sits atop a little hill) to produce yet another useless module: Error::Layout. I would like to hear some feedback on the API given below. I would also appreciate pointers on the right way to do this sort of thing, probably mentioning Exception::Class and general OO advice. I will put those pointers in a DISCLAIMER/CAVEATS section, which could make the module useful after all.

NAME

Error::Layout - Helps manage error handlers in your application


VERSION

Version 0.01_01


SYNOPSIS

    package MyApp::Err;
    use Error::Layout;
    package MyApp::Foo;
    use MyApp::Err;  # exports handle_err()
    if ($error_condition) {
        handle_err();
    }


DESCRIPTION

This module tries to help you organize your error handling code and make your main logic code look less cluttered.

You use Error::Layout in a central error-handling module of your application:

    package MyApp::Err;
    use Error::Layout;
    sub handle_err { ... } # Optional default handler

Then, in any ordinary module of your application where error-handling code seems to crowd out the main logic, you can use MyApp::Err, the error-handling module you created:

    package MyApp::Foo;
    use MyApp::Err;
    eval {
        # Risky business
    };
    if ($@) {
        handle_err();
    }

in which case Error::Layout will try to find a sub named handle_err() in MyApp::Foo::Err and MyApp::Err, in that order.

    package MyApp::Foo::Err;
    sub handle_err {
        ...
    }

If you want to pass arguments to your handler, you can use an anonymous hash:

    handle_err({ FOO => $foo });

If it is interested in the arguments, your handler should start by unloading the arguments hashref:

    sub handle_err {
        my (args_href) = @_;
        ...
    }

Your handler will always be passed an args_href, even if you did not call it with one, and it will always have $@, the eval error, saved as the value for the key '_EVAL_ERR'

    sub handle_err {
        my (args_href) = @_;
        my $eval_err = $args_href->{_EVAL_ERR};
        ...
    }

If you have multiple handlers in one module, or just want to put the handler in a module that is named in a way that reflects its job, you can pass a module identifier (the name of the handler's separate module) to handle_err as the first argument:

    package MyApp::Foo;
    use MyApp::Err;  # exports handle_err()
    if ($error_condition_1) {
        handle_err('Condition_One');
    }
    ...
    if ($error_condition_2) {
        handle_err('Condition_Two', {FOO => $foo});
    }

For the call handle_err('Condition_One'), Error::Layout will look for an implementation of handle_err in MyApp::Foo::Err::Condition_One and MyApp::Err::Condition_One, in that order. Any parameters hashref should come after the identifier, as shown in the 'Condition Two' example above.

If you want your error handlers to be named something other than 'handle_err', you should pass your desired name to Error::Layout:

    use Error::Layout 'myapp_err';

Then, you should replace 'handle_err' with 'myapp_err' in all the examples above.


In reply to Error::Layout - the incredible disappearing catcher. by fenLisesi

Title:
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, 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:
    & &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.