[Catalyst] Trapping added errors with the correct caller

Robert Rothenberg robrwo at gmail.com
Thu Oct 11 12:38:48 GMT 2012


I would like to trap every error added to $c->error() and log it, noting the
caller (filename, line number) in the logs.

I've not gotten Catalyst::Plugin::ErrorCatcher to work, so I wrote my own
plugin that overrides $c->error with the following method:

  use MRO::Compat;
  use namespace::autoclean;

  sub error {
    my ($c, @args) = @_;

    foreach my $arg (@args) {
        if ($arg) {

            $c->log->error($arg);
        }
    }

    return $c->next::method(@args);
 }

But this only logs errors as coming from my plugin.

Using Sub::Uplevel or fiddling with $Log::Dispatch::CallerDepth or
$Catalyst::Plugin::Log::Dispatch::CallerDepth doesn't seem to work.

I also tried writing the plugin as a Moose::Role that adds my trap before
error, but then it claims to be from one of the internal Moose classes in my
logs.

I can manually get the caller using caller(0) and add them to the log
messages, but that's a bit clumsy (and overrides log formats that don't
include the information).

So... what is the best practice for trapping errors in a way that preserves
caller information?




More information about the Catalyst mailing list