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.

This commit is contained in:
khenn 2010-05-23 12:12:05 -05:00
parent 82e52fd8d5
commit 5692106e85

View file

@ -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;