From e596f53a6a3768156a4fb37bbc31fe797fb62229 Mon Sep 17 00:00:00 2001 From: Pavel Kartavyy Date: Thu, 1 Oct 2015 15:24:44 +0300 Subject: [PATCH] ZooKeeper: throw correct exception code in get method [#METR-16142] --- libs/libzkutil/include/zkutil/ZooKeeper.h | 2 +- libs/libzkutil/src/ZooKeeper.cpp | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/libs/libzkutil/include/zkutil/ZooKeeper.h b/libs/libzkutil/include/zkutil/ZooKeeper.h index e2727265ca..03c4df7a15 100644 --- a/libs/libzkutil/include/zkutil/ZooKeeper.h +++ b/libs/libzkutil/include/zkutil/ZooKeeper.h @@ -120,7 +120,7 @@ public: /** Не бросает исключение при следующих ошибках: * - Такой ноды нет. В таком случае возвращает false. */ - bool tryGet(const std::string & path, std::string & res, Stat * stat = nullptr, EventPtr watch = nullptr); + bool tryGet(const std::string & path, std::string & res, Stat * stat = nullptr, EventPtr watch = nullptr, int * code = nullptr); void set(const std::string & path, const std::string & data, int32_t version = -1, Stat * stat = nullptr); diff --git a/libs/libzkutil/src/ZooKeeper.cpp b/libs/libzkutil/src/ZooKeeper.cpp index 26ea0aae89..6ca04aae05 100644 --- a/libs/libzkutil/src/ZooKeeper.cpp +++ b/libs/libzkutil/src/ZooKeeper.cpp @@ -372,14 +372,15 @@ int32_t ZooKeeper::getImpl(const std::string & path, std::string & res, Stat * s std::string ZooKeeper::get(const std::string & path, Stat * stat, EventPtr watch) { + int code; std::string res; - if (tryGet(path, res, stat, watch)) + if (tryGet(path, res, stat, watch, &code)) return res; else - throw KeeperException("Can't get data for node " + path + ": node doesn't exist"); + throw KeeperException("Can't get data for node " + path + ": node doesn't exist", code); } -bool ZooKeeper::tryGet(const std::string & path, std::string & res, Stat * stat_, EventPtr watch) +bool ZooKeeper::tryGet(const std::string & path, std::string & res, Stat * stat_, EventPtr watch, int * return_code) { int32_t code = retry(std::bind(&ZooKeeper::getImpl, this, std::ref(path), std::ref(res), stat_, watch)); @@ -387,6 +388,9 @@ bool ZooKeeper::tryGet(const std::string & path, std::string & res, Stat * stat_ code == ZNONODE)) throw KeeperException(code, path); + if (return_code) + *return_code = code; + return code == ZOK; } -- GitLab