From b4daa23f5e2fd242c52f82f1e644b97f21f4ff3e Mon Sep 17 00:00:00 2001 From: zhengjian Date: Wed, 24 Jul 2024 22:55:38 +0800 Subject: [PATCH] Resolve the duplicate registration key issue --- include/rest_rpc/router.h | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/include/rest_rpc/router.h b/include/rest_rpc/router.h index d9c4d06..489646e 100644 --- a/include/rest_rpc/router.h +++ b/include/rest_rpc/router.h @@ -50,16 +50,34 @@ public: template void register_handler(std::string const &name, Function f, bool pub = false) { uint32_t key = MD5::MD5Hash32(name.data()); - key2func_name_.emplace(key, name); - return register_nonmember_func(key, std::move(f)); + try { + if (key2func_name_.find(key) != key2func_name_.end()) { + throw std::invalid_argument("duplicate registration key !"); + } else { + key2func_name_.emplace(key, name); + return register_nonmember_func(key, std::move(f)); + } + } catch (const std::exception &e) { + std::cerr << e.what() << '\n'; + throw; + } } template void register_handler(std::string const &name, const Function &f, Self *self) { uint32_t key = MD5::MD5Hash32(name.data()); - key2func_name_.emplace(key, name); - return register_member_func(key, f, self); + try { + if (key2func_name_.find(key) != key2func_name_.end()) { + throw std::invalid_argument("duplicate registration key !"); + } else { + key2func_name_.emplace(key, name); + return register_member_func(key, f, self); + } + } catch (const std::exception &e) { + std::cerr << e.what() << '\n'; + throw; + } } void remove_handler(std::string const &name) {