Search code examples
perlcatalyst

How to suppress/downgrade "Unknown resource" errors from Catalyst


By default, Catalyst::Dispatch issues an error when an unknown resource (essentially a 404 error, by my understanding) is requested:

Delegate the dispatch to the action that matched the url, or return a message about unknown resource

And my application's log file shows:

Mar 30 10:19:08 mail myapp: Unknown resource "user/soapCaller.bs"
Mar 30 16:20:38 mail myapp: Unknown resource "HTTP/1.1"
Mar 30 16:20:38 mail myapp: Unknown resource "index.php"

Arguably, this seems like a bug to me; a simple 404 shouldn't really be considered on par with with a "Unable to connect to database" error, but that's beside the point. My question is:

How can I change this behavior?

I have had limited success with creating a simple default() action in Root.pm:

sub default : Private {
    my ( $self, $c ) = @_;
}

This successfully silences the errors to the log file, however, I have had no success writing my own log message/priority. None of the following attempts to output my own warning succeed.

sub default : Private {
    my ( $self, $c ) = @_;
    warn "Foo\n";
    $c->log->debug('Bar');
    $c->log->warn('Baz');
}

So what is the best approach to define my own behavior for "unknown resources" in a Catalyst application?


Solution

  • The problem was that I had an auto() action, which ran before default(), and redirected to another page.

    sub auto : Private {
        my ( $self, $c ) = @_;
        ...
        if ( ! $c->user_exists ) {
            $c->redirect( $c->uri_for('/login') );
        }
    }
    

    I was able to resolve this by adding:

        return 1 if $c->action eq 'default';
    

    before the redirection.