From 9bfed9979034def4a282d4dd6cdfc89a288be544 Mon Sep 17 00:00:00 2001 From: hallgren Date: Thu, 27 Mar 2014 15:02:27 +0000 Subject: [PATCH] support.js & gftranslate.js: improved handling of server errors In support.js, the functions http_get_json, ajax_http_get_json and ajax_http_post_json now calls the supplied error callback if the server returns invalid JSON (e.g. because of a crash). The function gftranslate.translate in gftranslate.js returns a JSON value containing an error message (since it doesn't have an error callback). This should result in fewer situations where "nothing happens" and the user doesn't know if it is beacuse the server is slow, or if there was an error. --- src/www/js/gftranslate.js | 5 ++++- src/www/js/support.js | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/www/js/gftranslate.js b/src/www/js/gftranslate.js index cf85a28e3..c0a47cfb3 100644 --- a/src/www/js/gftranslate.js +++ b/src/www/js/gftranslate.js @@ -7,7 +7,10 @@ gftranslate.jsonurl="/robust/Translate8.pgf" gftranslate.grammar="Translate" // the name of the grammar gftranslate.call=function(querystring,cont) { - http_get_json(gftranslate.jsonurl+querystring,cont) + function errcont(text,code) { + cont([{translations:[{error:code+" "+text}]}]) + } + http_get_json(gftranslate.jsonurl+querystring,cont,errcont) } // Translate a sentence diff --git a/src/www/js/support.js b/src/www/js/support.js index 934ea0f96..2c7dd782e 100644 --- a/src/www/js/support.js +++ b/src/www/js/support.js @@ -125,14 +125,31 @@ function ajax_http_post(url,formdata,callback,errorcallback) { // JSON via AJAX function ajax_http_get_json(url,cont,errorcallback) { - ajax_http_get(url, with_json(cont), errorcallback); + ajax_http_get(url, with_json(cont,errorcallback), errorcallback); } function ajax_http_post_json(url,formdata,cont,errorcallback) { - ajax_http_post(url, formdata, with_json(cont), errorcallback); + ajax_http_post(url, formdata, with_json(cont,errorcallback), errorcallback); } -function with_json(cont) { return function(txt){cont(eval("("+txt+")"));} } +function with_json(cont,errorcallback) { + return function(txt){ + if(txt) { + try { + var json=eval("("+txt+")") + } catch (e) { + if(errorcallback) + errorcallback("JSON parsing problem",500,"text/plain") + return + } + cont(json); + } + else { + if(errorcallback) + errorcallback("Empty response form server (crash?)",500,"text/plain") + } + } +} function sameOrigin(url) { var a=empty("a");