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

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>