[Rt-commit] r3163 - in HTTP-Server-Simple-Mason: lib/HTTP/Server/Simple t

glasser at bestpractical.com glasser at bestpractical.com
Wed Jun 15 12:15:24 EDT 2005


Author: glasser
Date: Wed Jun 15 12:15:24 2005
New Revision: 3163

Added:
   HTTP-Server-Simple-Mason/t/04unhandlederrors.t
      - copied, changed from r2482, HTTP-Server-Simple-Mason/t/01live.t
   HTTP-Server-Simple-Mason/t/05handlederrors.t
Modified:
   HTTP-Server-Simple-Mason/lib/HTTP/Server/Simple/Mason.pm
   HTTP-Server-Simple-Mason/t/03podcoverage.t
Log:
Add a "handle error" hook.  Also fix up some of the tests and POD.

Modified: HTTP-Server-Simple-Mason/lib/HTTP/Server/Simple/Mason.pm
==============================================================================
--- HTTP-Server-Simple-Mason/lib/HTTP/Server/Simple/Mason.pm	(original)
+++ HTTP-Server-Simple-Mason/lib/HTTP/Server/Simple/Mason.pm	Wed Jun 15 12:15:24 2005
@@ -52,6 +52,12 @@
     print STDOUT "HTTP/1.0 $status\n";
 };
 
+=head2 mason_handler 
+
+Returns the server's C<HTML::Mason::CGIHandler> object.  The first time
+this method is called, it creates a new handler by calling C<new_handler>.
+
+=cut
 
 sub mason_handler {
     my $self = shift;
@@ -85,9 +91,34 @@
     eval { my $m = $self->mason_handler;
 
         $m->handle_cgi_object($cgi) };
+
+    if ($@) {
+	$self->handle_error($@);
+    } 
 }
 
+=head2 handle_error ERROR
+
+If the call to C<handle_request> dies, C<handle_error> is called with the
+exception (that is, C<$@>).  By default, it does nothing; it can be overriden
+by your subclass.
+
+=cut
 
+sub handle_error {
+    my $self = shift;
+
+    return;
+} 
+
+=head2 new_handler
+
+Creates and returns a new C<HTML::Mason::CGIHandler>, with configuration
+specified by the C<default_mason_config> and C<mason_config> methods.
+You don't need to call this method yourself; C<mason_handler> will automatically
+call it the first time it is called.
+
+=cut
 
 sub new_handler {
     my $self    = shift;

Modified: HTTP-Server-Simple-Mason/t/03podcoverage.t
==============================================================================
--- HTTP-Server-Simple-Mason/t/03podcoverage.t	(original)
+++ HTTP-Server-Simple-Mason/t/03podcoverage.t	Wed Jun 15 12:15:24 2005
@@ -4,6 +4,6 @@
 use Test::More;
 
 eval "use Test::Pod::Coverage 1.04";
-plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" ;#if $@;
+plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@;
 all_pod_coverage_ok({  also_private => [ qr/^[A-Z_]+$/ ], });
 

Copied: HTTP-Server-Simple-Mason/t/04unhandlederrors.t (from r2482, HTTP-Server-Simple-Mason/t/01live.t)
==============================================================================
--- HTTP-Server-Simple-Mason/t/01live.t	(original)
+++ HTTP-Server-Simple-Mason/t/04unhandlederrors.t	Wed Jun 15 12:15:24 2005
@@ -15,7 +15,7 @@
 like($pid, qr/^-?\d+$/,'pid is numeric');
 sleep(1);
 my $content=LWP::Simple::get("http://localhost:13432");
-like($content,qr/2$/,"Returns a page containing only 2");
+is($content,'',"Returns an empty page");
 is(kill(9,$pid),1,'Signaled 1 process successfully');
 
 
@@ -29,9 +29,9 @@
     my $root = File::Spec->catdir(File::Spec->tmpdir, "mason-pages-$$");
     mkdir( $root ) or die $!;
     open (PAGE, '>', File::Spec->catfile($root, 'index.html')) or die $!;
-    print PAGE '<%1+1%>';
+    print PAGE '<%die%>';
     close (PAGE);
-    return ( comp_root => $root );
+    return ( comp_root => $root, error_mode => 'fatal', error_format => 'line' );
 }
 
 1;

Added: HTTP-Server-Simple-Mason/t/05handlederrors.t
==============================================================================
--- (empty file)
+++ HTTP-Server-Simple-Mason/t/05handlederrors.t	Wed Jun 15 12:15:24 2005
@@ -0,0 +1,44 @@
+use Test::More;
+BEGIN {
+    if (eval { require LWP::Simple }) {
+	plan tests => 5;
+    } else {
+	Test::More->import(skip_all =>"LWP::Simple not installed: $@");
+    }
+}
+
+use_ok( HTTP::Server::Simple::Mason);
+
+my $s=MyApp::Server->new(13432);
+is($s->port(),13432,"Constructor set port correctly");
+my $pid=$s->background();
+like($pid, qr/^-?\d+$/,'pid is numeric');
+sleep(1);
+my $content=LWP::Simple::get("http://localhost:13432");
+is($content,'We handled this error',"custom error handler called");
+is(kill(9,$pid),1,'Signaled 1 process successfully');
+
+
+
+
+package MyApp::Server;
+use base qw/HTTP::Server::Simple::Mason/;
+use File::Spec;
+
+sub mason_config {
+    my $root = File::Spec->catdir(File::Spec->tmpdir, "mason-pages-$$");
+    mkdir( $root ) or die $!;
+    open (PAGE, '>', File::Spec->catfile($root, 'index.html')) or die $!;
+    print PAGE '<%die%>';
+    close (PAGE);
+    return ( comp_root => $root, error_mode => 'fatal', error_format => 'line' );
+}
+
+sub handle_error {
+    my $self = shift;
+    my $error = shift;
+
+    print "We handled this error";
+} 
+
+1;


More information about the Rt-commit mailing list