add error handling for reading morphology update
Tara L Andrews [Thu, 7 Jun 2012 00:05:44 +0000 (02:05 +0200)]
lib/Text/Tradition/Collation/Reading/WordForm.pm
stemmaweb/lib/stemmaweb/Controller/Relation.pm
stemmaweb/root/js/relationship.js
stemmaweb/root/src/relate.tt

index ebfa445..9d27181 100644 (file)
@@ -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;
 
index 4bae11d..8785d6f 100644 (file)
@@ -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');
index 0c34643..3fa40d1 100644 (file)
@@ -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( '<p class="error">Error: ' + errobj.error + '</br>The relationship cannot be made.</p>' );
       }
@@ -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( '<p class="error">Error: ' + errobj.error + '</p>' );
+      }
   });
   
 
index 3e35dc7..49afb7b 100644 (file)
@@ -93,6 +93,7 @@ $(document).ready(function () {
                        <div id="morphology">
                        </div>
                        </select>
+                       <div id="reading_status"></div>
                </form>
     </div>