From 872fcecfad5bb2291800e1daea3aa2e9fedb81a8 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Thu, 27 Dec 2018 10:33:32 -0800 Subject: [PATCH] http: Report errors reading discs. For more clarity when they can't load. --- Core/FileLoaders/HTTPFileLoader.cpp | 7 +++++++ Core/FileLoaders/HTTPFileLoader.h | 5 +++++ Core/Loaders.cpp | 4 +++- Core/Loaders.h | 7 +++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Core/FileLoaders/HTTPFileLoader.cpp b/Core/FileLoaders/HTTPFileLoader.cpp index f3407d1127..9c14f5b9fb 100644 --- a/Core/FileLoaders/HTTPFileLoader.cpp +++ b/Core/FileLoaders/HTTPFileLoader.cpp @@ -29,6 +29,7 @@ void HTTPFileLoader::Prepare() { std::call_once(preparedFlag_, [this](){ if (!client_.Resolve(url_.Host().c_str(), url_.Port())) { ERROR_LOG(LOADER, "HTTP request failed, unable to resolve: %s port %d", url_.Host().c_str(), url_.Port()); + latestError_ = "Could not connect (name not resolved)"; return; } @@ -36,12 +37,14 @@ void HTTPFileLoader::Prepare() { Connect(); if (!connected_) { ERROR_LOG(LOADER, "HTTP request failed, failed to connect: %s port %d", url_.Host().c_str(), url_.Port()); + latestError_ = "Could not connect (refused to connect)"; return; } int err = client_.SendRequest("HEAD", url_.Resource().c_str()); if (err < 0) { ERROR_LOG(LOADER, "HTTP request failed, failed to send request: %s port %d", url_.Host().c_str(), url_.Port()); + latestError_ = "Could not connect (could not request data)"; Disconnect(); return; } @@ -52,6 +55,7 @@ void HTTPFileLoader::Prepare() { if (code != 200) { // Leave size at 0, invalid. ERROR_LOG(LOADER, "HTTP request failed, got %03d for %s", code, filename_.c_str()); + latestError_ = "Could not connect (invalid response)"; Disconnect(); return; } @@ -141,6 +145,7 @@ size_t HTTPFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags f int err = client_.SendRequest("GET", url_.Resource().c_str(), requestHeaders, nullptr); if (err < 0) { + latestError_ = "Invalid response reading data"; Disconnect(); return 0; } @@ -150,6 +155,7 @@ size_t HTTPFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags f int code = client_.ReadResponseHeaders(&readbuf, responseHeaders); if (code != 206) { ERROR_LOG(LOADER, "HTTP server did not respond with range, received code=%03d", code); + latestError_ = "Invalid response reading data"; Disconnect(); return 0; } @@ -188,6 +194,7 @@ size_t HTTPFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags f if (!supportedResponse) { ERROR_LOG(LOADER, "HTTP server did not respond with the range we wanted."); + latestError_ = "Invalid response reading data"; return 0; } diff --git a/Core/FileLoaders/HTTPFileLoader.h b/Core/FileLoaders/HTTPFileLoader.h index ab4773604f..d853153177 100644 --- a/Core/FileLoaders/HTTPFileLoader.h +++ b/Core/FileLoaders/HTTPFileLoader.h @@ -48,6 +48,10 @@ public: cancelConnect_ = true; } + std::string LatestError() const override { + return latestError_; + } + private: void Prepare(); @@ -67,6 +71,7 @@ private: std::string filename_; bool connected_ = false; bool cancelConnect_ = false; + const char *latestError_ = ""; std::once_flag preparedFlag_; std::mutex readAtMutex_; diff --git a/Core/Loaders.cpp b/Core/Loaders.cpp index c901a1245a..14bc31973d 100644 --- a/Core/Loaders.cpp +++ b/Core/Loaders.cpp @@ -294,7 +294,9 @@ bool LoadFile(FileLoader **fileLoaderPtr, std::string *error_string) { case IdentifiedFileType::ERROR_IDENTIFYING: ERROR_LOG(LOADER, "Could not read file"); - *error_string = "Error reading file"; + *error_string = fileLoader ? fileLoader->LatestError() : ""; + if (error_string->empty()) + *error_string = "Error reading file"; break; case IdentifiedFileType::ARCHIVE_RAR: diff --git a/Core/Loaders.h b/Core/Loaders.h index 216cc0fcd9..527fa52f5b 100644 --- a/Core/Loaders.h +++ b/Core/Loaders.h @@ -93,6 +93,10 @@ public: // Cancel any operations that might block, if possible. virtual void Cancel() { } + + virtual std::string LatestError() const { + return ""; + } }; class ProxiedFileLoader : public FileLoader { @@ -125,6 +129,9 @@ public: void Cancel() override { backend_->Cancel(); } + std::string LatestError() const override { + return backend_->LatestError(); + } protected: FileLoader *backend_;