=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 );
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 /;
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
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');
}
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');
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 );
}
$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
return Text::Tradition->new( $opts );
}
-=head2 end
-
-Attempt to render a view, if needed.
-
-=cut
-
-sub end : ActionClass('RenderView') {}
=head1 AUTHOR