Commit 327ad249 authored by Paweł Wegner's avatar Paweł Wegner

Added code to error messages.

parent 811ce006
......@@ -50,9 +50,9 @@ void DownloadFileCallback::done() {
emit window_->downloadProgressChanged(0, 0);
}
void DownloadFileCallback::error(const std::string& desc) {
void DownloadFileCallback::error(Error e) {
std::unique_lock<std::mutex> lock(window_->stream_mutex());
std::cerr << "[FAIL] Download: " << desc << "\n";
std::cerr << "[FAIL] Download: " << e.description_ << "\n";
emit window_->downloadProgressChanged(0, 0);
}
......@@ -85,9 +85,9 @@ void UploadFileCallback::done() {
emit window_->uploadProgressChanged(0, 0);
}
void UploadFileCallback::error(const std::string& description) {
void UploadFileCallback::error(Error e) {
std::unique_lock<std::mutex> lock(window_->stream_mutex());
std::cerr << "[FAIL] Upload: " << description << "\n";
std::cerr << "[FAIL] Upload: " << e.description_ << "\n";
emit window_->uploadProgressChanged(0, 0);
}
......@@ -118,10 +118,9 @@ void CloudProviderCallback::declined(const ICloudProvider&) {
emit window_->closeBrowser();
}
void CloudProviderCallback::error(const ICloudProvider&,
const std::string& desc) {
void CloudProviderCallback::error(const ICloudProvider&, Error e) {
std::unique_lock<std::mutex> lock(window_->stream_mutex());
std::cerr << "[FAIL] Authorize " << desc.c_str() << "\n";
std::cerr << "[FAIL] Authorize " << e.description_ << "\n";
emit window_->closeBrowser();
}
......@@ -133,9 +132,9 @@ void ListDirectoryCallback::receivedItem(IItem::Pointer item) {
void ListDirectoryCallback::done(const std::vector<IItem::Pointer>&) {}
void ListDirectoryCallback::error(const std::string& str) {
void ListDirectoryCallback::error(Error e) {
std::unique_lock<std::mutex> lock(window_->stream_mutex());
std::cerr << "[FAIL] ListDirectory: " << str << "\n";
std::cerr << "[FAIL] ListDirectory: " << e.description_ << "\n";
emit window_->closeBrowser();
}
......@@ -157,9 +156,9 @@ void DownloadThumbnailCallback::done() {
emit item_->receivedImage();
}
void DownloadThumbnailCallback::error(const std::string& error) {
void DownloadThumbnailCallback::error(Error e) {
std::unique_lock<std::mutex> lock(item_->window_->stream_mutex());
std::cerr << "[FAIL] Thumbnail: " << error << "\n";
std::cerr << "[FAIL] Thumbnail: " << e.description_ << "\n";
}
void DownloadThumbnailCallback::progress(uint32_t, uint32_t) {}
......@@ -35,6 +35,7 @@ using cloudstorage::IListDirectoryCallback;
using cloudstorage::IDownloadFileCallback;
using cloudstorage::IUploadFileCallback;
using cloudstorage::IItem;
using cloudstorage::Error;
class ListDirectoryCallback : public IListDirectoryCallback {
public:
......@@ -44,7 +45,7 @@ class ListDirectoryCallback : public IListDirectoryCallback {
void done(const std::vector<IItem::Pointer>&) override;
void error(const std::string& str) override;
void error(Error) override;
private:
Window* window_;
......@@ -58,7 +59,7 @@ class DownloadThumbnailCallback : public IDownloadFileCallback {
void done() override;
void error(const std::string& error) override;
void error(Error) override;
void progress(uint32_t, uint32_t) override;
......@@ -73,7 +74,7 @@ class DownloadFileCallback : public IDownloadFileCallback {
void receivedData(const char* data, uint32_t length) override;
void done() override;
void error(const std::string&) override;
void error(Error) override;
void progress(uint32_t total, uint32_t now) override;
private:
......@@ -90,7 +91,7 @@ class UploadFileCallback : public IUploadFileCallback {
uint32_t putData(char* data, uint32_t maxlength) override;
uint64_t size() override;
void done() override;
void error(const std::string& description) override;
void error(Error) override;
void progress(uint32_t total, uint32_t now) override;
private:
......@@ -105,7 +106,7 @@ class CloudProviderCallback : public cloudstorage::ICloudProvider::ICallback {
Status userConsentRequired(const cloudstorage::ICloudProvider&) override;
void accepted(const cloudstorage::ICloudProvider&) override;
void declined(const cloudstorage::ICloudProvider&) override;
void error(const cloudstorage::ICloudProvider&, const std::string&) override;
void error(const cloudstorage::ICloudProvider&, Error) override;
private:
Window* window_;
......
......@@ -182,7 +182,7 @@ MockProvider::MockDownloadFileRequest::MockDownloadFileRequest(
: ":/resources/file.png";
QFile file(filename.c_str());
if (!file.open(QFile::ReadOnly)) {
callback_->error("Not found file: " + filename);
callback_->error({404, "Not found file: " + filename});
}
while (file.bytesAvailable()) {
callback_->receivedData(buffer.data(),
......
......@@ -37,8 +37,8 @@ class Callback : public cloudstorage::ICloudProvider::ICallback {
}
void error(const cloudstorage::ICloudProvider&,
const std::string& description) override {
std::cout << "Error: " << description << "\n";
cloudstorage::Error e) override {
std::cout << "Error: " << e.description_ << "\n";
}
private:
......
......@@ -105,8 +105,7 @@ AuthorizeRequest::Pointer AmazonDrive::authorizeAsync() {
std::stringstream input, output, error;
int code = r->send(request.get(), input, output, &error);
if (!IHttpRequest::isSuccess(code)) {
if (!r->is_cancelled())
callback()->error(*this, "Couldn't obtain endpoints.");
if (!r->is_cancelled()) callback()->error(*this, {code, error.str()});
return Error{code, error.str()};
}
Json::Value response;
......
......@@ -386,7 +386,7 @@ std::vector<IItem::Pointer> AmazonS3::listDirectoryResponse(
void AmazonS3::authorizeRequest(IHttpRequest& request) const {
if (!crypto()) {
callback()->error(*this, "No crypto functions provided.");
callback()->error(*this, {500, "No crypto functions provided."});
return;
}
std::string current_date = currentDate();
......
......@@ -75,9 +75,7 @@ class ListDirectoryCallback : public cloudstorage::IListDirectoryCallback {
callback_(result);
}
void error(const std::string& d) override {
callback_(cloudstorage::Error{0, d});
}
void error(cloudstorage::Error e) override { callback_(e); }
private:
cloudstorage::ListDirectoryCallback callback_;
......@@ -99,9 +97,9 @@ class DownloadFileCallback : public cloudstorage::IDownloadFileCallback {
callback_(nullptr);
}
void error(const std::string& error) override {
void error(cloudstorage::Error e) override {
file_.close();
callback_(cloudstorage::Error{400, error});
callback_(e);
}
void progress(uint32_t, uint32_t) override {}
......@@ -136,9 +134,7 @@ class UploadFileCallback : public cloudstorage::IUploadFileCallback {
void done() override { callback_(nullptr); }
void error(const std::string& d) override {
callback_(cloudstorage::Error{400, d});
}
void error(cloudstorage::Error e) override { callback_(e); }
void progress(uint32_t, uint32_t) override {}
......
......@@ -156,7 +156,7 @@ class HttpDataCallback : public IDownloadFileCallback {
}
void done() override {}
void error(const std::string&) override {}
void error(Error) override {}
void progress(uint32_t, uint32_t) override {}
Buffer::Pointer buffer_;
......@@ -353,7 +353,7 @@ ICloudProvider::ListDirectoryRequest::Pointer MegaNz::listDirectoryAsync(
if (!ensureAuthorized(r)) {
Error e = {401, "authorization failed"};
if (!r->is_cancelled())
callback->error("Authorization failed.");
callback->error({401, "Authorization failed."});
else
e = {IHttpRequest::Aborted, ""};
return e;
......@@ -393,7 +393,7 @@ ICloudProvider::UploadFileRequest::Pointer MegaNz::uploadFileAsync(
if (!ensureAuthorized(r)) {
Error e = {401, "authorization failed"};
if (!r->is_cancelled())
callback->error("Authorization failed.");
callback->error({401, "Authorization failed."});
else
e = {IHttpRequest::Aborted, ""};
return e;
......@@ -403,7 +403,7 @@ ICloudProvider::UploadFileRequest::Pointer MegaNz::uploadFileAsync(
std::fstream mega_cache(cache.c_str(),
std::fstream::out | std::fstream::binary);
if (!mega_cache) {
callback->error("Couldn't open cache file " + cache);
callback->error({403, "Couldn't open cache file " + cache});
return Error{403, "couldn't open cache file" + cache};
}
std::array<char, BUFFER_SIZE> buffer;
......@@ -429,7 +429,7 @@ ICloudProvider::UploadFileRequest::Pointer MegaNz::uploadFileAsync(
mega_->removeTransferListener(&listener);
if (listener.status_ != Listener::SUCCESS) {
if (!r->is_cancelled()) {
callback->error("Upload error: " + listener.error_);
callback->error({500, "Upload error: " + listener.error_});
return Error{500, listener.error_};
} else
return Error{IHttpRequest::Aborted, ""};
......@@ -449,7 +449,7 @@ ICloudProvider::DownloadFileRequest::Pointer MegaNz::getThumbnailAsync(
if (!ensureAuthorized(r)) {
Error e = {401, "authorization failed"};
if (!r->is_cancelled())
callback->error("Authorization failed.");
callback->error({401, "Authorization failed."});
else
e = {IHttpRequest::Aborted, ""};
return e;
......@@ -468,7 +468,7 @@ ICloudProvider::DownloadFileRequest::Pointer MegaNz::getThumbnailAsync(
std::fstream cache_file(cache.c_str(),
std::fstream::in | std::fstream::binary);
if (!cache_file) {
callback->error("Couldn't open cache file " + cache);
callback->error({500, "Couldn't open cache file " + cache});
return Error{500, "couldn't open cache file " + cache};
}
std::array<char, BUFFER_SIZE> buffer;
......@@ -621,7 +621,7 @@ MegaNz::downloadResolver(IItem::Pointer item,
if (!ensureAuthorized(r)) {
Error e = {401, "authorization failed"};
if (!r->is_cancelled())
callback->error("Authorization failed.");
callback->error({401, "Authorization failed."});
else
e = {IHttpRequest::Aborted, ""};
return e;
......@@ -641,7 +641,7 @@ MegaNz::downloadResolver(IItem::Pointer item,
mega_->removeTransferListener(&listener);
if (listener.status_ != Listener::SUCCESS) {
if (!r->is_cancelled()) {
callback->error("Failed to download");
callback->error({500, "Failed to download"});
return Error{500, "failed to download"};
} else
return Error{IHttpRequest::Aborted, ""};
......
......@@ -45,9 +45,8 @@ ICloudProvider::UploadFileRequest::Pointer OneDrive::uploadFileAsync(
IItem::Pointer parent, const std::string& filename,
IUploadFileCallback::Pointer callback) {
auto r = util::make_unique<Request<EitherError<void>>>(shared_from_this());
r->set_error_callback([callback](Request<EitherError<void>>* r, int code,
const std::string& desc) {
callback->error(r->error_string(code, desc));
r->set_error_callback([callback](Request<EitherError<void>>* r, Error e) {
callback->error(e);
});
r->set_resolver([=](Request<EitherError<void>>* r) -> EitherError<void> {
std::stringstream output;
......@@ -61,7 +60,7 @@ ICloudProvider::UploadFileRequest::Pointer OneDrive::uploadFileAsync(
},
output, &error);
if (!IHttpRequest::isSuccess(code)) {
callback->error("Failed to create upload session.");
callback->error(error);
return error;
}
Json::Value response;
......@@ -88,7 +87,7 @@ ICloudProvider::UploadFileRequest::Pointer OneDrive::uploadFileAsync(
callback->progress(size, sent + now);
});
if (!IHttpRequest::isSuccess(code)) {
callback->error("Failed to upload chunk.");
callback->error(error);
return error;
}
sent += length;
......
......@@ -93,12 +93,11 @@ ICloudProvider::DownloadFileRequest::Pointer YandexDisk::downloadFileAsync(
IItem::Pointer item, IDownloadFileCallback::Pointer callback) {
auto r = util::make_unique<Request<EitherError<void>>>(shared_from_this());
r->set_error_callback([this, callback](Request<EitherError<void>>* r,
int code, const std::string& desc) {
callback->error(r->error_string(code, desc));
});
Error e) { callback->error(e); });
r->set_resolver([this, item, callback](
Request<EitherError<void>>* r) -> EitherError<void> {
std::stringstream output;
Error error;
int code = r->sendRequest(
[this, item](std::ostream&) {
auto request =
......@@ -106,10 +105,11 @@ ICloudProvider::DownloadFileRequest::Pointer YandexDisk::downloadFileAsync(
request->setParameter("path", item->id());
return request;
},
output);
if (!IHttpRequest::isSuccess(code))
callback->error("Couldn't get download url.");
else {
output, &error);
if (!IHttpRequest::isSuccess(code)) {
callback->error(error);
return error;
} else {
Json::Value json;
output >> json;
DownloadStreamWrapper wrapper(std::bind(
......@@ -118,9 +118,14 @@ ICloudProvider::DownloadFileRequest::Pointer YandexDisk::downloadFileAsync(
std::string url = json["href"].asString();
code = r->sendRequest(
[this, url](std::ostream&) { return http()->create(url, "GET"); },
stream, nullptr,
stream, &error,
std::bind(&IDownloadFileCallback::progress, callback.get(), _1, _2));
if (IHttpRequest::isSuccess(code)) callback->done();
if (IHttpRequest::isSuccess(code)) {
callback->done();
return nullptr;
} else {
return error;
}
}
});
return std::move(r);
......@@ -130,9 +135,8 @@ ICloudProvider::UploadFileRequest::Pointer YandexDisk::uploadFileAsync(
IItem::Pointer directory, const std::string& filename,
IUploadFileCallback::Pointer callback) {
auto r = util::make_unique<Request<EitherError<void>>>(shared_from_this());
r->set_error_callback([callback](Request<EitherError<void>>* r, int code,
const std::string& desc) {
callback->error(r->error_string(code, desc));
r->set_error_callback([callback](Request<EitherError<void>>* r, Error e) {
callback->error(e);
});
r->set_resolver([this, directory, filename,
callback](Request<EitherError<void>>* r) {
......@@ -171,7 +175,7 @@ ICloudProvider::UploadFileRequest::Pointer YandexDisk::uploadFileAsync(
else
return error;
} else {
callback->error("Couldn't get upload url.");
callback->error(error);
return error;
}
});
......
......@@ -65,16 +65,15 @@ ICloudProvider::ListDirectoryRequest::Pointer YouTube::listDirectoryAsync(
auto r = util::make_unique<Request<EitherError<std::vector<IItem::Pointer>>>>(
shared_from_this());
auto is_fine = [](int code) { return code == 404; };
r->set_error_callback(
[callback, is_fine](Request<EitherError<std::vector<IItem::Pointer>>>* r,
int code, const std::string& error) {
if (!is_fine(code)) callback->error(r->error_string(code, error));
});
r->set_error_callback([callback, is_fine](
Request<EitherError<std::vector<IItem::Pointer>>>* r, Error e) {
if (!is_fine(e.code_)) callback->error(e);
});
r->set_resolver([item, callback, is_fine,
this](Request<EitherError<std::vector<IItem::Pointer>>>* r)
-> EitherError<std::vector<IItem::Pointer>> {
if (item->type() != IItem::FileType::Directory) {
callback->error("Trying to list non-directory.");
callback->error(Error{403, "Trying to list non-directory."});
return Error{403, "trying to list non-directory"};
}
std::string page_token;
......@@ -163,9 +162,7 @@ ICloudProvider::DownloadFileRequest::Pointer YouTube::downloadFileAsync(
IItem::Pointer item, IDownloadFileCallback::Pointer callback) {
auto r = util::make_unique<Request<EitherError<void>>>(shared_from_this());
r->set_error_callback([this, callback](Request<EitherError<void>>* r,
int code, const std::string& desc) {
callback->error(r->error_string(code, desc));
});
Error e) { callback->error(e); });
r->set_resolver([this, item, callback](
Request<EitherError<void>>* r) -> EitherError<void> {
std::string url = item->url();
......
......@@ -84,11 +84,8 @@ class ICloudProvider {
/**
* Called when an error occurred.
*
* @param description error's description
*/
virtual void error(const ICloudProvider&,
const std::string& description) = 0;
virtual void error(const ICloudProvider&, Error) = 0;
};
/**
......
......@@ -32,6 +32,11 @@
namespace cloudstorage {
struct Error {
int code_;
std::string description_;
};
/**
* Class representing pending request. When there is no reference to the
* request, it's immediately cancelled.
......@@ -86,10 +91,8 @@ class IListDirectoryCallback {
/**
* Called when error occurred.
*
* @param description error description
*/
virtual void error(const std::string& description) = 0;
virtual void error(Error) = 0;
};
class IDownloadFileCallback {
......@@ -113,10 +116,8 @@ class IDownloadFileCallback {
/**
* Called when error occurred.
*
* @param description error description
*/
virtual void error(const std::string& description) = 0;
virtual void error(Error) = 0;
/**
* Called when progress has changed.
......@@ -160,9 +161,8 @@ class IUploadFileCallback {
/**
* Called when error occurred.
* @param description
*/
virtual void error(const std::string& description) = 0;
virtual void error(Error) = 0;
/**
* Called when upload progress changed.
......@@ -173,11 +173,6 @@ class IUploadFileCallback {
virtual void progress(uint32_t total, uint32_t now) = 0;
};
struct Error {
int code_;
std::string description_;
};
template <class Left, class Right>
class Either {
public:
......
......@@ -71,7 +71,7 @@ EitherError<void> AuthorizeRequest::oauth2Authorization() {
} else if (r && !IHttpRequest::isClientError(code)) {
if (!is_cancelled())
provider()->callback()->error(*provider(),
error_string(code, error_stream.str()));
Error{code, error_stream.str()});
return Error{code, error_stream.str()};
}
......@@ -91,7 +91,7 @@ EitherError<void> AuthorizeRequest::oauth2Authorization() {
return nullptr;
} else if (!is_cancelled()) {
provider()->callback()->error(*provider(),
error_string(code, error_stream.str()));
Error{code, error_stream.str()});
}
}
return Error{code, error_stream.str()};
......
......@@ -73,7 +73,7 @@ void DownloadFileRequest::generateThumbnail(
semaphore.wait();
if (r->is_cancelled()) return item_data->cancel();
if (!item_data->result().right())
return callback->error("Couldn't get item url.");
return callback->error(*item_data->result().left());
auto thumbnail_data = r->provider()->thumbnailer()->generateThumbnail(
r->provider(), item_data->result().right(),
[&semaphore, callback](EitherError<std::vector<char>> data) {
......@@ -81,7 +81,7 @@ void DownloadFileRequest::generateThumbnail(
callback->receivedData(data.right()->data(), data.right()->size());
callback->done();
} else {
callback->error(data.left()->description_);
callback->error(*data.left());
}
semaphore.notify();
});
......@@ -90,7 +90,7 @@ void DownloadFileRequest::generateThumbnail(
}
void DownloadFileRequest::error(int code, const std::string& description) {
if (!fallback_thumbnail_) callback_->error(error_string(code, description));
if (!fallback_thumbnail_) callback_->error(Error{code, description});
}
DownloadStreamWrapper::DownloadStreamWrapper(
......
......@@ -33,21 +33,22 @@ ListDirectoryRequest::ListDirectoryRequest(std::shared_ptr<CloudProvider> p,
: Request(p),
directory_(std::move(directory)),
callback_(std::move(callback)) {
set_resolver([this](Request*) -> std::vector<IItem::Pointer> {
set_resolver([this](Request*) -> EitherError<std::vector<IItem::Pointer>> {
if (directory_->type() != IItem::FileType::Directory) {
callback_->error("Trying to list non-directory.");
return {};
callback_->error({403, "Trying to list non-directory."});
return Error{403, "trying to list non directory"};
}
std::string page_token;
std::vector<IItem::Pointer> result;
bool failure = false;
Error error;
do {
std::stringstream output_stream;
int code = sendRequest(
[this, &page_token](std::ostream& i) {
return provider()->listDirectoryRequest(*directory_, page_token, i);
},
output_stream);
output_stream, &error);
if (IHttpRequest::isSuccess(code)) {
page_token = "";
for (auto& t :
......@@ -58,15 +59,19 @@ ListDirectoryRequest::ListDirectoryRequest(std::shared_ptr<CloudProvider> p,
} else
failure = true;
} while (!page_token.empty() && !failure);
if (!failure) callback_->done(result);
return result;
if (!failure) {
callback_->done(result);
return result;
} else {
return error;
}
});
}
ListDirectoryRequest::~ListDirectoryRequest() { cancel(); }
void ListDirectoryRequest::error(int code, const std::string& description) {
callback_->error(error_string(code, description));
callback_->error(Error{code, description});
}
} // namespace cloudstorage
......@@ -132,7 +132,7 @@ bool Request<T>::reauthorize() {
template <class T>
void Request<T>::error(int code, const std::string& desc) {
if (error_callback_) error_callback_(this, code, desc);
if (error_callback_) error_callback_(this, Error{code, desc});
}
template <class T>
......
......@@ -44,7 +44,7 @@ class Request : public IRequest<ReturnValue> {
public:
using ProgressFunction = std::function<void(uint32_t, uint32_t)>;
using Resolver = std::function<ReturnValue(Request*)>;
using ErrorCallback = std::function<void(Request*, int, const std::string&)>;
using ErrorCallback = std::function<void(Request*, Error)>;
using CancelCallback = std::function<void()>;
Request(std::shared_ptr<CloudProvider>);
......
......@@ -40,7 +40,7 @@ UploadFileRequest::UploadFileRequest(
callback_(callback) {
set_resolver([this](Request*) -> EitherError<void> {
if (directory_->type() != IItem::FileType::Directory) {
callback_->error("Can't upload into non-directory.");
callback_->error({403, "Can't upload into non-directory."});
return Error{403, "can't upload into non-directory"};
}
std::stringstream response_stream;
......@@ -68,7 +68,7 @@ UploadFileRequest::UploadFileRequest(
UploadFileRequest::~UploadFileRequest() { cancel(); }
void UploadFileRequest::error(int code, const std::string& description) {
callback_->error(error_string(code, description));
callback_->error(Error{code, description});
}
UploadStreamWrapper::UploadStreamWrapper(
......
......@@ -46,8 +46,8 @@ class Callback : public cloudstorage::ICloudProvider::ICallback {
std::cerr << "access denied ;_;\n";
}
void error(const cloudstorage::ICloudProvider&, const std::string&) override {
}
void error(const cloudstorage::ICloudProvider&,
cloudstorage::Error) override {}
private:
std::string drive_file_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment