aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-04-25 15:58:19 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-04-25 15:58:19 +0900
commitaa49fbcf8b20b7f9e184be41cbe3d0c13a115e6b (patch)
treea43c1c6e9758139e4b45483db2c15fa52eee3211 /lib
parent5f6ac71684056aaae13335a6b867d43c8cdb1692 (diff)
downloadlibquotient-aa49fbcf8b20b7f9e184be41cbe3d0c13a115e6b.tar.gz
libquotient-aa49fbcf8b20b7f9e184be41cbe3d0c13a115e6b.zip
BaseJob: rewrite error detection using genuine HTTP codes
Qt Network error codes don't represent well some cases. Closes #200.
Diffstat (limited to 'lib')
-rw-r--r--lib/jobs/basejob.cpp64
-rw-r--r--lib/jobs/basejob.h4
2 files changed, 40 insertions, 28 deletions
diff --git a/lib/jobs/basejob.cpp b/lib/jobs/basejob.cpp
index 4e35cb01..ab07f6ad 100644
--- a/lib/jobs/basejob.cpp
+++ b/lib/jobs/basejob.cpp
@@ -340,36 +340,44 @@ BaseJob::Status BaseJob::doCheckReply(QNetworkReply* reply) const
const auto httpCode =
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
- if (httpCode == 429) // Qt doesn't know about it yet
- return { TooManyRequestsError, tr("Too many requests") };
-
- // Should we check httpCode instead? Maybe even use it in BaseJob::Status?
- // That would make codes in logs slightly more readable.
- switch( reply->error() )
+ // QNetworkReply error codes seem to be flawed when it comes to HTTP;
+ // see, e.g., https://github.com/QMatrixClient/libqmatrixclient/issues/200
+ // The below processing is based on
+ // https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
+ if (httpCode / 100 == 2) // 2xx
{
- case QNetworkReply::NoError:
- if (checkContentType(reply->rawHeader("Content-Type"),
- d->expectedContentTypes))
- return NoError;
- else // A warning in the logs might be more proper instead
- return { IncorrectResponseError,
- "Incorrect content type of the response" };
-
- case QNetworkReply::AuthenticationRequiredError:
- case QNetworkReply::ContentAccessDenied:
- case QNetworkReply::ContentOperationNotPermittedError:
- return { ContentAccessError, reply->errorString() };
-
- case QNetworkReply::ProtocolInvalidOperationError:
- case QNetworkReply::UnknownContentError:
- return { IncorrectRequestError, reply->errorString() };
-
- case QNetworkReply::ContentNotFoundError:
- return { NotFoundError, reply->errorString() };
-
- default:
- return { NetworkError, reply->errorString() };
+ if (checkContentType(reply->rawHeader("Content-Type"),
+ d->expectedContentTypes))
+ return NoError;
+ else // A warning in the logs might be more proper instead
+ return { UnexpectedResponseTypeWarning,
+ "Unexpected content type of the response" };
}
+
+ return { [httpCode]() -> StatusCode {
+ if (httpCode / 10 == 41)
+ return httpCode == 410 ? IncorrectRequestError : NotFoundError;
+ switch (httpCode)
+ {
+ case 401: case 403: case 407:
+ return ContentAccessError;
+ case 404:
+ return NotFoundError;
+ case 400: case 405: case 406: case 426: case 428:
+ case 505:
+ case 494: // Unofficial nginx "Request header too large"
+ case 497: // Unofficial nginx "HTTP request sent to HTTPS port"
+ return IncorrectRequestError;
+ case 429:
+ return TooManyRequestsError;
+ case 501: case 510:
+ return RequestNotImplementedError;
+ case 511:
+ return NetworkAuthRequiredError;
+ default:
+ return NetworkError;
+ }
+ }(), reply->errorString() };
}
BaseJob::Status BaseJob::parseReply(QNetworkReply* reply)
diff --git a/lib/jobs/basejob.h b/lib/jobs/basejob.h
index f243066f..763ef75a 100644
--- a/lib/jobs/basejob.h
+++ b/lib/jobs/basejob.h
@@ -53,6 +53,8 @@ namespace QMatrixClient
enum StatusCode { NoError = 0 // To be compatible with Qt conventions
, Success = 0
, Pending = 1
+ , WarningLevel = 20
+ , UnexpectedResponseTypeWarning = 21
, Abandoned = 50 //< A very brief period between abandoning and object deletion
, ErrorLevel = 100 //< Errors have codes starting from this
, NetworkError = 100
@@ -63,6 +65,8 @@ namespace QMatrixClient
, IncorrectRequestError
, IncorrectResponseError
, TooManyRequestsError
+ , RequestNotImplementedError
+ , NetworkAuthRequiredError
, UserDefinedError = 200
};