http: Report errors reading discs.

For more clarity when they can't load.
This commit is contained in:
Unknown W. Brackets 2018-12-27 10:33:32 -08:00
parent 7f84c87931
commit 872fcecfad
4 changed files with 22 additions and 1 deletions

View file

@ -29,6 +29,7 @@ void HTTPFileLoader::Prepare() {
std::call_once(preparedFlag_, [this](){ std::call_once(preparedFlag_, [this](){
if (!client_.Resolve(url_.Host().c_str(), url_.Port())) { 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()); 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; return;
} }
@ -36,12 +37,14 @@ void HTTPFileLoader::Prepare() {
Connect(); Connect();
if (!connected_) { if (!connected_) {
ERROR_LOG(LOADER, "HTTP request failed, failed to connect: %s port %d", url_.Host().c_str(), url_.Port()); 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; return;
} }
int err = client_.SendRequest("HEAD", url_.Resource().c_str()); int err = client_.SendRequest("HEAD", url_.Resource().c_str());
if (err < 0) { if (err < 0) {
ERROR_LOG(LOADER, "HTTP request failed, failed to send request: %s port %d", url_.Host().c_str(), url_.Port()); 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(); Disconnect();
return; return;
} }
@ -52,6 +55,7 @@ void HTTPFileLoader::Prepare() {
if (code != 200) { if (code != 200) {
// Leave size at 0, invalid. // Leave size at 0, invalid.
ERROR_LOG(LOADER, "HTTP request failed, got %03d for %s", code, filename_.c_str()); ERROR_LOG(LOADER, "HTTP request failed, got %03d for %s", code, filename_.c_str());
latestError_ = "Could not connect (invalid response)";
Disconnect(); Disconnect();
return; 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); int err = client_.SendRequest("GET", url_.Resource().c_str(), requestHeaders, nullptr);
if (err < 0) { if (err < 0) {
latestError_ = "Invalid response reading data";
Disconnect(); Disconnect();
return 0; 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); int code = client_.ReadResponseHeaders(&readbuf, responseHeaders);
if (code != 206) { if (code != 206) {
ERROR_LOG(LOADER, "HTTP server did not respond with range, received code=%03d", code); ERROR_LOG(LOADER, "HTTP server did not respond with range, received code=%03d", code);
latestError_ = "Invalid response reading data";
Disconnect(); Disconnect();
return 0; return 0;
} }
@ -188,6 +194,7 @@ size_t HTTPFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags f
if (!supportedResponse) { if (!supportedResponse) {
ERROR_LOG(LOADER, "HTTP server did not respond with the range we wanted."); ERROR_LOG(LOADER, "HTTP server did not respond with the range we wanted.");
latestError_ = "Invalid response reading data";
return 0; return 0;
} }

View file

@ -48,6 +48,10 @@ public:
cancelConnect_ = true; cancelConnect_ = true;
} }
std::string LatestError() const override {
return latestError_;
}
private: private:
void Prepare(); void Prepare();
@ -67,6 +71,7 @@ private:
std::string filename_; std::string filename_;
bool connected_ = false; bool connected_ = false;
bool cancelConnect_ = false; bool cancelConnect_ = false;
const char *latestError_ = "";
std::once_flag preparedFlag_; std::once_flag preparedFlag_;
std::mutex readAtMutex_; std::mutex readAtMutex_;

View file

@ -294,7 +294,9 @@ bool LoadFile(FileLoader **fileLoaderPtr, std::string *error_string) {
case IdentifiedFileType::ERROR_IDENTIFYING: case IdentifiedFileType::ERROR_IDENTIFYING:
ERROR_LOG(LOADER, "Could not read file"); 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; break;
case IdentifiedFileType::ARCHIVE_RAR: case IdentifiedFileType::ARCHIVE_RAR:

View file

@ -93,6 +93,10 @@ public:
// Cancel any operations that might block, if possible. // Cancel any operations that might block, if possible.
virtual void Cancel() { virtual void Cancel() {
} }
virtual std::string LatestError() const {
return "";
}
}; };
class ProxiedFileLoader : public FileLoader { class ProxiedFileLoader : public FileLoader {
@ -125,6 +129,9 @@ public:
void Cancel() override { void Cancel() override {
backend_->Cancel(); backend_->Cancel();
} }
std::string LatestError() const override {
return backend_->LatestError();
}
protected: protected:
FileLoader *backend_; FileLoader *backend_;