diff options
author | Sandor Magyar <SMagyar@aphysci.com> | 2022-10-19 16:53:33 +0300 |
---|---|---|
committer | Sandor Magyar <SMagyar@aphysci.com> | 2022-10-19 16:53:33 +0300 |
commit | 0674e79066fbf9eaa0b9f65a3baa7a1e59b5e78d (patch) | |
tree | 7ea61d11de6c36f82b9b40170fd607f9c39f5b2f | |
parent | 5f67ef4d6f2c1760562f2931bee13990da11d43e (diff) |
Improve arg passing and exceptions in mongo_sink
-rw-r--r-- | include/spdlog/sinks/mongo_sink.h | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/include/spdlog/sinks/mongo_sink.h b/include/spdlog/sinks/mongo_sink.h index 36201f5c..ca16d665 100644 --- a/include/spdlog/sinks/mongo_sink.h +++ b/include/spdlog/sinks/mongo_sink.h @@ -20,7 +20,7 @@ #include <bsoncxx/view_or_value.hpp> #include <mongocxx/client.hpp> -#include <mongocxx/exception/logic_error.hpp> +#include <mongocxx/exception/exception.hpp> #include <mongocxx/instance.hpp> #include <mongocxx/uri.hpp> @@ -31,10 +31,16 @@ class mongo_sink : public base_sink<Mutex> { public: mongo_sink(const std::string &db_name, const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017") - : mongo_sink(std::make_shared<mongocxx::instance>(), db_name, collection_name, uri) {} + try + : mongo_sink(std::make_shared<mongocxx::instance>(), db_name, collection_name, uri) + {} + catch (const mongocxx::exception &e) + { + throw_spdlog_ex(fmt_lib::format("Error opening database: {}", e.what())); + } - mongo_sink(const std::shared_ptr<mongocxx::instance> &instance, const std::string &db_name, const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017") - : instance_(instance) + mongo_sink(std::shared_ptr<mongocxx::instance> instance, const std::string &db_name, const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017") + : instance_(std::move(instance)) , db_name_(db_name) , coll_name_(collection_name) { @@ -42,9 +48,9 @@ public: { client_ = spdlog::details::make_unique<mongocxx::client>(mongocxx::uri{uri}); } - catch (const std::exception&) + catch (const std::exception &e) { - throw spdlog_ex("Error opening database"); + throw_spdlog_ex(fmt_lib::format("Error opening database: {}", e.what())); } } |