Catalyst::ControllerRole::CatchErrors

Synopsis

Todays calendar entry explains how to deal with uncaught errors in your application using Catalyst::ControllerRole::CatchErrors.

Flexibility

If your Catalyst application throws an error the default behaviour is to generate a HTML error page saying something along the lines of "Sorry, there was a server error!".

If debugging is enabled this page is different and shows the error message and optionally a stacktrace.

Catalyst::ControllerRole::CatchErrors allows you to do custom error handling. The following scenarios come to mind.

Customize the error page layout.

The Catalyst error page comes with its own CSS and looks entirely different then your page design. See also Catalyst::Plugin::CustomErrorMessage.

Customize the error page content.

Perhaps you want to give some information to your users so they get a clue what went wrong.

Send a non-HTML error response.

If you have a REST application you don't want to send text/html default errors.

Rethrow an error

Maybe you want to catch some errors and let others be handled by the default mechanism. Catalyst::ControllerRole::CatchErrors lets you do that too. Just rethrow it.

Example

    package MyApp::Controller::Root;
    use Moose;
    BEGIN { extends 'Catalyst::Controller' }
    with 'Catalyst::ControllerRole::CatchErrors';

    sub catch_errors : Private {
        my ($self, $c, @errors) = @_;
        # stuff
    }

Conclusion

This module was written as a proof of concept and might be moved into the Catalyst core at some point in the future.

Author

David Schmidt davewood@cpan.org