aboutsummaryrefslogtreecommitdiff
path: root/lib/jobs/basejob.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2020-12-23 17:21:01 +0100
committerKitsune Ral <Kitsune-Ral@users.sf.net>2020-12-23 17:21:01 +0100
commit9ef83e044ed4f8409156b19d529dfc7e45f565c1 (patch)
treeeb740dce9e81c68f4c465ed4ddcfaec3f77cb407 /lib/jobs/basejob.cpp
parent104356d945671762af23e346f7898a3208770d97 (diff)
downloadlibquotient-9ef83e044ed4f8409156b19d529dfc7e45f565c1.tar.gz
libquotient-9ef83e044ed4f8409156b19d529dfc7e45f565c1.zip
BaseJob: tolerate unexpected error payloads
Proxy servers may return arbitrary HTML, for one example; so don't expect to find a valid JSON object in whatever non-empty payload next to a non-2xx HTTP code. Fixes #421.
Diffstat (limited to 'lib/jobs/basejob.cpp')
-rw-r--r--lib/jobs/basejob.cpp17
1 files changed, 9 insertions, 8 deletions
diff --git a/lib/jobs/basejob.cpp b/lib/jobs/basejob.cpp
index b757854a..e16ba4ef 100644
--- a/lib/jobs/basejob.cpp
+++ b/lib/jobs/basejob.cpp
@@ -510,14 +510,15 @@ BaseJob::Status BaseJob::prepareResult() { return Success; }
BaseJob::Status BaseJob::prepareError()
{
- if (!d->rawResponse.isEmpty()) {
- if (const auto status = d->parseJson(); !status.good())
- return status; // If there's anything there, it should be JSON
- if (d->jsonResponse.isArray()) // ...specifically, a JSON object
- return { IncorrectResponse,
- tr("Malformed error JSON: an array instead of an object") };
- }
-
+ // Try to make sense of the error payload but be prepared for all kinds
+ // of unexpected stuff (raw HTML, plain text, foreign JSON among those)
+ if (!d->rawResponse.isEmpty()
+ && reply()->rawHeader("Content-Type") == "application/json")
+ d->parseJson();
+
+ // By now, if d->parseJson() above succeeded then jsonData() will return
+ // a valid JSON object - or an empty object otherwise (in which case most
+ // of if's below will fall through to `return NoError` at the end
const auto& errorJson = jsonData();
const auto errCode = errorJson.value("errcode"_ls).toString();
if (error() == TooManyRequestsError || errCode == "M_LIMIT_EXCEEDED") {