Commit 579da9db authored by Paweł Wegner's avatar Paweł Wegner

Removed error methods from callbacks.

parent 327ad249
......@@ -41,21 +41,18 @@ void DownloadFileCallback::receivedData(const char* data, uint32_t length) {
file_.write(data, length);
}
void DownloadFileCallback::done() {
{
std::unique_lock<std::mutex> lock(window_->stream_mutex());
void DownloadFileCallback::done(EitherError<void> e) {
std::unique_lock<std::mutex> lock(window_->stream_mutex());
if (e.left()) {
std::cerr << "[FAIL] Download: " << e.left()->description_ << "\n";
} else {
std::cerr << "[OK] Finished download.\n";
}
file_.close();
emit window_->downloadProgressChanged(0, 0);
}
void DownloadFileCallback::error(Error e) {
std::unique_lock<std::mutex> lock(window_->stream_mutex());
std::cerr << "[FAIL] Download: " << e.description_ << "\n";
emit window_->downloadProgressChanged(0, 0);
}
void DownloadFileCallback::progress(uint32_t total, uint32_t now) {
emit window_->downloadProgressChanged(total, now);
}
......@@ -79,15 +76,13 @@ uint32_t UploadFileCallback::putData(char* data, uint32_t maxlength) {
uint64_t UploadFileCallback::size() { return file_.size(); }
void UploadFileCallback::done() {
std::unique_lock<std::mutex> lock(window_->stream_mutex());
std::cerr << "[OK] Successfuly uploaded\n";
emit window_->uploadProgressChanged(0, 0);
}
void UploadFileCallback::error(Error e) {
void UploadFileCallback::done(EitherError<void> e) {
std::unique_lock<std::mutex> lock(window_->stream_mutex());
std::cerr << "[FAIL] Upload: " << e.description_ << "\n";
if (e.left()) {
std::cerr << "[FAIL] Upload: " << e.left()->description_ << "\n";
} else {
std::cerr << "[OK] Successfuly uploaded\n";
}
emit window_->uploadProgressChanged(0, 0);
}
......@@ -130,12 +125,12 @@ void ListDirectoryCallback::receivedItem(IItem::Pointer item) {
emit window_->addedItem(item);
}
void ListDirectoryCallback::done(const std::vector<IItem::Pointer>&) {}
void ListDirectoryCallback::error(Error e) {
std::unique_lock<std::mutex> lock(window_->stream_mutex());
std::cerr << "[FAIL] ListDirectory: " << e.description_ << "\n";
emit window_->closeBrowser();
void ListDirectoryCallback::done(EitherError<std::vector<IItem::Pointer>> e) {
if (e.left()) {
std::unique_lock<std::mutex> lock(window_->stream_mutex());
std::cerr << "[FAIL] ListDirectory: " << e.left()->description_ << "\n";
emit window_->closeBrowser();
}
}
DownloadThumbnailCallback::DownloadThumbnailCallback(ItemModel* i) : item_(i) {}
......@@ -145,7 +140,8 @@ void DownloadThumbnailCallback::receivedData(const char* data,
data_ += std::string(data, data + length);
}
void DownloadThumbnailCallback::done() {
void DownloadThumbnailCallback::done(EitherError<void> e) {
if (e.left()) return;
{
QFile file(QDir::tempPath() + "/" +
Window::escapeFileName(item_->item()->filename()).c_str() +
......@@ -156,9 +152,4 @@ void DownloadThumbnailCallback::done() {
emit item_->receivedImage();
}
void DownloadThumbnailCallback::error(Error e) {
std::unique_lock<std::mutex> lock(item_->window_->stream_mutex());
std::cerr << "[FAIL] Thumbnail: " << e.description_ << "\n";
}
void DownloadThumbnailCallback::progress(uint32_t, uint32_t) {}
......@@ -36,6 +36,7 @@ using cloudstorage::IDownloadFileCallback;
using cloudstorage::IUploadFileCallback;
using cloudstorage::IItem;
using cloudstorage::Error;
using cloudstorage::EitherError;
class ListDirectoryCallback : public IListDirectoryCallback {
public:
......@@ -43,9 +44,7 @@ class ListDirectoryCallback : public IListDirectoryCallback {
void receivedItem(IItem::Pointer item) override;
void done(const std::vector<IItem::Pointer>&) override;
void error(Error) override;
void done(EitherError<std::vector<IItem::Pointer>>) override;
private:
Window* window_;
......@@ -57,9 +56,7 @@ class DownloadThumbnailCallback : public IDownloadFileCallback {
void receivedData(const char* data, uint32_t length) override;
void done() override;
void error(Error) override;
void done(EitherError<void>) override;
void progress(uint32_t, uint32_t) override;
......@@ -73,8 +70,7 @@ class DownloadFileCallback : public IDownloadFileCallback {
DownloadFileCallback(Window*, std::string filename);
void receivedData(const char* data, uint32_t length) override;
void done() override;
void error(Error) override;
void done(EitherError<void>) override;
void progress(uint32_t total, uint32_t now) override;
private:
......@@ -90,8 +86,7 @@ class UploadFileCallback : public IUploadFileCallback {
void reset() override;
uint32_t putData(char* data, uint32_t maxlength) override;
uint64_t size() override;
void done() override;
void error(Error) override;
void done(EitherError<void>) override;
void progress(uint32_t total, uint32_t now) override;
private:
......
......@@ -182,13 +182,13 @@ MockProvider::MockDownloadFileRequest::MockDownloadFileRequest(
: ":/resources/file.png";
QFile file(filename.c_str());
if (!file.open(QFile::ReadOnly)) {
callback_->error({404, "Not found file: " + filename});
callback_->done(Error{404, "Not found file: " + filename});
}
while (file.bytesAvailable()) {
callback_->receivedData(buffer.data(),
file.read(buffer.data(), buffer_size));
}
callback_->done();
callback_->done(nullptr);
});
}
......
......@@ -71,12 +71,10 @@ class ListDirectoryCallback : public cloudstorage::IListDirectoryCallback {
void receivedItem(cloudstorage::IItem::Pointer) override {}
void done(const std::vector<cloudstorage::IItem::Pointer>& result) override {
void done(cloudstorage::EitherError<std::vector<cloudstorage::IItem::Pointer>> result) override {
callback_(result);
}
void error(cloudstorage::Error e) override { callback_(e); }
private:
cloudstorage::ListDirectoryCallback callback_;
};
......@@ -92,12 +90,7 @@ class DownloadFileCallback : public cloudstorage::IDownloadFileCallback {
file_.write(data, length);
}
void done() override {
file_.close();
callback_(nullptr);
}
void error(cloudstorage::Error e) override {
void done(cloudstorage::EitherError<void> e) override {
file_.close();
callback_(e);
}
......@@ -132,9 +125,7 @@ class UploadFileCallback : public cloudstorage::IUploadFileCallback {
uint64_t size() override { return size_; }
void done() override { callback_(nullptr); }
void error(cloudstorage::Error e) override { callback_(e); }
void done(cloudstorage::EitherError<void> e) override { callback_(e); }
void progress(uint32_t, uint32_t) override {}
......
......@@ -155,8 +155,7 @@ class HttpDataCallback : public IDownloadFileCallback {
for (uint32_t i = 0; i < length; i++) buffer_->data_.push(data[i]);
}
void done() override {}
void error(Error) override {}
void done(EitherError<void>) override {}
void progress(uint32_t, uint32_t) override {}
Buffer::Pointer buffer_;
......@@ -351,11 +350,9 @@ ICloudProvider::ListDirectoryRequest::Pointer MegaNz::listDirectoryAsync(
Request<EitherError<std::vector<IItem::Pointer>>>* r)
-> EitherError<std::vector<IItem::Pointer>> {
if (!ensureAuthorized(r)) {
Error e = {401, "authorization failed"};
if (!r->is_cancelled())
callback->error({401, "Authorization failed."});
else
e = {IHttpRequest::Aborted, ""};
Error e = r->is_cancelled() ? Error{IHttpRequest::Aborted, ""}
: Error{401, "authorization failed"};
callback->done(e);
return e;
}
std::unique_ptr<mega::MegaNode> node(
......@@ -391,11 +388,9 @@ ICloudProvider::UploadFileRequest::Pointer MegaNz::uploadFileAsync(
r->set_resolver([this, item, callback, filename](
Request<EitherError<void>>* r) -> EitherError<void> {
if (!ensureAuthorized(r)) {
Error e = {401, "authorization failed"};
if (!r->is_cancelled())
callback->error({401, "Authorization failed."});
else
e = {IHttpRequest::Aborted, ""};
Error e = r->is_cancelled() ? Error{IHttpRequest::Aborted, ""}
: Error{401, "authorization failed"};
callback->done(e);
return e;
}
std::string cache = temporaryFileName();
......@@ -403,7 +398,7 @@ ICloudProvider::UploadFileRequest::Pointer MegaNz::uploadFileAsync(
std::fstream mega_cache(cache.c_str(),
std::fstream::out | std::fstream::binary);
if (!mega_cache) {
callback->error({403, "Couldn't open cache file " + cache});
callback->done(Error{403, "Couldn't open cache file " + cache});
return Error{403, "couldn't open cache file" + cache};
}
std::array<char, BUFFER_SIZE> buffer;
......@@ -429,12 +424,12 @@ ICloudProvider::UploadFileRequest::Pointer MegaNz::uploadFileAsync(
mega_->removeTransferListener(&listener);
if (listener.status_ != Listener::SUCCESS) {
if (!r->is_cancelled()) {
callback->error({500, "Upload error: " + listener.error_});
callback->done(Error{500, "Upload error: " + listener.error_});
return Error{500, listener.error_};
} else
return Error{IHttpRequest::Aborted, ""};
} else {
callback->done();
callback->done(nullptr);
return nullptr;
}
});
......@@ -447,11 +442,9 @@ ICloudProvider::DownloadFileRequest::Pointer MegaNz::getThumbnailAsync(
r->set_resolver([this, item, callback](
Request<EitherError<void>>* r) -> EitherError<void> {
if (!ensureAuthorized(r)) {
Error e = {401, "authorization failed"};
if (!r->is_cancelled())
callback->error({401, "Authorization failed."});
else
e = {IHttpRequest::Aborted, ""};
Error e = r->is_cancelled() ? Error{IHttpRequest::Aborted, ""}
: Error{401, "authorization failed"};
callback->done(e);
return e;
}
Request<EitherError<void>>::Semaphore semaphore(r);
......@@ -468,7 +461,7 @@ ICloudProvider::DownloadFileRequest::Pointer MegaNz::getThumbnailAsync(
std::fstream cache_file(cache.c_str(),
std::fstream::in | std::fstream::binary);
if (!cache_file) {
callback->error({500, "Couldn't open cache file " + cache});
callback->done(Error{500, "couldn't open cache file " + cache});
return Error{500, "couldn't open cache file " + cache};
}
std::array<char, BUFFER_SIZE> buffer;
......@@ -476,7 +469,7 @@ ICloudProvider::DownloadFileRequest::Pointer MegaNz::getThumbnailAsync(
cache_file.read(buffer.data(), BUFFER_SIZE);
callback->receivedData(buffer.data(), cache_file.gcount());
} while (cache_file.gcount() > 0);
callback->done();
callback->done(nullptr);
} else {
cloudstorage::DownloadFileRequest::generateThumbnail(r, item, callback);
}
......@@ -492,7 +485,8 @@ ICloudProvider::DeleteItemRequest::Pointer MegaNz::deleteItemAsync(
r->set_resolver([this, item, callback](
Request<EitherError<void>>* r) -> EitherError<void> {
if (!ensureAuthorized(r)) {
Error e{401, "authrization failed"};
Error e = r->is_cancelled() ? Error{IHttpRequest::Aborted, ""}
: Error{401, "authorization failed"};
callback(e);
return e;
}
......@@ -521,7 +515,8 @@ ICloudProvider::CreateDirectoryRequest::Pointer MegaNz::createDirectoryAsync(
auto r = util::make_unique<Request<EitherError<IItem>>>(shared_from_this());
r->set_resolver([=](Request<EitherError<IItem>>* r) -> EitherError<IItem> {
if (!ensureAuthorized(r)) {
Error e{401, "authorization error"};
Error e = r->is_cancelled() ? Error{IHttpRequest::Aborted, ""}
: Error{401, "authorization failed"};
callback(e);
return e;
}
......@@ -558,9 +553,10 @@ ICloudProvider::MoveItemRequest::Pointer MegaNz::moveItemAsync(
auto r = util::make_unique<Request<EitherError<void>>>(shared_from_this());
r->set_resolver([=](Request<EitherError<void>>* r) -> EitherError<void> {
if (!ensureAuthorized(r)) {
Error error{401, "authorization error"};
callback(error);
return error;
Error e = r->is_cancelled() ? Error{IHttpRequest::Aborted, ""}
: Error{401, "authorization failed"};
callback(e);
return e;
}
std::unique_ptr<mega::MegaNode> source_node(
mega_->getNodeByPath(source->id().c_str()));
......@@ -589,9 +585,10 @@ ICloudProvider::RenameItemRequest::Pointer MegaNz::renameItemAsync(
auto r = util::make_unique<Request<EitherError<void>>>(shared_from_this());
r->set_resolver([=](Request<EitherError<void>>* r) -> EitherError<void> {
if (!ensureAuthorized(r)) {
Error error{401, "authorization error"};
callback(error);
return error;
Error e = r->is_cancelled() ? Error{IHttpRequest::Aborted, ""}
: Error{401, "authorization failed"};
callback(e);
return e;
}
std::unique_ptr<mega::MegaNode> node(
mega_->getNodeByPath(item->id().c_str()));
......@@ -619,11 +616,9 @@ MegaNz::downloadResolver(IItem::Pointer item,
return [this, item, callback, start,
size](Request<EitherError<void>>* r) -> EitherError<void> {
if (!ensureAuthorized(r)) {
Error e = {401, "authorization failed"};
if (!r->is_cancelled())
callback->error({401, "Authorization failed."});
else
e = {IHttpRequest::Aborted, ""};
Error e = r->is_cancelled() ? Error{IHttpRequest::Aborted, ""}
: Error{401, "authorization failed"};
callback->done(e);
return e;
}
std::unique_ptr<mega::MegaNode> node(
......@@ -640,13 +635,12 @@ MegaNz::downloadResolver(IItem::Pointer item,
while (listener.status_ == Listener::IN_PROGRESS) semaphore.wait();
mega_->removeTransferListener(&listener);
if (listener.status_ != Listener::SUCCESS) {
if (!r->is_cancelled()) {
callback->error({500, "Failed to download"});
return Error{500, "failed to download"};
} else
return Error{IHttpRequest::Aborted, ""};
Error e = r->is_cancelled() ? Error{IHttpRequest::Aborted, ""}
: Error{500, "failed to download"};
callback->done(e);
return e;
} else {
callback->done();
callback->done(nullptr);
return nullptr;
}
};
......
......@@ -45,9 +45,6 @@ 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, Error e) {
callback->error(e);
});
r->set_resolver([=](Request<EitherError<void>>* r) -> EitherError<void> {
std::stringstream output;
Error error;
......@@ -60,7 +57,7 @@ ICloudProvider::UploadFileRequest::Pointer OneDrive::uploadFileAsync(
},
output, &error);
if (!IHttpRequest::isSuccess(code)) {
callback->error(error);
callback->done(error);
return error;
}
Json::Value response;
......@@ -87,12 +84,12 @@ ICloudProvider::UploadFileRequest::Pointer OneDrive::uploadFileAsync(
callback->progress(size, sent + now);
});
if (!IHttpRequest::isSuccess(code)) {
callback->error(error);
callback->done(error);
return error;
}
sent += length;
}
callback->done();
callback->done(nullptr);
return nullptr;
});
return std::move(r);
......
......@@ -92,8 +92,6 @@ ICloudProvider::GetItemDataRequest::Pointer YandexDisk::getItemDataAsync(
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,
Error e) { callback->error(e); });
r->set_resolver([this, item, callback](
Request<EitherError<void>>* r) -> EitherError<void> {
std::stringstream output;
......@@ -107,7 +105,7 @@ ICloudProvider::DownloadFileRequest::Pointer YandexDisk::downloadFileAsync(
},
output, &error);
if (!IHttpRequest::isSuccess(code)) {
callback->error(error);
callback->done(error);
return error;
} else {
Json::Value json;
......@@ -121,7 +119,7 @@ ICloudProvider::DownloadFileRequest::Pointer YandexDisk::downloadFileAsync(
stream, &error,
std::bind(&IDownloadFileCallback::progress, callback.get(), _1, _2));
if (IHttpRequest::isSuccess(code)) {
callback->done();
callback->done(nullptr);
return nullptr;
} else {
return error;
......@@ -135,11 +133,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, Error e) {
callback->error(e);
});
r->set_resolver([this, directory, filename,
callback](Request<EitherError<void>>* r) {
r->set_resolver([this, directory, filename, callback](
Request<EitherError<void>>* r) -> EitherError<void> {
std::stringstream output;
Error error;
int code = r->sendRequest(
......@@ -170,14 +165,13 @@ ICloudProvider::UploadFileRequest::Pointer YandexDisk::uploadFileAsync(
},
output, &error,
std::bind(&IUploadFileCallback::progress, callback.get(), _1, _2));
if (IHttpRequest::isSuccess(code))
callback->done();
else
return error;
} else {
callback->error(error);
return error;
if (IHttpRequest::isSuccess(code)) {
callback->done(nullptr);
return nullptr;
}
}
callback->done(error);
return error;
});
return std::move(r);
}
......
......@@ -65,15 +65,11 @@ 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, 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(Error{403, "Trying to list non-directory."});
callback->done(Error{403, "Trying to list non-directory."});
return Error{403, "trying to list non-directory"};
}
std::string page_token;
......@@ -161,8 +157,6 @@ ICloudProvider::GetItemDataRequest::Pointer YouTube::getItemDataAsync(
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,
Error e) { callback->error(e); });
r->set_resolver([this, item, callback](
Request<EitherError<void>>* r) -> EitherError<void> {
std::string url = item->url();
......@@ -185,9 +179,10 @@ ICloudProvider::DownloadFileRequest::Pointer YouTube::downloadFileAsync(
stream, &error,
std::bind(&IDownloadFileCallback::progress, callback.get(), _1, _2));
if (IHttpRequest::isSuccess(code)) {
callback->done();
callback->done(nullptr);
return nullptr;
} else {
callback->done(error);
return error;
}
});
......
......@@ -32,10 +32,12 @@
namespace cloudstorage {
struct Error {
int code_;
std::string description_;
};
struct Error;
template <class Left, class Right> class Either;
template <class T>
using EitherError = Either<Error, T>;
/**
* Class representing pending request. When there is no reference to the
......@@ -87,12 +89,7 @@ class IListDirectoryCallback {
*
* @param result contains all retrieved children
*/
virtual void done(const std::vector<IItem::Pointer>& result) = 0;
/**
* Called when error occurred.
*/
virtual void error(Error) = 0;
virtual void done(EitherError<std::vector<IItem::Pointer>>) = 0;
};
class IDownloadFileCallback {
......@@ -112,12 +109,7 @@ class IDownloadFileCallback {
/**
* Called when the download has finished.
*/
virtual void done() = 0;
/**
* Called when error occurred.
*/
virtual void error(Error) = 0;
virtual void done(EitherError<void>) = 0;
/**
* Called when progress has changed.
......@@ -157,12 +149,7 @@ class IUploadFileCallback {
/**
* Called when the upload is finished sucessfully.
*/
virtual void done() = 0;
/**
* Called when error occurred.
*/
virtual void error(Error) = 0;
virtual void done(EitherError<void>) = 0;
/**
* Called when upload progress changed.
......@@ -173,6 +160,11 @@ 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:
......@@ -202,9 +194,6 @@ class Either<Left, void> {
std::shared_ptr<Left> left_;
};
template <class T>
using EitherError = Either<Error, T>;
using ExchangeCodeCallback = std::function<void(EitherError<std::string>)>;
using GetItemCallback = std::function<void(EitherError<IItem>)>;
using GetItemDataCallback = std::function<void(EitherError<IItem>)>;
......
......@@ -50,7 +50,7 @@ DownloadFileRequest::DownloadFileRequest(std::shared_ptr<CloudProvider> p,
std::bind(&DownloadFileRequest::ICallback::progress, callback_.get(),
_1, _2));
if (IHttpRequest::isSuccess(code)) {
callback_->done();
callback_->done(nullptr);
return nullptr;
} else if (fallback_thumbnail_) {
generateThumbnail(r, file_, callback_);
......@@ -73,15 +73,15 @@ void DownloadFileRequest::generateThumbnail(
semaphore.wait();
if (r->is_cancelled()) return item_data->cancel();
if (!item_data->result().right())
return callback->error(*item_data->result().left());
return callback->done(item_data->result().left());
auto thumbnail_data = r->provider()->thumbnailer()->generateThumbnail(
r->provider(), item_data->result().right(),
[&semaphore, callback](EitherError<std::vector<char>> data) {
if (data.right()) {
callback->receivedData(data.right()->data(), data.right()->size());
callback->done();
callback->done(nullptr);
} else {
callback->error(*data.left());
callback->done(data.left());
}
semaphore.notify();
});
......@@ -89,10 +89,6 @@ void DownloadFileRequest::generateThumbnail(
if (r->is_cancelled()) thumbnail_data->cancel();
}
void DownloadFileRequest::error(int code, const std::string& description) {
if (!fallback_thumbnail_) callback_->error(Error{code, description});
}
DownloadStreamWrapper::DownloadStreamWrapper(
std::function<void(const char*, uint32_t)> callback)
: callback_(std::move(callback)) {}
......
......@@ -54,9 +54,6 @@ class DownloadFileRequest : public Request<EitherError<void>> {
static void generateThumbnail(Request<EitherError<void>>*, IItem::Pointer,
ICallback::Pointer);
protected:
void error(int code, const std::string& description) override;
private:
IItem::Pointer file_;
DownloadStreamWrapper stream_wrapper_;
......
......@@ -35,8 +35,9 @@ ListDirectoryRequest::ListDirectoryRequest(std::shared_ptr<CloudProvider> p,
callback_(std::move(callback)) {
set_resolver([this](Request*) -> EitherError<std::vector<IItem::Pointer>> {
if (directory_->type() != IItem::FileType::Directory) {
callback_->error({403, "Trying to list non-directory."});
return Error{403, "trying to list non directory"};
Error e{403, "trying to list non directory"};
callback_->done(e);
return e;
}
std::string page_token;
std::vector<IItem::Pointer> result;
......@@ -63,6 +64,7 @@ ListDirectoryRequest::ListDirectoryRequest(std::shared_ptr<CloudProvider> p,
callback_->done(result);
return result;
} else {
callback_->done(error);
return error;
}
});
......@@ -70,8 +72,4 @@ ListDirectoryRequest::ListDirectoryRequest(std::shared_ptr<CloudProvider> p,
ListDirectoryRequest::~ListDirectoryRequest() { cancel(); }
void ListDirectoryRequest::error(int code, const std::string& description) {
callback_->error(Error{code, description});
}
} // namespace cloudstorage
......@@ -38,9 +38,6 @@ class ListDirectoryRequest
ICallback::Pointer);
~ListDirectoryRequest();
protected:
void error(int code, const std::string& description) override;
private:
IItem::Pointer directory_;
ICallback::Pointer callback_;
......
......@@ -60,11 +60,6 @@ void Request<T>::set_resolver(Resolver resolver) {
function_ = std::async(std::launch::async, std::bind(resolver, this));
}
template <class T>
void Request<T>::set_error_callback(ErrorCallback f) {
error_callback_ = f;
}
template <class T>
void Request<T>::set_cancel_callback(CancelCallback f) {
cancel_callback_ = f;
......@@ -130,26 +125,6 @@ bool Request<T>::reauthorize() {
return ret;
}
template <class T>
void Request<T>::error(int code, const std::string& desc) {
if (error_callback_) error_callback_(this, Error{code, desc});
}
template <class T>
std::string Request<T>::error_string(int code, const std::string& desc) const {
std::stringstream stream;
if (code < 0) {
code *= -1;
stream << "HTTP library error " << code;
auto p = provider();
if (p) stream << ": " << p->http()->error(code);
} else {
stream << "HTTP code " << code;
if (isPrintable(desc)) stream << ": " << desc;
}
return stream.str();
}
template <class T>
int Request<T>::sendRequest(
std::function<IHttpRequest::Pointer(std::ostream&)> factory,
......@@ -164,20 +139,13 @@ int Request<T>::sendRequest(
send(request.get(), input, output, &error_stream, download, upload);
if (IHttpRequest::isSuccess(code)) return code;
if (p->reauthorize(code)) {
if (!reauthorize()) {
if (!is_cancelled()) this->error(code, error_stream.str());
} else {
if (reauthorize()) {<