make error page render for microservice controller
Tara L Andrews [Sun, 22 Jan 2012 00:45:04 +0000 (01:45 +0100)]
lib/Text/Tradition.pm
lib/Text/Tradition/Stemma.pm
stemmaweb/Makefile.PL
stemmaweb/lib/stemmaweb/Controller/Microservice.pm
stemmaweb/root/src/error.tt [new file with mode: 0644]

index 8abeb6a..f0edbbe 100644 (file)
@@ -311,8 +311,18 @@ is( $t->stemma, $s, "Stemma is the right one" );
 =cut
 
 sub add_stemma {
-       my( $self, $dot ) = @_;
-       open my $stemma_fh, '<', $dot or warn "Could not open file $dot";
+       my $self = shift;
+       my %opts = @_;
+       my $stemma_fh;
+       if( $opts{'dotfile'} ) {
+               open $stemma_fh, '<', $opts{'dotfile'}
+                       or warn "Could not open file " . $opts{'dotfile'};
+       } elsif( $opts{'dot'} ) {
+               my $str = $opts{'dot'};
+               open $stemma_fh, '<', \$str;
+       }
+       # Assume utf-8
+       binmode $stemma_fh, ':utf8';
        my $stemma = Text::Tradition::Stemma->new( 
                'collation' => $self->collation,
                'dot' => $stemma_fh );
index fbf9ff7..4f98ba1 100644 (file)
@@ -46,8 +46,6 @@ sub BUILD {
 
 sub graph_from_dot {
        my( $self, $dotfh ) = @_;
-       # Assume utf-8
-       binmode( $dotfh, ':utf8' );
        my $reader = Graph::Reader::Dot->new();
        my $graph = $reader->read_graph( $dotfh );
        if( $graph ) {
index 2fdb6b2..a31e6ed 100644 (file)
@@ -19,6 +19,7 @@ requires 'Catalyst::View::Download::Plain';
 requires 'Catalyst::View::JSON';
 requires 'Catalyst::View::TT';
 requires 'Moose';
+requires 'TryCatch';
 requires 'namespace::autoclean';
 requires 'Config::General'; # This should reflect the config file format you've chosen
                  # See Catalyst::Plugin::ConfigLoader for supported formats
index 4027d4f..3084367 100644 (file)
@@ -2,7 +2,9 @@ package stemmaweb::Controller::Microservice;
 use Moose;
 use namespace::autoclean;
 use JSON;
+use TryCatch;
 use Text::Tradition;
+#use Text::Tradition::Error;
 use Text::Tradition::Stemma;
 use Text::Tradition::StemmaUtil qw/ character_input phylip_pars newick_to_svg /;
 
@@ -45,8 +47,12 @@ Parse the passed collation data and return an SVG of the collated text.
 sub renderSVG :Local :Args(0) {
        my( $self, $c ) = @_;
        my $tradition = _parse_to_tradition( $c->request );
-       $c->stash->{'result'} = $tradition->collation->as_svg;
-       $c->forward('View::SVG');
+       try {
+               $c->stash->{'result'} = $tradition->collation->as_svg;
+               $c->forward('View::SVG');
+       } catch( Text::Tradition::Error $e ) {
+               $c->detach( 'error', [ $e ] );
+       }
 }
 
 =head1 STEMMA / DISTANCE TREE URLs
@@ -62,7 +68,12 @@ Parameter: dot => a string containing the dot description of the stemma.
 sub stemma_svg :Local :Args(0) {
        my( $self, $c ) = @_;
        my $t = Text::Tradition->new();
-       my $stemma = $t->add_stemma( 'dot' => $c->req->param('dot') );
+       my $stemma;
+       try {
+               $stemma = $t->add_stemma( 'dot' => $c->req->param('dot') );
+       } catch( Text::Tradition::Error $e ) {
+               $c->detach( 'error', [ $e ] );
+       }
        $c->stash->{'result'} = $stemma->as_svg;
        $c->forward('View::SVG');
 }
@@ -108,8 +119,6 @@ Exactly one of 'alignment' or 'matrix' must be specified.
 
 sub run_pars :Local :Args(0) {
        my( $self, $c ) = @_;
-       my $error;
-       my $view = 'View::JSON';
        my $matrix;
        if( $c->request->param('matrix') ) {
                $matrix = $c->request->param('matrix');
@@ -118,34 +127,35 @@ sub run_pars :Local :Args(0) {
                my $table = from_json( $c->request->param('alignment') );
                $matrix = character_input( $table );
        } else {
-               $error = "Must pass either an alignment or a matrix";
+               $c->detach( 'error', [ "Must pass either an alignment or a matrix" ] );
        }
        
        # Got the matrix, so try to run pars.
-       my( $result, $output );
-       unless( $error ) {
-               ( $result, $output ) = phylip_pars( $matrix );
-               $error = $output unless( $result );
+       my $output;
+       try {
+               $output = phylip_pars( $matrix );
+       } catch( Text::Tradition::Error $e ) {
+               $c->detach( 'error', [ $e ] );
        }
        
        # Did we want newick or a graph?
-       unless( $error ) {
-               my $format = 'newick';
-               $format = $c->request->param('format') if $c->request->param('format');
-               if( $format eq 'svg' ) {
-                       # Do something
+       my $view = 'View::JSON';
+       my $format = 'newick';
+       $format = $c->request->param('format') if $c->request->param('format');
+       if( $format eq 'svg' ) {
+               # Do something
+               try {
                        $c->stash->{'result'} = newick_to_svg( $output );
                        $view = 'View::SVG';
-               } elsif( $format ne 'newick' ) {
-                       $error = "Requested output format $format unknown";
-               } else {
-                       $c->stash->{'result'} = { 'tree' => $output };
+               } catch( Text::Tradition::Error $e ) {
+                       $c->detach( 'error', [ $e ] );
                }
+       } elsif( $format ne 'newick' ) {
+               $c->detach( 'error', [ "Requested output format $format unknown" ] );
+       } else {
+               $c->stash->{'result'} = { 'tree' => $output };
        }
 
-       if( $error ) {
-               $c->stash->{'error'} = $error;
-       } # else the stash is populated.
        $c->forward( $view );
 }
 
@@ -183,6 +193,23 @@ sub view_svg :Local :Args(0) {
     $c->stash->{template} = 'stemma_gadget.tt';
 }
 
+=head2 error
+
+Default response when actions generate Text::Tradition::Error exceptions
+
+=cut
+
+sub error :Private {
+       my( $self, $c, $error ) = @_;
+       my $errstr = $error;
+       if( ref( $error ) eq 'Text::Tradition::Error' ) {
+               $errstr = $error->ident . ": " . $error->message;
+       }
+       $c->response->code( 500 );
+       $c->stash->{'error'} = $errstr;
+       $c->stash->{'template'} = 'error.tt';
+}
+
 =head2 default
 
 Standard 404 error page
@@ -212,13 +239,6 @@ sub _parse_to_tradition {
        return Text::Tradition->new( $opts );
 }
 
-=head2 end
-
-Attempt to render a view, if needed.
-
-=cut
-
-sub end : ActionClass('RenderView') {}
 
 =head1 AUTHOR
 
diff --git a/stemmaweb/root/src/error.tt b/stemmaweb/root/src/error.tt
new file mode 100644 (file)
index 0000000..260fc84
--- /dev/null
@@ -0,0 +1 @@
+[% error %]
\ No newline at end of file