Change api error structure
This commit is contained in:
@@ -35,21 +35,13 @@ SString CSiaCurl::UrlEncode(const SString& data, const bool& allowSlash)
|
||||
return ret;
|
||||
}
|
||||
|
||||
SiaCurlError CSiaCurl::CheckApiError(const json& result)
|
||||
SString CSiaCurl::GetApiErrorMessage(const json& result)
|
||||
{
|
||||
SiaCurlError ret = SiaCurlError::Success;
|
||||
|
||||
SString ret;
|
||||
if (result.find("message") != result.end())
|
||||
{
|
||||
ret = SiaCurlError::UnknownFailure;
|
||||
|
||||
const std::string msg = result["message"].get<std::string>();
|
||||
if ((msg.length() >= 3))
|
||||
{
|
||||
if ((msg.substr(0, 3) == "404"))
|
||||
{
|
||||
ret = SiaCurlError::HttpError;
|
||||
}
|
||||
}
|
||||
ret = result["message"].get<std::string>();
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -61,40 +53,29 @@ std::string CSiaCurl::ConstructPath(const SString& relativePath) const
|
||||
return ret;
|
||||
}
|
||||
|
||||
SiaCurlError CSiaCurl::CheckHttpError(const std::string& result)
|
||||
{
|
||||
if (result.length() && ((result.length() < 2) || (result[0] != '{')))
|
||||
{
|
||||
return SiaCurlError::HttpError;
|
||||
}
|
||||
|
||||
return SiaCurlError::Success;
|
||||
}
|
||||
|
||||
SiaCurlError CSiaCurl::ProcessResponse(const int& res, const int& httpCode, const std::string& result, json& response) const
|
||||
{
|
||||
SiaCurlError ret;
|
||||
SiaCurlError ret;
|
||||
if ((res == CURLE_OK) && ((httpCode >= 200) && (httpCode <300)))
|
||||
{
|
||||
ret = CheckHttpError(result);
|
||||
if (ApiSuccess(ret))
|
||||
{
|
||||
ret = (result.length() ? CheckApiError((response = json::parse(result.c_str()))) : SiaCurlError::Success);
|
||||
}
|
||||
if (result.length())
|
||||
{
|
||||
response = json::parse(result.c_str());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((res == CURLE_COULDNT_RESOLVE_HOST) || (res == CURLE_COULDNT_CONNECT))
|
||||
{
|
||||
ret = SiaCurlError::NoResponse;
|
||||
ret = SiaCurlErrorCode::NoResponse;
|
||||
}
|
||||
else if (httpCode)
|
||||
{
|
||||
ret = SiaCurlError::HttpError;
|
||||
ret = { SiaCurlErrorCode::HttpError, GetApiErrorMessage(result) };
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = SiaCurlError::UnknownFailure;
|
||||
ret = { SiaCurlErrorCode::UnknownFailure, "Unknown curl error" };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,14 +121,14 @@ SiaCurlError CSiaCurl::_Get(const SString& path, const HttpParameters& parameter
|
||||
|
||||
bool CSiaCurl::CheckVersion(SiaCurlError& error) const
|
||||
{
|
||||
error = SiaCurlError::InvalidRequiredVersion;
|
||||
error = SiaCurlErrorCode::InvalidRequiredVersion;
|
||||
if (GetHostConfig().RequiredVersion.Length())
|
||||
{
|
||||
error = SiaCurlError::NoResponse;
|
||||
error = SiaCurlErrorCode::NoResponse;
|
||||
const SString serverVersion = GetServerVersion();
|
||||
if (serverVersion.Length())
|
||||
{
|
||||
error = (serverVersion == GetHostConfig().RequiredVersion) ? SiaCurlError::Success : SiaCurlError::ServerVersionMismatch;
|
||||
error = (serverVersion == GetHostConfig().RequiredVersion) ? SiaCurlErrorCode::Success : SiaCurlErrorCode::ServerVersionMismatch;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -103,7 +103,7 @@ void CSiaApi::_CSiaRenter::Refresh(const CSiaCurl& siaCurl, CSiaDriveConfig* sia
|
||||
std::uint32_t totalProgress = std::accumulate(std::next(fileList.begin()), fileList.end(), fileList[0]->GetUploadProgress(), [](const std::uint32_t& progress, const CSiaFilePtr& file)
|
||||
{
|
||||
return progress + min(100, file->GetUploadProgress());
|
||||
}) / fileList.size();
|
||||
}) / static_cast<std::uint32_t>(fileList.size());
|
||||
|
||||
SetTotalUsedBytes(total);
|
||||
SetTotalUploadProgress(totalProgress);
|
||||
@@ -135,10 +135,11 @@ void CSiaApi::_CSiaRenter::Refresh(const CSiaCurl& siaCurl, CSiaDriveConfig* sia
|
||||
|
||||
SiaApiError CSiaApi::_CSiaRenter::RefreshFileTree( )
|
||||
{
|
||||
SiaApiError ret = SiaApiError::RequestError;
|
||||
SiaApiError ret;
|
||||
CSiaFileTreePtr tempTree(new CSiaFileTree(GetSiaCurl(), &GetSiaDriveConfig()));
|
||||
json result;
|
||||
if (ApiSuccess(GetSiaCurl().Get(L"/renter/files", result)))
|
||||
json result;
|
||||
SiaCurlError cerror = GetSiaCurl().Get(L"/renter/files", result);
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
tempTree->BuildTree(result);
|
||||
{
|
||||
@@ -146,6 +147,10 @@ SiaApiError CSiaApi::_CSiaRenter::RefreshFileTree( )
|
||||
_fileTree = tempTree;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = { SiaApiErrorCode::RequestError, cerror.GetReason() };
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -158,16 +163,18 @@ SiaApiError CSiaApi::_CSiaRenter::FileExists(const SString& siaPath, bool& exist
|
||||
{
|
||||
exists = siaFileTree->FileExists(siaPath);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SiaApiError CSiaApi::_CSiaRenter::DownloadFile(const SString& siaPath, const SString& location) const
|
||||
{
|
||||
SiaApiError ret = SiaApiError::RequestError;
|
||||
SiaApiError ret;
|
||||
json result;
|
||||
if (ApiSuccess(GetSiaCurl().Get(L"/renter/download/" + siaPath, { { L"destination", location } }, result)))
|
||||
auto cerror = GetSiaCurl().Get(L"/renter/download/" + siaPath, { { L"destination", location } }, result);
|
||||
if (!ApiSuccess(cerror))
|
||||
{
|
||||
ret = SiaApiError::Success;
|
||||
ret = { SiaApiErrorCode::RequestError, cerror.GetReason() };
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -181,7 +188,7 @@ SiaApiError CSiaApi::_CSiaRenter::GetFileTree(CSiaFileTreePtr& siaFileTree) cons
|
||||
siaFileTree.reset(new CSiaFileTree(GetSiaCurl(), &GetSiaDriveConfig()));
|
||||
}
|
||||
|
||||
return SiaApiError::Success;
|
||||
return SiaApiErrorCode::Success;
|
||||
}
|
||||
|
||||
SiaRenterAllowance CSiaApi::_CSiaRenter::GetAllowance() const
|
||||
@@ -191,18 +198,19 @@ SiaRenterAllowance CSiaApi::_CSiaRenter::GetAllowance() const
|
||||
|
||||
SiaApiError CSiaApi::_CSiaRenter::SetAllowance(const SiaRenterAllowance& renterAllowance)
|
||||
{
|
||||
SiaApiError ret = SiaApiError::RequestError;
|
||||
SiaApiError ret;
|
||||
|
||||
json result;
|
||||
if (ApiSuccess(GetSiaCurl().Post(L"/renter",
|
||||
auto cerror = GetSiaCurl().Post(L"/renter",
|
||||
{
|
||||
{ "funds", SiaCurrencyToHastingsString(renterAllowance.Funds) },
|
||||
{ "hosts", SString::FromUInt64(renterAllowance.Hosts) },
|
||||
{ "period", SString::FromUInt64(renterAllowance.Period) },
|
||||
{ "renewwindow", SString::FromUInt64 (renterAllowance.RenewWindowInBlocks) }
|
||||
}, result)))
|
||||
}, result);
|
||||
if (!ApiSuccess(cerror))
|
||||
{
|
||||
ret = SiaApiError::Success;
|
||||
ret = { SiaApiErrorCode::RequestError, cerror.GetReason() };
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
@@ -83,20 +83,26 @@ void CSiaApi::_CSiaWallet::Refresh(const CSiaCurl& siaCurl, CSiaDriveConfig* sia
|
||||
|
||||
SiaApiError CSiaApi::_CSiaWallet::Create(const SiaSeedLanguage& seedLanguage, SString& seed)
|
||||
{
|
||||
SiaApiError error = SiaApiError::RequestError;
|
||||
SiaApiError error = SiaApiErrorCode::NotConnected;
|
||||
if (GetConnected())
|
||||
{
|
||||
error = SiaApiError::WalletExists;
|
||||
if (!GetCreated())
|
||||
if (GetCreated())
|
||||
{
|
||||
error = SiaApiErrorCode::WalletExists;
|
||||
}
|
||||
else
|
||||
{
|
||||
error = SiaApiError::RequestError;
|
||||
json result;
|
||||
SiaCurlError cerror = GetSiaCurl().Post(L"/wallet/init", { {L"dictionary", SeedLangToString(seedLanguage)} }, result);
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
error = SiaApiError::Success;
|
||||
error = SiaApiErrorCode::Success;
|
||||
seed = result["primaryseed"].get<std::string>();
|
||||
}
|
||||
else
|
||||
{
|
||||
error = { SiaApiErrorCode::RequestError, cerror.GetReason() };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,24 +111,26 @@ SiaApiError CSiaApi::_CSiaWallet::Create(const SiaSeedLanguage& seedLanguage, SS
|
||||
|
||||
SiaApiError CSiaApi::_CSiaWallet::Restore(const SString& seed)
|
||||
{
|
||||
SiaApiError error = SiaApiError::NotImplemented;
|
||||
SiaApiError error = SiaApiErrorCode::NotImplemented;
|
||||
// TODO Future enhancement
|
||||
return error;
|
||||
}
|
||||
|
||||
SiaApiError CSiaApi::_CSiaWallet::Lock()
|
||||
{
|
||||
SiaApiError error = GetCreated() ? (GetLocked() ? SiaApiError::WalletLocked : SiaApiError::Success) : SiaApiError::WalletNotCreated;
|
||||
SiaApiError error = GetCreated() ? (GetLocked() ? SiaApiErrorCode::WalletLocked : SiaApiErrorCode::Success) : SiaApiErrorCode::WalletNotCreated;
|
||||
if (ApiSuccess(error))
|
||||
{
|
||||
error = SiaApiError::RequestError;
|
||||
|
||||
json result;
|
||||
SiaCurlError cerror = GetSiaCurl().Post(L"/wallet/lock", {}, result);
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
error = SiaApiError::Success;
|
||||
error = SiaApiErrorCode::Success;
|
||||
}
|
||||
else
|
||||
{
|
||||
error = { SiaApiErrorCode::RequestError, cerror.GetReason() };
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
@@ -130,17 +138,19 @@ SiaApiError CSiaApi::_CSiaWallet::Lock()
|
||||
|
||||
SiaApiError CSiaApi::_CSiaWallet::Unlock(const SString& password)
|
||||
{
|
||||
SiaApiError error = GetCreated() ? (GetLocked() ? SiaApiError::Success : SiaApiError::WalletUnlocked) : SiaApiError::WalletNotCreated;
|
||||
SiaApiError error = GetCreated() ? (GetLocked() ? SiaApiErrorCode::Success : SiaApiErrorCode::WalletUnlocked) : SiaApiErrorCode::WalletNotCreated;
|
||||
if (ApiSuccess(error))
|
||||
{
|
||||
error = SiaApiError::RequestError;
|
||||
|
||||
json result;
|
||||
SiaCurlError cerror = GetSiaCurl().Post(L"/wallet/unlock", { {L"encryptionpassword", password} }, result);
|
||||
if (ApiSuccess(cerror))
|
||||
{
|
||||
error = SiaApiError::Success;
|
||||
error = SiaApiErrorCode::Success;
|
||||
}
|
||||
else
|
||||
{
|
||||
error = { SiaApiErrorCode::RequestError, cerror.GetReason() };
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
|
@@ -387,7 +387,7 @@ UploadStatus CUploadManager::GetUploadStatus(const SString& siaPath)
|
||||
|
||||
UploadError CUploadManager::AddOrUpdate(const SString& siaPath, SString filePath)
|
||||
{
|
||||
UploadError ret = UploadError::Success;
|
||||
UploadError ret;
|
||||
|
||||
// Relative to absolute and grab parent folder of source
|
||||
FilePath rootPath = filePath;
|
||||
@@ -439,26 +439,26 @@ UploadError CUploadManager::AddOrUpdate(const SString& siaPath, SString filePath
|
||||
else
|
||||
{
|
||||
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseInsertFailed(siaPath, filePath, insert.getErrorMsg())));
|
||||
ret = UploadError::DatabaseError;
|
||||
ret = UploadErrorCode::DatabaseError;
|
||||
}
|
||||
}
|
||||
catch (SQLite::Exception e)
|
||||
{
|
||||
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseExceptionOccurred("AddOrUpdate(insert)", e)));
|
||||
ret = UploadError::DatabaseError;
|
||||
ret = { UploadErrorCode::DatabaseError, e.getErrorStr() };
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SQLite::Exception e)
|
||||
{
|
||||
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseExceptionOccurred("AddOrUpdate(query)", e)));
|
||||
ret = UploadError::DatabaseError;
|
||||
ret = { UploadErrorCode::DatabaseError, e.getErrorStr() };
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(SourceFileNotFound(siaPath, filePath)));
|
||||
ret = UploadError::SourceFileNotFound;
|
||||
ret = UploadErrorCode::SourceFileNotFound;
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -466,7 +466,7 @@ UploadError CUploadManager::AddOrUpdate(const SString& siaPath, SString filePath
|
||||
|
||||
UploadError CUploadManager::Remove(const SString& siaPath)
|
||||
{
|
||||
UploadError ret = UploadError::Success;
|
||||
UploadError ret;
|
||||
bool remove = false;
|
||||
SString siaDriveFilePath;
|
||||
try
|
||||
@@ -506,7 +506,7 @@ UploadError CUploadManager::Remove(const SString& siaPath)
|
||||
catch (SQLite::Exception e)
|
||||
{
|
||||
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseExceptionOccurred("Remove", e)));
|
||||
ret = UploadError::DatabaseError;
|
||||
ret = { UploadErrorCode::DatabaseError, e.getErrorStr() };
|
||||
}
|
||||
|
||||
if (remove)
|
||||
|
Reference in New Issue
Block a user