Skip to content

Commit

Permalink
Made $session->log->fatal() message pass through
Browse files Browse the repository at this point in the history
  • Loading branch information
patspam committed Apr 11, 2010
1 parent 716bdae commit 42c1a8e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 51 deletions.
4 changes: 2 additions & 2 deletions app.psgi
Expand Up @@ -13,8 +13,8 @@ builder {
# Open/close the WebGUI::Session at the outer-most onion layer
enable '+WebGUI::Middleware::Session',
config => $wg->config;#,
#error_docs => { 500 => "$root/www/maintenance.html" };
config => $wg->config,
error_docs => { 500 => "$root/www/maintenance.html" };

# Return the app
$wg;
Expand Down
6 changes: 6 additions & 0 deletions lib/WebGUI/Exception.pm
Expand Up @@ -264,6 +264,12 @@ use Exception::Class (
description => "Couldn't establish a connection.",
fields => [qw{ resource }],
},


'WebGUI::Error::Fatal' => {
isa => 'WebGUI::Error',
description => "Fatal error that should be shown to all site visitors.",
},

);

Expand Down
39 changes: 0 additions & 39 deletions lib/WebGUI/Middleware/Debug.pm

This file was deleted.

36 changes: 36 additions & 0 deletions lib/WebGUI/Middleware/HTTPExceptions.pm
@@ -0,0 +1,36 @@
package WebGUI::Middleware::HTTPExceptions;
use strict;
use parent qw(Plack::Middleware::HTTPExceptions);

=head1 NAME
WebGUI::Middleware::HTTPExceptions - Converts Exceptions into HTTP Errors
=head1 DESCRIPTION
This is PSGI middleware for WebGUI that detects exceptions and turns
them into HTTP Errors. This class is a subclass of L<Plack::Middleware::HTTPExceptions>
=cut

use Carp ();
use Try::Tiny;
use Scalar::Util 'blessed';
use HTTP::Status ();

sub transform_error {
my $self = shift;
my ($e, $env) = @_;

# Handle WebGUI::Error::Fatal errors specially, since unlike most 500
# errors we actually want the user to see the error message (generated by
# $session->log->fatal)
if (blessed $e && $e->isa('WebGUI::Error::Fatal')) {
my $message = $e->message;
return [ 500, [ 'Content-Type' => 'text/html', 'Content-Length' => length($message) ], [ $message ] ];
} else {
$self->SUPER::transform_error(@_);
}
}

1;
15 changes: 7 additions & 8 deletions lib/WebGUI/Middleware/Session.pm
Expand Up @@ -6,7 +6,7 @@ use WebGUI::Session;
use Try::Tiny;
use Plack::Middleware::StackTrace;
use Plack::Middleware::Debug;
use Plack::Middleware::HTTPExceptions;
use WebGUI::Middleware::HTTPExceptions;
use Plack::Middleware::ErrorDocument;

use Plack::Util::Accessor qw( config error_docs );
Expand All @@ -31,20 +31,19 @@ and not worry about closing it.
sub call {
my ( $self, $env ) = @_;

my $app = $self->app;
my $app = $self->app;
my $config = $self->config or die 'Mandatory config parameter missing';

my $session = try {
$env->{'webgui.session'} = WebGUI::Session->open( $config->getWebguiRoot, $config, $env );
};

if (!$session) {

if ( !$session ) {

# We don't have access to a db connection to find out if the user is allowed to see
# a verbose error message or not, so resort to a generic Internal Server Error
# (using the error_docs mapping)
return Plack::Middleware::ErrorDocument->wrap(
sub { [ 500, [], [] ] },
%{ $self->error_docs } )->($env);
return Plack::Middleware::ErrorDocument->wrap( sub { [ 500, [], [] ] }, %{ $self->error_docs } )->($env);
}

my $debug = $session->log->canShowDebug;
Expand All @@ -55,7 +54,7 @@ sub call {
}

# Turn exceptions into HTTP errors
$app = Plack::Middleware::HTTPExceptions->wrap($app);
$app = WebGUI::Middleware::HTTPExceptions->wrap($app);

# HTTP error document mapping
if ( !$debug && $self->error_docs ) {
Expand Down
6 changes: 4 additions & 2 deletions lib/WebGUI/Session/ErrorHandler.pm
Expand Up @@ -257,8 +257,10 @@ If this problem persists, please contact us with what you were trying to do and
END_HTML
}

# Fatal errors cause an exception to be thrown
WebGUI::Error->throw( error => $error );
# Fatal errors cause an exception to be thrown - use WebGUI::Error::Fatal so
# that WebGUI knows to show this error message to all site users (instead of showing
# non-debug users the generic error screen)
WebGUI::Error::Fatal->throw( error => $error );
}


Expand Down

0 comments on commit 42c1a8e

Please sign in to comment.