Skip to content

Commit

Permalink
Log reason of file download failure
Browse files Browse the repository at this point in the history
Log a description of the status code received when a file fails to
download from the build api.

BUG=b/395472945
  • Loading branch information
jemoreira committed Feb 12, 2025
1 parent 6f328cb commit 1f0d4fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
6 changes: 3 additions & 3 deletions base/cvd/cuttlefish/host/libs/web/android_build_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,9 @@ Result<void> AndroidBuildApi::ArtifactToFile(const DeviceBuild& build,
const std::string& artifact,
const std::string& path) {
const auto url = CF_EXPECT(GetArtifactDownloadUrl(build, artifact));
bool is_successful_download =
CF_EXPECT(http_client->DownloadToFile(url, path)).HttpSuccess();
CF_EXPECT_EQ(is_successful_download, true);
auto response = CF_EXPECT(http_client->DownloadToFile(url, path));
CF_EXPECTF(response.HttpSuccess(), "Failed to download file: {}",
response.StatusDescription());
return {};
}

Expand Down
28 changes: 22 additions & 6 deletions base/cvd/cuttlefish/host/libs/web/http_client/http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

#include <chrono>
#include <functional>
#include <mutex>
#include <string>
#include <type_traits>
#include <vector>

#include <json/json.h>
#include <fmt/format.h>

#include "common/libs/utils/result.h"

Expand All @@ -32,11 +32,27 @@ struct HttpVoidResponse {};

template <typename T>
struct HttpResponse {
bool HttpInfo() { return http_code >= 100 && http_code <= 199; }
bool HttpSuccess() { return http_code >= 200 && http_code <= 299; }
bool HttpRedirect() { return http_code >= 300 && http_code <= 399; }
bool HttpClientError() { return http_code >= 400 && http_code <= 499; }
bool HttpServerError() { return http_code >= 500 && http_code <= 599; }
bool HttpInfo() const { return http_code >= 100 && http_code <= 199; }
bool HttpSuccess() const { return http_code >= 200 && http_code <= 299; }
bool HttpRedirect() const { return http_code >= 300 && http_code <= 399; }
bool HttpClientError() const { return http_code >= 400 && http_code <= 499; }
bool HttpServerError() const { return http_code >= 500 && http_code <= 599; }

std::string StatusDescription() const {
switch (http_code) {
case 200: return "OK";
case 201: return "Created";
case 204: return "No Content";
case 400: return "Bad Request";
case 401: return "Unauthorized";
case 403: return "Forbidden";
case 404: return "File Not Found";
case 500: return "Internal Server Error";
case 502: return "Bad Gateway";
case 503: return "Service Unavailable";
default: return fmt::format("Status Code: {}", http_code);
}
}

typename std::conditional<std::is_void_v<T>, HttpVoidResponse, T>::type data;
long http_code;
Expand Down

0 comments on commit 1f0d4fb

Please sign in to comment.