From: Tara L Andrews Date: Thu, 7 Jun 2012 00:05:44 +0000 (+0200) Subject: add error handling for reading morphology update X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2Fstemmatology.git;a=commitdiff_plain;h=a7f4020a1a1fd72aba6e25dc0a8f8aa9a1891202 add error handling for reading morphology update --- diff --git a/lib/Text/Tradition/Collation/Reading/WordForm.pm b/lib/Text/Tradition/Collation/Reading/WordForm.pm index ebfa445..9d27181 100644 --- a/lib/Text/Tradition/Collation/Reading/WordForm.pm +++ b/lib/Text/Tradition/Collation/Reading/WordForm.pm @@ -3,6 +3,8 @@ package Text::Tradition::Collation::Reading::WordForm; use Lingua::Features::Structure; use JSON (); use Moose; +use Text::Tradition::Error; +use TryCatch; =head1 NAME @@ -70,7 +72,12 @@ around BUILDARGS => sub { if( exists $args->{'JSON'} ) { my @data = split( / \/\/ /, $args->{'JSON'} ); # print STDERR "Attempting to parse " . $data[2] . " into structure"; - my $morph = Lingua::Features::Structure->from_string( $data[2] ); + my $morph; + try { + $morph = Lingua::Features::Structure->from_string( $data[2] ); + } catch { + throw("Could not parse string " . $data[2] . " into morphological structure"); + } $args = { 'language' => $data[0], 'lemma' => $data[1], 'morphology' => $morph }; } @@ -97,6 +104,13 @@ sub TO_JSON { $self->morphology->to_string() ); } +sub throw { + Text::Tradition::Error->throw( + 'ident' => 'Wordform error', + 'message' => $_[0], + ); +} + no Moose; __PACKAGE__->meta->make_immutable; diff --git a/stemmaweb/lib/stemmaweb/Controller/Relation.pm b/stemmaweb/lib/stemmaweb/Controller/Relation.pm index 4bae11d..8785d6f 100644 --- a/stemmaweb/lib/stemmaweb/Controller/Relation.pm +++ b/stemmaweb/lib/stemmaweb/Controller/Relation.pm @@ -298,12 +298,14 @@ sub reading :Chained('text') :PathPart :Args(1) { $c->stash->{'result'} = $rdg ? _reading_struct( $rdg ) : { 'error' => "No reading with ID $reading_id" }; } elsif ( $c->request->method eq 'POST' ) { + my $errmsg; # Are we re-lemmatizing? if( $c->request->param('relemmatize') ) { my $nf = $c->request->param('normal_form'); # TODO throw error unless $nf $rdg->normal_form( $nf ); # TODO throw error if lemmatization fails + # TODO skip this if normal form hasn't changed $rdg->lemmatize(); } else { # Set all the values that we have for the reading. @@ -320,9 +322,14 @@ sub reading :Chained('text') :PathPart :Args(1) { unless( defined $idx ) { # Make the word form and add it to the lexeme. $c->log->debug("Adding new form for $strrep"); - $idx = $lx->add_matching_form( $strrep ) - 1; + try { + $idx = $lx->add_matching_form( $strrep ) - 1; + } catch( Text::Tradition::Error $e ) { + $c->response->status( '403' ); + $errmsg = $e->message; + } } - $lx->disambiguate( $idx ); + $lx->disambiguate( $idx ) if defined $idx; } elsif( $read_write_keys{$p} ) { my $val = _clean_booleans( $rdg, $p, $c->request->param( $p ) ); $rdg->$p( $val ); @@ -330,7 +337,8 @@ sub reading :Chained('text') :PathPart :Args(1) { } } $m->save( $tradition ); - $c->stash->{'result'} = _reading_struct( $rdg ); + $c->stash->{'result'} = $errmsg ? { 'error' => $errmsg } + : _reading_struct( $rdg ); } $c->forward('View::JSON'); diff --git a/stemmaweb/root/js/relationship.js b/stemmaweb/root/js/relationship.js index 0c34643..3fa40d1 100644 --- a/stemmaweb/root/js/relationship.js +++ b/stemmaweb/root/js/relationship.js @@ -692,7 +692,8 @@ $(document).ready(function () { $("#dialog_overlay").hide(); } }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) { - if( ( ajaxSettings.type == 'POST' ) && jqXHR.status == 403 ) { + if( ajaxSettings.url == getTextURL('relationships') + && ajaxSettings.type == 'POST' && jqXHR.status == 403 ) { var errobj = jQuery.parseJSON( jqXHR.responseText ); $('#status').append( '

Error: ' + errobj.error + '
The relationship cannot be made.

' ); } @@ -747,6 +748,7 @@ $(document).ready(function () { $( this ).dialog( "close" ); }, Update: function() { + $('#reading_status').empty(); var reading_id = $('#reading_id').val() form_values = { 'id' : reading_id, @@ -781,6 +783,7 @@ $(document).ready(function () { open: function() { $(".ui-widget-overlay").css("background", "none"); $("#dialog_overlay").show(); + $('#reading_status').empty(); $("#dialog_overlay").height( $("#enlargement_container").height() ); $("#dialog_overlay").width( $("#enlargement_container").innerWidth() ); $("#dialog_overlay").offset( $("#enlargement_container").offset() ); @@ -788,6 +791,12 @@ $(document).ready(function () { close: function() { $("#dialog_overlay").hide(); } + }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) { + if( ajaxSettings.url.lastIndexOf( getReadingURL('') ) > -1 + && ajaxSettings.type == 'POST' && jqXHR.status == 403 ) { + var errobj = jQuery.parseJSON( jqXHR.responseText ); + $('#reading_status').append( '

Error: ' + errobj.error + '

' ); + } }); diff --git a/stemmaweb/root/src/relate.tt b/stemmaweb/root/src/relate.tt index 3e35dc7..49afb7b 100644 --- a/stemmaweb/root/src/relate.tt +++ b/stemmaweb/root/src/relate.tt @@ -93,6 +93,7 @@ $(document).ready(function () {
+