From 5692106e850f9438292b44c627031cf3caac1e78 Mon Sep 17 00:00:00 2001 From: khenn Date: Sun, 23 May 2010 12:12:05 -0500 Subject: [PATCH] Added better error handling for JSON parse errors. Scripts don't crash any longer when JSON gets a bad parse string. Instead the script receives a valid empty response and a warning is thrown to the log containing the JSON string passed in as well as the user who tried to execute. --- lib/WebGUI/Content/AjaxI18N.pm | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/WebGUI/Content/AjaxI18N.pm b/lib/WebGUI/Content/AjaxI18N.pm index 5712b9625..4d6018526 100644 --- a/lib/WebGUI/Content/AjaxI18N.pm +++ b/lib/WebGUI/Content/AjaxI18N.pm @@ -49,20 +49,25 @@ sub handler { # Only handle op=ajaxGetI18N return undef unless ( $session->form->get( "op" ) eq "ajaxGetI18N" ); - my $json = $session->form->get( "request" ); - my $namespaces = JSON->new->decode( $json ); - my $i18n = WebGUI::International->new( $session ); my $response = {}; + my $json = $session->form->get( "request" ); + my $namespaces = eval { JSON->new->decode( $json ) }; + unless ($@) { + my $i18n = WebGUI::International->new( $session ); - for my $ns ( keys %{ $namespaces } ) { - for my $key ( @{ $namespaces->{ $ns } } ) { - $response->{ $ns }->{ $key } = $i18n->get( $key, $ns ); + for my $ns ( keys %{ $namespaces } ) { + for my $key ( @{ $namespaces->{ $ns } } ) { + $response->{ $ns }->{ $key } = $i18n->get( $key, $ns ); + } } } - + else { + $session->log->warn("User ".$session->user->username." tried to execute ajaxGetI18n but could not decode JSON string: $json"); + } $session->http->setMimeType( "application/json" ); return JSON->new->encode( $response ); } + 1;