Handle network timeout before any other error

This commit is contained in:
LordGrey 2023-09-04 19:45:28 +02:00
parent 89eb3523c6
commit bc8cecf19c

View File

@ -296,13 +296,11 @@ httpResponse ProviderRestApi::getResponse(QNetworkReply* const& reply)
if (error.error != QJsonParseError::NoError) if (error.error != QJsonParseError::NoError)
{ {
//Received not valid JSON response //Received not valid JSON response
//std::cout << "Response: [" << replyData.toStdString() << "]" << std::endl;
response.setError(true); response.setError(true);
response.setErrorReason(error.errorString()); response.setErrorReason(error.errorString());
} }
else else
{ {
//std::cout << "Response: [" << QString(jsonDoc.toJson(QJsonDocument::Compact)).toStdString() << "]" << std::endl;
response.setBody(jsonDoc); response.setBody(jsonDoc);
} }
} }
@ -314,40 +312,41 @@ httpResponse ProviderRestApi::getResponse(QNetworkReply* const& reply)
else else
{ {
QString errorReason; QString errorReason;
if (httpStatusCode > 0) { if (reply->error() == QNetworkReply::OperationCanceledError)
QString httpReason = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString(); {
QString advise; errorReason = "Network request timeout error";
switch ( httpStatusCode ) {
case HttpStatusCode::BadRequest:
advise = "Check Request Body";
break;
case HttpStatusCode::UnAuthorized:
advise = "Check Authentication Token (API Key)";
break;
case HttpStatusCode::Forbidden:
advise = "No permission to access the given resource";
break;
case HttpStatusCode::NotFound:
advise = "Check Resource given";
break;
default:
advise = httpReason;
break;
}
errorReason = QString ("[%3 %4] - %5").arg(httpStatusCode).arg(httpReason, advise);
} }
else else
{ {
if (reply->error() == QNetworkReply::OperationCanceledError) qDebug() << "httpStatusCode: "<< httpStatusCode;
{ if (httpStatusCode > 0) {
errorReason = "Network request timeout error"; QString httpReason = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
QString advise;
switch ( httpStatusCode ) {
case HttpStatusCode::BadRequest:
advise = "Check Request Body";
break;
case HttpStatusCode::UnAuthorized:
advise = "Check Authentication Token (API Key)";
break;
case HttpStatusCode::Forbidden:
advise = "No permission to access the given resource";
break;
case HttpStatusCode::NotFound:
advise = "Check Resource given";
break;
default:
advise = httpReason;
break;
}
errorReason = QString ("[%3 %4] - %5").arg(httpStatusCode).arg(httpReason, advise);
} }
else else
{ {
errorReason = reply->errorString(); errorReason = reply->errorString();
} }
} }
response.setError(true); response.setError(true);
response.setErrorReason(errorReason); response.setErrorReason(errorReason);
} }