From 0674e79066fbf9eaa0b9f65a3baa7a1e59b5e78d Mon Sep 17 00:00:00 2001 From: Sandor Magyar Date: Wed, 19 Oct 2022 09:53:33 -0400 Subject: [PATCH] Improve arg passing and exceptions in mongo_sink --- include/spdlog/sinks/mongo_sink.h | 18 ++++++++++++------ 1 file 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 #include -#include +#include #include #include @@ -31,10 +31,16 @@ class mongo_sink : public base_sink { 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(), db_name, collection_name, uri) {} + try + : mongo_sink(std::make_shared(), 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 &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 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::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())); } }