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](){
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;
}

View File

@ -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_;

View File

@ -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:

View File

@ -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_;