forked from GitHub/gf-core
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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user