diff options
| author | hallgren <hallgren@chalmers.se> | 2014-03-27 15:02:27 +0000 |
|---|---|---|
| committer | hallgren <hallgren@chalmers.se> | 2014-03-27 15:02:27 +0000 |
| commit | 9bfed9979034def4a282d4dd6cdfc89a288be544 (patch) | |
| tree | cf0358891bf1205892221c518f7a89598e316d1f /src | |
| parent | 911ae42296780b81f2afd7795c2eabce745649c8 (diff) | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/www/js/gftranslate.js | 5 | ||||
| -rw-r--r-- | src/www/js/support.js | 25 |
2 files changed, 25 insertions, 5 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,15 +125,32 @@ 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,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 with_json(cont) { return function(txt){cont(eval("("+txt+")"));} } - function sameOrigin(url) { var a=empty("a"); a.href=url; // converts to an absolute URL |
