Commit 8c14bbdc authored by Matthieu Dorier's avatar Matthieu Dorier
Browse files

added a proper sonata exception class

parent 22ee54dc
......@@ -5,6 +5,7 @@
#include <memory>
#include <unordered_set>
#include <sonata/Collection.hpp>
#include <sonata/Exception.hpp>
namespace sonata {
......
#ifndef __SONATA_EXCEPTION_HPP
#define __SONATA_EXCEPTION_HPP
#include <exception>
#include <string>
namespace sonata {
class Exception : public std::exception {
const std::string& m_error;
public:
template<typename ... Args>
Exception(Args&&... args)
: m_error(std::forward<Args>(args)...) {}
virtual const char* what() const noexcept override {
return m_error.c_str();
}
};
}
#endif
#include "sonata/Admin.hpp"
#include "sonata/Exception.hpp"
#include "sonata/RequestResult.hpp"
#include "AdminImpl.hpp"
......@@ -36,7 +37,7 @@ void Admin::createDatabase(const std::string& address,
auto ph = tl::provider_handle(endpoint, provider_id);
RequestResult<bool> result = self->m_create_database.on(ph)(self->m_token, db_name, db_type, db_config);
if(not result.success()) {
throw std::runtime_error(result.error());
throw Exception(result.error());
}
}
......@@ -57,7 +58,7 @@ void Admin::attachDatabase(const std::string& address,
auto ph = tl::provider_handle(endpoint, provider_id);
RequestResult<bool> result = self->m_attach_database.on(ph)(self->m_token, db_name, db_type, db_config);
if(not result.success()) {
throw std::runtime_error(result.error());
throw Exception(result.error());
}
}
......@@ -76,7 +77,7 @@ void Admin::detachDatabase(const std::string& address,
auto ph = tl::provider_handle(endpoint, provider_id);
RequestResult<bool> result = self->m_detach_database.on(ph)(self->m_token, db_name);
if(not result.success()) {
throw std::runtime_error(result.error());
throw Exception(result.error());
}
}
......@@ -87,7 +88,7 @@ void Admin::destroyDatabase(const std::string& address,
auto ph = tl::provider_handle(endpoint, provider_id);
RequestResult<bool> result = self->m_destroy_database.on(ph)(self->m_token, db_name);
if(not result.success()) {
throw std::runtime_error(result.error());
throw Exception(result.error());
}
}
......
#include "sonata/Exception.hpp"
#include "sonata/Client.hpp"
#include "sonata/Database.hpp"
#include "sonata/Collection.hpp"
......@@ -40,7 +41,7 @@ Database Client::open(const std::string& address,
auto db_impl = std::make_shared<DatabaseImpl>(self, std::move(ph), db_name);
return Database(db_impl);
} else {
throw std::runtime_error(result.error());
throw Exception(result.error());
return Database(nullptr);
}
}
......
#include "sonata/Collection.hpp"
#include "sonata/RequestResult.hpp"
#include "sonata/Exception.hpp"
#include "ClientImpl.hpp"
#include "DatabaseImpl.hpp"
......@@ -28,7 +29,7 @@ Collection::operator bool() const {
}
uint64_t Collection::store(const std::string& record) const {
if(not self) throw std::runtime_error("Invalid sonata::Collection object");
if(not self) throw Exception("Invalid sonata::Collection object");
auto& rpc = self->m_database->m_client->m_coll_store;
auto& ph = self->m_database->m_ph;
auto& db_name = self->m_database->m_name;
......@@ -36,7 +37,7 @@ uint64_t Collection::store(const std::string& record) const {
if(result.success()) {
return result.value();
} else {
throw std::runtime_error(result.error());
throw Exception(result.error());
}
return 0;
}
......@@ -47,7 +48,7 @@ uint64_t Collection::store(const Json::Value& record) const {
void Collection::fetch(uint64_t id, std::string* out) const {
if(not out) return;
if(not self) throw std::runtime_error("Invalid sonata::Collection object");
if(not self) throw Exception("Invalid sonata::Collection object");
auto& rpc = self->m_database->m_client->m_coll_fetch;
auto& ph = self->m_database->m_ph;
auto& db_name = self->m_database->m_name;
......@@ -55,7 +56,7 @@ void Collection::fetch(uint64_t id, std::string* out) const {
if(result.success()) {
*out = std::move(result.value());
} else {
throw std::runtime_error(result.error());
throw Exception(result.error());
}
}
......@@ -71,12 +72,12 @@ void Collection::fetch(uint64_t id, Json::Value* result) const {
&errors);
if(!parsingSuccessful) {
throw std::runtime_error(errors);
throw Exception(errors);
}
}
void Collection::filter(const std::string& filterCode, std::vector<std::string>* out) const {
if(not self) throw std::runtime_error("Invalid sonata::Collection object");
if(not self) throw Exception("Invalid sonata::Collection object");
auto& rpc = self->m_database->m_client->m_coll_filter;
auto& ph = self->m_database->m_ph;
auto& db_name = self->m_database->m_name;
......@@ -84,7 +85,7 @@ void Collection::filter(const std::string& filterCode, std::vector<std::string>*
if(result.success()) {
if(out) *out = std::move(result.value());
} else {
throw std::runtime_error(result.error());
throw Exception(result.error());
}
}
......@@ -105,7 +106,7 @@ void Collection::filter(const std::string& filterCode, Json::Value* result) cons
&tmp,
&errors);
if(!parsingSuccessful) {
throw std::runtime_error(errors);
throw Exception(errors);
}
tmp_array[i] = std::move(tmp);
}
......@@ -114,13 +115,13 @@ void Collection::filter(const std::string& filterCode, Json::Value* result) cons
}
void Collection::update(uint64_t id, const std::string& record) const {
if(not self) throw std::runtime_error("Invalid sonata::Collection object");
if(not self) throw Exception("Invalid sonata::Collection object");
auto& rpc = self->m_database->m_client->m_coll_update;
auto& ph = self->m_database->m_ph;
auto& db_name = self->m_database->m_name;
RequestResult<bool> result = rpc.on(ph)(db_name, self->m_name, id, record);
if(result.success()) return;
else throw std::runtime_error(result.error());
else throw Exception(result.error());
}
void Collection::update(uint64_t id, const Json::Value& record) const {
......@@ -128,7 +129,7 @@ void Collection::update(uint64_t id, const Json::Value& record) const {
}
void Collection::all(std::vector<std::string>* out) const {
if(not self) throw std::runtime_error("Invalid sonata::Collection object");
if(not self) throw Exception("Invalid sonata::Collection object");
auto& rpc = self->m_database->m_client->m_coll_all;
auto& ph = self->m_database->m_ph;
auto& db_name = self->m_database->m_name;
......@@ -136,7 +137,7 @@ void Collection::all(std::vector<std::string>* out) const {
if(result.success()) {
if(out) *out = std::move(result.value());
} else {
throw std::runtime_error(result.error());
throw Exception(result.error());
}
}
......@@ -154,7 +155,7 @@ void Collection::all(Json::Value* result) const {
&tmp,
&errors);
if(!parsingSuccessful) {
throw std::runtime_error(errors);
throw Exception(errors);
}
tmp_array[i] = std::move(tmp);
}
......@@ -163,35 +164,35 @@ void Collection::all(Json::Value* result) const {
}
uint64_t Collection::last_record_id() const {
if(not self) throw std::runtime_error("Invalid sonata::Collection object");
if(not self) throw Exception("Invalid sonata::Collection object");
auto& rpc = self->m_database->m_client->m_coll_last_id;
auto& ph = self->m_database->m_ph;
auto& db_name = self->m_database->m_name;
RequestResult<uint64_t> result = rpc.on(ph)(db_name, self->m_name);
if(not result.success())
throw std::runtime_error(result.error());
throw Exception(result.error());
return result.value();
}
size_t Collection::size() const {
if(not self) throw std::runtime_error("Invalid sonata::Collection object");
if(not self) throw Exception("Invalid sonata::Collection object");
auto& rpc = self->m_database->m_client->m_coll_size;
auto& ph = self->m_database->m_ph;
auto& db_name = self->m_database->m_name;
RequestResult<size_t> result = rpc.on(ph)(db_name, self->m_name);
if(not result.success())
throw std::runtime_error(result.error());
throw Exception(result.error());
return result.value();
}
void Collection::erase(uint64_t id) const {
if(not self) throw std::runtime_error("Invalid sonata::Collection object");
if(not self) throw Exception("Invalid sonata::Collection object");
auto& rpc = self->m_database->m_client->m_coll_erase;
auto& ph = self->m_database->m_ph;
auto& db_name = self->m_database->m_name;
RequestResult<bool> result = rpc.on(ph)(db_name, self->m_name, id);
if(not result.success()) {
throw std::runtime_error(result.error());
throw Exception(result.error());
}
}
......
......@@ -28,39 +28,39 @@ Database::operator bool() const {
}
Collection Database::create(const std::string& collectionName) const {
if(not self) throw std::runtime_error("Invalid sonata::Database object");
if(not self) throw Exception("Invalid sonata::Database object");
RequestResult<bool> result = self->m_client->m_create_collection.on(self->m_ph)(self->m_name, collectionName);
if(result.success()) {
auto coll_impl = std::make_shared<CollectionImpl>(self, collectionName);
return Collection(coll_impl);
} else {
throw std::runtime_error(result.error());
throw Exception(result.error());
return Collection(nullptr);
}
}
Collection Database::open(const std::string& collectionName) const {
if(not self) throw std::runtime_error("Invalid sonata::Database object");
if(not self) throw Exception("Invalid sonata::Database object");
RequestResult<bool> result = self->m_client->m_open_collection.on(self->m_ph)(self->m_name, collectionName);
if(result.success()) {
auto coll_impl = std::make_shared<CollectionImpl>(self, collectionName);
return Collection(coll_impl);
} else {
throw std::runtime_error(result.error());
throw Exception(result.error());
return Collection(nullptr);
}
}
void Database::drop(const std::string& collectionName) const {
if(not self) throw std::runtime_error("Invalid sonata::Database object");
if(not self) throw Exception("Invalid sonata::Database object");
RequestResult<bool> result = self->m_client->m_drop_collection.on(self->m_ph)(self->m_name, collectionName);
if(not result.success()) {
throw std::runtime_error(result.error());
throw Exception(result.error());
}
}
bool Database::exists(const std::string& collectionName) const {
if(not self) throw std::runtime_error("Invalid sonata::Database object");
if(not self) throw Exception("Invalid sonata::Database object");
RequestResult<bool> result = self->m_client->m_open_collection.on(self->m_ph)(self->m_name, collectionName);
return result.success();
}
......@@ -68,11 +68,11 @@ bool Database::exists(const std::string& collectionName) const {
void Database::execute(const std::string& code,
const std::unordered_set<std::string>& vars,
std::unordered_map<std::string,std::string>* out) const {
if(not self) throw std::runtime_error("Invalid sonata::Database object");
if(not self) throw Exception("Invalid sonata::Database object");
RequestResult<std::unordered_map<std::string,std::string>> result
= self->m_client->m_execute_on_database.on(self->m_ph)(self->m_name, code, vars);
if(not result.success()) {
throw std::runtime_error(result.error());
throw Exception(result.error());
}
if(out)
*out = std::move(result.value());
......@@ -81,7 +81,7 @@ void Database::execute(const std::string& code,
void Database::execute(const std::string& code,
const std::unordered_set<std::string>& vars,
Json::Value* result) const {
if(not self) throw std::runtime_error("Invalid sonata::Database object");
if(not self) throw Exception("Invalid sonata::Database object");
std::unordered_map<std::string,std::string> ret;
if(result) {
std::unordered_map<std::string,std::string> ret;
......@@ -97,7 +97,7 @@ void Database::execute(const std::string& code,
&tmp,
&errors);
if(!parsingSuccessful) {
throw std::runtime_error(errors);
throw Exception(errors);
}
tmp_result[name] = std::move(tmp);
}
......
......@@ -56,7 +56,7 @@ class UnQLiteBackend : public Backend {
if(!result.success()) {
result.error() = vm.get<std::string>("err");
}
} catch(const std::runtime_error& e) {
} catch(const Exception& e) {
result.success() = false;
result.error() = e.what();
}
......@@ -78,7 +78,7 @@ class UnQLiteBackend : public Backend {
vm.execute();
result.success() = vm.get<bool>("ret");
result.error() = "Collection"s + coll_name + " does not exist";
} catch(const std::runtime_error& e) {
} catch(const Exception& e) {
result.success() = false;
result.error() = e.what();
}
......@@ -107,7 +107,7 @@ class UnQLiteBackend : public Backend {
if(!result.success()) {
result.error() = vm.get<std::string>("err");
}
} catch(const std::runtime_error& e) {
} catch(const Exception& e) {
result.success() = false;
result.error() = e.what();
}
......@@ -142,7 +142,7 @@ class UnQLiteBackend : public Backend {
} else {
result.value() = vm.get<uint64_t>("id");
}
} catch(const std::runtime_error& e) {
} catch(const Exception& e) {
result.success() = false;
result.error() = e.what();
}
......@@ -179,7 +179,7 @@ class UnQLiteBackend : public Backend {
vm["output"].printToStream(ss);
result.value() = ss.str();
}
} catch(const std::runtime_error& e) {
} catch(const Exception& e) {
result.success() = false;
result.error() = e.what();
}
......@@ -221,7 +221,7 @@ class UnQLiteBackend : public Backend {
});
result.value() = std::move(array);
}
} catch(const std::runtime_error& e) {
} catch(const Exception& e) {
result.success() = false;
result.error() = e.what();
}
......@@ -254,7 +254,7 @@ class UnQLiteBackend : public Backend {
if(!result.success()) {
result.error() = vm.get<std::string>("err");
}
} catch(const std::runtime_error& e) {
} catch(const Exception& e) {
result.success() = false;
result.error() = e.what();
}
......@@ -294,7 +294,7 @@ class UnQLiteBackend : public Backend {
});
result.value() = std::move(array);
}
} catch(const std::runtime_error& e) {
} catch(const Exception& e) {
result.success() = false;
result.error() = e.what();
}
......@@ -327,7 +327,7 @@ class UnQLiteBackend : public Backend {
} else {
result.value() = vm["id"];
}
} catch(const std::runtime_error& e) {
} catch(const Exception& e) {
result.success() = false;
result.error() = e.what();
}
......@@ -360,7 +360,7 @@ class UnQLiteBackend : public Backend {
} else {
result.value() = vm["size"];
}
} catch(const std::runtime_error& e) {
} catch(const Exception& e) {
result.success() = false;
result.error() = e.what();
}
......@@ -393,7 +393,7 @@ class UnQLiteBackend : public Backend {
if(!result.success()) {
result.error() = vm.get<std::string>("err");
}
} catch(const std::runtime_error& e) {
} catch(const Exception& e) {
result.success() = false;
result.error() = e.what();
}
......@@ -414,7 +414,7 @@ class UnQLiteBackend : public Backend {
val.printToStream(ss);
result.value().emplace(name, ss.str());
}
} catch(const std::runtime_error& e) {
} catch(const Exception& e) {
result.success() = false;
result.error() = e.what();
result.value().clear();
......@@ -445,12 +445,12 @@ std::unique_ptr<Backend> UnQLiteBackend::create(const Json::Value& config) {
bool temporary = config.get("temporary", false).asBool();
bool inmemory = config.get("in-memory", false).asBool();
if((not config.isMember("path")) && not inmemory)
throw std::runtime_error("UnQLiteBackend needs to be initialized with a path");
throw Exception("UnQLiteBackend needs to be initialized with a path");
std::string db_path = config.get("path","").asString();
if(db_path.size() > 0) {
std::ifstream f(db_path.c_str());
if(f.good()) {
throw std::runtime_error("Database file "s + db_path + " already exists");
throw Exception("Database file "s + db_path + " already exists");
}
}
unqlite* pDB;
......@@ -463,7 +463,7 @@ std::unique_ptr<Backend> UnQLiteBackend::create(const Json::Value& config) {
if(temporary) mode = mode | UNQLITE_OPEN_TEMP_DB;
ret = unqlite_open(&pDB, db_path.c_str(), mode);
if(ret != UNQLITE_OK) {
throw std::runtime_error("Could not open or create database at "s + db_path);
throw Exception("Could not open or create database at "s + db_path);
}
// forcing the file to be created
unqlite_kv_store(pDB,"___",-1,"",1);
......@@ -478,11 +478,11 @@ std::unique_ptr<Backend> UnQLiteBackend::create(const Json::Value& config) {
std::unique_ptr<Backend> UnQLiteBackend::attach(const Json::Value& config) {
if(not config.isMember("path"))
throw std::runtime_error("UnQLiteBackend needs to be initialized with a path");
throw Exception("UnQLiteBackend needs to be initialized with a path");
std::string db_path = config["path"].asString();
std::ifstream f(db_path.c_str());
if(!f.good()) {
throw std::runtime_error("Database file "s + db_path + " does not exist");
throw Exception("Database file "s + db_path + " does not exist");
}
unqlite* pDB;
spdlog::trace("[unqlite] Opening UnQLite database");
......@@ -490,7 +490,7 @@ std::unique_ptr<Backend> UnQLiteBackend::attach(const Json::Value& config) {
int mode = UNQLITE_OPEN_READWRITE;
ret = unqlite_open(&pDB, db_path.c_str(), mode);
if(ret != UNQLITE_OK) {
throw std::runtime_error("Could not open database at "s + db_path);
throw Exception("Could not open database at "s + db_path);
}
auto backend = std::make_unique<UnQLiteBackend>();
backend->m_db = pDB;
......
......@@ -80,15 +80,15 @@ class UnQLiteVM {
std::string error = "UnQLite error: "s;
if(len > 0) {
error += errorBuffer;
throw std::runtime_error(error);
throw Exception(error);
}
unqlite_config(m_db, UNQLITE_CONFIG_ERR_LOG, &errorBuffer, &len);
if(len > 0) {
error += errorBuffer;
throw std::runtime_error(error);
throw Exception(error);
}
error += "(unknown)";
throw std::runtime_error(error);
throw Exception(error);
}
static int output_callback(const void *pOutput, unsigned int nOutputLen, void *pUserData) {
......
......@@ -9,27 +9,13 @@
#include <unordered_map>
#include <iostream>
#include "sonata/Exception.hpp"
#include "invoke/invoke.hpp"
namespace sonata {
class UnQLiteVM;
class UnQLiteValueException : public std::exception {
std::string m_message;
public:
UnQLiteValueException(const std::string& msg)
: std::exception()
, m_message(msg) {}
virtual const char* what() const noexcept {
return m_message.c_str();
}
};
class UnQLiteValue {
private:
......@@ -259,7 +245,7 @@ class UnQLiteValue {
template<typename T>
operator T() const {
if(!is<T>()) {
throw UnQLiteValueException("Invalid type conversion requested");
throw Exception("Invalid type conversion requested");
}
return as<T>();
}
......@@ -293,7 +279,7 @@ class UnQLiteValue {
void foreach(const std::function<void(unsigned, const UnQLiteValue&)>& f) {
if(not unqlite_value_is_json_array(m_value)) {
throw UnQLiteValueException("UnQLiteValue is not an array");
throw Exception("UnQLiteValue is not an array");
}
using arg_type =
std::tuple<size_t,
......@@ -309,7 +295,7 @@ class UnQLiteValue {
void foreach(const std::function<void(const std::string&, const UnQLiteValue&)>& f) {
if(not unqlite_value_is_json_object(m_value)) {
throw UnQLiteValueException("UnQLiteValue is not an array");
throw Exception("UnQLiteValue is not an array");
}
using arg_type =
std::tuple<size_t,
......
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