Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prefer reporting error over status for 200 OK #5378

Merged
merged 1 commit into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unversioned

- Bugfix: If a network request errors with 200 OK, Qt's error code is now reported instead of the HTTP status. (#5378)
- Dev: Add doxygen build target. (#5377)
- Dev: Make printing of strings in tests easier. (#5379)

Expand Down
11 changes: 10 additions & 1 deletion src/common/network/NetworkResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ const QByteArray &NetworkResult::getData() const

QString NetworkResult::formatError() const
{
if (this->status_)
// Print the status for errors that mirror HTTP status codes (=0 || >99)
if (this->status_ && (this->error_ == QNetworkReply::NoError ||
this->error_ > QNetworkReply::UnknownNetworkError))
{
return QString::number(*this->status_);
}
Expand All @@ -77,6 +79,13 @@ QString NetworkResult::formatError() const
this->error_);
if (name == nullptr)
{
if (this->status_)
{
return QStringLiteral("unknown error (status: %1, error: %2)")
.arg(QString::number(*this->status_),
QString::number(this->error_));
}

return QStringLiteral("unknown error (%1)").arg(this->error_);
}
return name;
Expand Down
13 changes: 11 additions & 2 deletions tests/src/NetworkResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,21 @@ TEST(NetworkResult, Errors)
"RemoteHostClosedError");

// status code takes precedence
checkResult({Error::TimeoutError, 400, {}}, Error::TimeoutError, 400,
"400");
checkResult({Error::InternalServerError, 400, {}},
Error::InternalServerError, 400, "400");

// error takes precedence (1..=99)
checkResult({Error::BackgroundRequestNotAllowedError, 400, {}},
Error::BackgroundRequestNotAllowedError, 400,
"BackgroundRequestNotAllowedError");
checkResult({Error::UnknownNetworkError, 400, {}},
Error::UnknownNetworkError, 400, "UnknownNetworkError");
}

TEST(NetworkResult, InvalidError)
{
checkResult({static_cast<Error>(-1), {}, {}}, static_cast<Error>(-1),
std::nullopt, "unknown error (-1)");
checkResult({static_cast<Error>(-1), 42, {}}, static_cast<Error>(-1), 42,
"unknown error (status: 42, error: -1)");
}
Loading