add extra error information to webresponse

This commit is contained in:
dankmeme01 2025-04-03 02:04:13 +02:00
parent 74395fc1b6
commit dcd3244944
4 changed files with 32 additions and 6 deletions
cmake
loader
include/Geode/utils
src

View file

@ -32,9 +32,9 @@ if (GEODE_TARGET_PLATFORM STREQUAL "iOS")
"-framework Foundation" # needed for many things
"-framework AVFoundation" # needed for fmod
"-framework AudioToolbox" # needed for fmod
${GEODE_LOADER_PATH}/include/link/ios/libssl.a
${GEODE_LOADER_PATH}/include/link/ios/libcrypto.a
${GEODE_LOADER_PATH}/include/link/ios/libnghttp2.a
${GEODE_LOADER_PATH}/include/link/ios/libssl.a
${GEODE_LOADER_PATH}/include/link/ios/libcrypto.a
${GEODE_LOADER_PATH}/include/link/ios/libnghttp2.a
${GEODE_LOADER_PATH}/include/link/ios/libcurl.a
${GEODE_LOADER_PATH}/include/link/ios/libfmod_iphoneos.a
)

View file

@ -92,6 +92,13 @@ namespace geode::utils::web {
* @return std::optional<std::vector<std::string>>
*/
std::optional<std::vector<std::string>> getAllHeadersNamed(std::string_view name) const;
/**
* Returns additional error information, in case the request failed.
* In case the request did not fail, or no more information is available beyond what `string` returns,
* an empty string is returned.
*/
std::string const& errorMessage() const;
};
class GEODE_DLL WebProgress final {

View file

@ -152,11 +152,18 @@ public:
}
}
else {
auto resp = event->getValue();
m_status = DownloadStatusError {
.details = fmt::format("Server returned error {}", event->getValue()->code()),
.details = fmt::format("Server returned error {}", resp->code()),
};
log::error("Failed to download {}, server returned error {}", m_id, event->getValue()->code());
log::error("{}", event->getValue()->string().unwrapOr("No response"));
log::error("Failed to download {}, server returned error {}", m_id, resp->code());
log::error("{}", resp->string().unwrapOr("No response"));
const auto& extErr = resp->errorMessage();
if (!extErr.empty()) {
log::error("Extended error info: {}", extErr);
}
}
}
else if (auto progress = event->getProgress()) {

View file

@ -96,6 +96,7 @@ class WebResponse::Impl {
public:
int m_code;
ByteVector m_data;
std::string m_errMessage;
std::unordered_map<std::string, std::vector<std::string>> m_headers;
Result<> into(std::filesystem::path const& path) const;
@ -159,6 +160,10 @@ std::optional<std::vector<std::string>> WebResponse::getAllHeadersNamed(std::str
return std::nullopt;
}
std::string const& WebResponse::errorMessage() const {
return m_impl->m_errMessage;
}
class WebProgress::Impl {
public:
size_t m_downloadCurrent;
@ -400,6 +405,11 @@ WebTask WebRequest::send(std::string_view method, std::string_view url) {
// Do not fail if response code is 4XX or 5XX
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 0L);
// If an error happens, we want to get a more specific description of the issue
char errorBuf[CURL_ERROR_SIZE];
errorBuf[0] = '\0';
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuf);
// Get headers from the response
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &responseData);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, (+[](char* buffer, size_t size, size_t nitems, void* ptr) {
@ -455,6 +465,8 @@ WebTask WebRequest::send(std::string_view method, std::string_view url) {
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
responseData.response.m_impl->m_code = static_cast<int>(code);
responseData.response.m_impl->m_errMessage = std::string(errorBuf);
// Free up curl memory
curl_slist_free_all(headers);
curl_easy_cleanup(curl);