webgui/lib/WebGUI/Middleware/HTTPExceptions.pm

47 lines
1.2 KiB
Perl

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 ();
=head2 transform_error ($env)
Transforms exceptions of the class WebGUI::Error::Fatal into HTTP 500 error messages, displaying
the contents of the exception to the user.
=head3 $env
A Plack environment hash
=cut
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;