[Catalyst-commits] r10818 -
trunk/misc/exception_test_case_simplest/lib
ferz at dev.catalyst.perl.org
ferz at dev.catalyst.perl.org
Tue Jul 7 17:21:01 GMT 2009
Author: ferz
Date: 2009-07-07 17:21:01 +0000 (Tue, 07 Jul 2009)
New Revision: 10818
Modified:
trunk/misc/exception_test_case_simplest/lib/TestAppClassException.pm
Log:
Stripped down more lines
Modified: trunk/misc/exception_test_case_simplest/lib/TestAppClassException.pm
===================================================================
--- trunk/misc/exception_test_case_simplest/lib/TestAppClassException.pm 2009-07-07 16:39:09 UTC (rev 10817)
+++ trunk/misc/exception_test_case_simplest/lib/TestAppClassException.pm 2009-07-07 17:21:01 UTC (rev 10818)
@@ -9,13 +9,9 @@
my %classes = (
'TestAppClassException::Exception' => {
description => 'Generic exception',
- fields => [ qw( headers status status_message payload ) ],
+ fields => [ qw( status ) ],
alias => 'throw'
},
- 'TestAppClassException::Exception::BadRequest' => {
- isa => 'TestAppClassException::Exception',
- description => '400 - Bad request',
- },
);
my @exports = grep { defined } map { $classes{ $_ }->{ alias } } keys %classes;
@@ -39,164 +35,13 @@
use HTTP::Headers ();
use HTTP::Status ();
use Scalar::Util qw( blessed );
+#use parent 'Exception::Class';
-sub headers {
- my $self = shift;
- my $headers = $self->{headers};
- unless ( defined $headers ) {
- return undef;
- }
-
- if ( blessed $headers && $headers->isa('HTTP::Headers') ) {
- return $headers;
- }
-
- if ( ref $headers eq 'ARRAY' ) {
- return $self->{headers} = HTTP::Headers->new( @{ $headers } );
- }
-
- if ( ref $headers eq 'HASH' ) {
- return $self->{headers} = HTTP::Headers->new( %{ $headers } );
- }
-
- AraException->throw(
- message => qq(Can't coerce a '$headers' into a HTTP::Headers instance.)
- );
-}
-
sub status {
return $_[0]->{status} ||= 500;
}
-sub is_info {
- return HTTP::Status::is_info( $_[0]->status );
-}
-
-sub is_success {
- return HTTP::Status::is_success( $_[0]->status );
-}
-
-sub is_redirect {
- return HTTP::Status::is_redirect( $_[0]->status );
-}
-
-sub is_error {
- return HTTP::Status::is_error( $_[0]->status );
-}
-
-sub is_client_error {
- return HTTP::Status::is_client_error( $_[0]->status );
-}
-
-sub is_server_error {
- return HTTP::Status::is_server_error( $_[0]->status );
-}
-
-sub status_line {
- return sprintf "%s %s", $_[0]->status, $_[0]->status_message;
-}
-
-sub status_message {
- return $_[0]->{status_message} ||= HTTP::Status::status_message( $_[0]->status );
-}
-
-my %messages = (
- 400 => 'Browser sent a request that this server could not understand.',
- 401 => 'The requested resource requires user authentication.',
- 403 => 'Insufficient permission to access the requested resource on this server.',
- 404 => 'The requested resource was not found on this server.',
- 405 => 'The requested method is not allowed.',
- 500 => 'The server encountered an internal error or misconfiguration and was unable to complete the request.',
- 501 => 'The server does not support the functionality required to fulfill the request.',
-);
-
-sub public_message {
- return $messages{ $_[0]->status } || 'An error occurred.';
-}
-
-sub as_public_html {
- my $self = shift;
-
- my $title = shift || $self->status_line;
- my $header = shift || $self->status_message;
- my $message = shift || $self->public_message;
-
-return <<EOF;
-<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
-<html>
- <head>
- <title>$title</title>
- </head>
- <body>
- <h1>$header</h1>
- <p>$message</p>
- </body>
-</html>
-EOF
-
-}
-
-sub has_headers {
- return defined $_[0]->{headers} ? 1 : 0;
-}
-
-sub has_payload {
- return defined $_[0]->{payload} && length $_[0]->{payload} ? 1 : 0;
-}
-
-sub has_status_message {
- return defined $_[0]->{status_message} ? 1 : 0;
-}
-
-sub full_message {
- my $self = shift;
- my $message = $self->message;
-
- if ( $self->has_payload ) {
- $message .= sprintf " %s.", $self->payload;
- }
-
- return $message;
-}
-
-
-package TestAppClassException::Exception::FileNotFound;
-
-sub status {
- return $_[0]->{status} ||= 404;
-}
-
-package TestAppClassException::Exception::AccessDenied;
-
-sub status {
- return $_[0]->{status} ||= 401;
-}
-
-package TestAppClassException::Exception::InsufficientPermission;
-
-sub status {
- return $_[0]->{status} ||= 403;
-}
-
-package TestAppClassException::Exception::PreconditionFailed;
-
-sub status {
- return $_[0]->{status} ||= 412;
-}
-
-package TestAppClassException::Exception::SeeOther;
-
-sub status {
- return $_[0]->{status} ||= 303;
-}
-
-package TestAppClassException::Exception::BadRequest;
-
-sub status {
- return $_[0]->{status} ||= 400;
-}
-
#########
package TestAppClassException;
@@ -204,15 +49,12 @@
use strict;
use warnings;
-
-#use TestAppClassException::Exceptions;
-
use Scalar::Util ();
use Catalyst::Runtime '5.80';
-use Catalyst qw/ -Debug StackTrace /;
+use Catalyst qw/ /;
-our $VERSION = '0.01';
+our $VERSION = '0.02';
# Start the application
__PACKAGE__->setup;
@@ -244,74 +86,5 @@
=cut
-sub finalize {
- my ( $c ) = shift;
- $c->handle_exception if @{ $c->error };
- $c->maybe::next::method( @_ );
-}
-sub handle_exception {
- my( $c ) = @_;
- my $error = $c->error->[ 0 ];
-
- $c->log->debug("baseurl:" . $c->stash->{baseurl});
-
- if( ! Scalar::Util::blessed( $error ) or !$error->isa( 'TestAppClassException::Exception' ) ) {
- $error = TestAppClassException::Exception->new( message => "$error" );
- }
-
- # handle debug-mode forced-debug from RenderView
- if( $c->debug && $error->message =~ m{^forced debug} ) {
- return;
- }
-
- $c->clear_errors;
-
- if ( $error->is_error ) {
- $c->response->headers->remove_content_headers;
- }
-
- if ( $error->has_headers ) {
- $c->response->headers->merge( $error->headers );
- }
-
- # log the error
- if ( $error->is_server_error ) {
- $c->log->error( $error->as_string );
- }
- elsif ( $error->is_client_error ) {
- $c->log->warn( $error->as_string ) if $error->status =~ /^40[034]$/;
- }
-
- if( $error->is_redirect ) {
- # recent Catalyst will give us a default body for redirects
-
- if( $error->can( 'uri' ) ) {
- $c->response->redirect( $error->uri( $c ) );
- }
-
- return;
- }
-
- $c->log->debug($error);
- $c->log->debug($error->as_public_html );
-
- $c->response->status( $error->status );
- $c->response->content_type( 'text/html; charset=utf-8' );
- $c->response->body(
- $c->view( 'TT' )->render( $c, 'error.tt2', { baseurl=> $c->stash->{baseurl}, error => $error, message=>$error->as_public_html } )
- );
-
- # processing the error has bombed. just send it back plainly.
- $c->response->body( $error->as_public_html ) if $@;
-}
-
-$SIG{ __DIE__ } = sub {
- return if Scalar::Util::blessed( $_[ 0 ] );
- # don't inflate detachments
- return if $_[ 0 ] eq $Catalyst::DETACH;
- TestAppClassException::Exception->throw( message => join '', @_ );
-};
-
-
1;
More information about the Catalyst-commits
mailing list