6

in my stage server I would like to activate the debug so the clients can find errors for themselves before the app goes to the production server.

BUT I only want the first part of the message, not the Request, or the Session Data.

For example: Couldn't render template "templates/home.tt2: file error - templates/inc/heater: not found".

The message is enough for me and for my client to see that the "header" call is misspelled.

The Request has a lot of irrelevant information for the client, but also has A LOT of internal developing information that should be hidden all the time!!

Regards

3 Answers 3

3

What you want is to override Catalyst's dump_these method. This returns a list of things to display on Catalyst's error debugging page.

The default implementation looks like:

sub dump_these {
    my $c = shift;
    [ Request => $c->req ],
    [ Response => $c->res ],
    [ Stash => $c->stash ],
    [ Config => $c->config ];
}

but you can make it more restrictive, for example

sub dump_these {
    my $c = shift;
    return [ Apology => "We're sorry that you encountered a problem" ],
           [ Response => substr($c->res->body, 0, 512) ];
}

You would define dump_these in your app's main module -- the one where you use Catalyst.

4
  • Great!, but in the usual MyApp.pm file, after the "use Catalyst..." all I have are package configurations, like PACKAGE->config( encoding => 'UTF-8' ) So I do not know how to insert here a subroutine. I tried to paste the code as it is, just in case it rings the bell, but it doesn't work.
    – Nacho B
    Commented Sep 26, 2013 at 22:13
  • 1
    That should be right. Then you'd want dump_these defined in the MyApp package. If you're not sure what package this method belongs in, and you can tolerate a Subroutine redefined ... warning, you could also define it like sub Catalyst::dump_these { ... }.
    – mob
    Commented Sep 27, 2013 at 15:53
  • Hi, @mob,I tried again. Now I can remove the Stash and the Config blocks. I can add blocks like your "Apology" suggestion. But If I remove (or just change) Response, the debug screen appears, but in the console I get an exception in engine Can't call method "status" on an undefined value.... If I remove the Request block i get a total crash exception, with no screen and Can't call method "method".... Any suggestion?
    – Nacho B
    Commented Oct 4, 2013 at 17:54
  • Hmmmmm. The log_request and log_response methods in Catalyst also use dump_these and expect to get back values for Request and Response. Next suggestion: hack the finalize_error or _dump_error_page_element methods in the Catalyst::Engine module.
    – mob
    Commented Oct 4, 2013 at 19:19
2

I had a similar problem that I solved by overriding the Catalyst method log_request_parameters.

Something like this (as @mob said, put it in your main module):

sub log_request_parameters {
    my $c          = shift;
    my %all_params = @_; 

    my $copy = Clone::clone(\%all_params);  # don't change the 'real' request params

    # Then, do anything you want to only print what matters to you,
    # for example, to hide some POST parameters:
    my $body = $copy->{body} || {}; 
    foreach my $key (keys %$body) {
        $body->{$key} = '****' if $key =~ /password/;
    }

    return $c->SUPER::log_request_parameters( %$copy );
}

But you could also simply return at the beginning, if you don't want any GET/POST parameters displayed.

0

Well, I didn't think of the more obvious solution, in your case: you could simply set your log level to something higher than debug, which would prevent these debug logs from being displayed, but would keep the error logs:

    # (or a similar condition to check you are not on the production server)
    if ( !__PACKAGE__->config->{dev} ) {
        __PACKAGE__->log->levels( 'warn', 'error', 'fatal' ) if ref __PACKAGE__->log;
    }

Not the answer you're looking for? Browse other questions tagged or ask your own question.