Api: added error message for server returning unexpected html,

added error message for error during JSON.parse
This commit is contained in:
Hauke Schade 2017-11-11 11:57:36 +01:00
parent 1c492cadf6
commit e1ba95ebef

View file

@ -1399,25 +1399,48 @@ function process_readyState(httpreq) {
var successful = false var successful = false
var errorMessage = null var errorMessage = null
var responseObject = null var responseObject = null
var responseContentType = httpreq.getResponseHeader('Content-Type')
if(httpreq.status === 200 && httpreq.responseText if (httpreq.status === 200 &&
&& httpreq.responseText.length > 2) { responseContentType.match(/json/) &&
httpreq.responseText &&
httpreq.responseText.length > 2) {
console.log(httpreq.responseText);
responseObject = JSON.parse(httpreq.responseText); try {
responseObject = JSON.parse(httpreq.responseText);
if (responseObject.status === 0) { if (responseObject.status === 0) {
successful = true successful = true
} else if(responseObject.content.error) { } else if(responseObject.content.error) {
errorMessage = "Error: " + responseObject.content.error; errorMessage = "Error: " + responseObject.content.error
} else { } else {
errorMessage = "Error (tt-rss status " + responseObject.status + ")" errorMessage = "Error (tt-rss status " + responseObject.status + ")"
}
}
catch (e) {
console.log(e, httpreq.responseText)
errorMessage = "Error (http status " + httpreq.status + ")"
errorMessage = "\n" + e
} }
} else { } else {
errorMessage = "Error (http status " + httpreq.status + ")" if (responseContentType.match(/html/)) {
var title = httpreq.responseText.match(/<title>(.*)<\/title>/);
var body = httpreq.responseText.match(/<body>[^]*<p>([^]*)<\/p>[^]*<\/body>/);
if (httpreq.responseText) { if (title && title.length === 2) {
errorMessage += ": " + httpreq.responseText errorMessage = "Error (" + title[1] + ")"
}
if (body && body.length === 2) {
errorMessage += "\n" + body[1]
}
}
else {
errorMessage = "Error (http status " + httpreq.status + ")"
if (httpreq.responseText) {
errorMessage += "\n" + httpreq.responseText
}
} }
} }