Merge pull request #52347 from vitlibar/zooKeeper-retries-control-rethrows-with-original-callstack

ZooKeeperRetriesControl rethrows with original callstack
This commit is contained in:
Nikita Mikhaylov 2023-07-20 12:14:04 +02:00 committed by GitHub
commit ec20786940
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -72,7 +72,7 @@ public:
if (!Coordination::isHardwareError(e.code))
throw;
setKeeperError(e.code, e.message());
setKeeperError(std::current_exception(), e.code, e.message());
}
catch (...)
{
@ -91,16 +91,16 @@ public:
}
catch (const zkutil::KeeperException & e)
{
setKeeperError(e.code, e.message());
setKeeperError(std::current_exception(), e.code, e.message());
}
catch (const Exception & e)
{
setUserError(e.code(), e.what());
setUserError(std::current_exception(), e.code(), e.what());
}
return false;
}
void setUserError(int code, std::string message)
void setUserError(std::exception_ptr exception, int code, std::string message)
{
if (retries_info.logger)
LOG_TRACE(
@ -113,16 +113,28 @@ public:
iteration_succeeded = false;
user_error.code = code;
user_error.message = std::move(message);
user_error.exception = exception;
keeper_error = KeeperError{};
}
template <typename... Args>
void setUserError(std::exception_ptr exception, int code, fmt::format_string<Args...> fmt, Args &&... args)
{
setUserError(exception, code, fmt::format(fmt, std::forward<Args>(args)...));
}
void setUserError(int code, std::string message)
{
setUserError(std::make_exception_ptr(Exception::createDeprecated(message, code)), code, message);
}
template <typename... Args>
void setUserError(int code, fmt::format_string<Args...> fmt, Args &&... args)
{
setUserError(code, fmt::format(fmt, std::forward<Args>(args)...));
}
void setKeeperError(Coordination::Error code, std::string message)
void setKeeperError(std::exception_ptr exception, Coordination::Error code, std::string message)
{
if (retries_info.logger)
LOG_TRACE(
@ -135,9 +147,21 @@ public:
iteration_succeeded = false;
keeper_error.code = code;
keeper_error.message = std::move(message);
keeper_error.exception = exception;
user_error = UserError{};
}
template <typename... Args>
void setKeeperError(std::exception_ptr exception, Coordination::Error code, fmt::format_string<Args...> fmt, Args &&... args)
{
setKeeperError(exception, code, fmt::format(fmt, std::forward<Args>(args)...));
}
void setKeeperError(Coordination::Error code, std::string message)
{
setKeeperError(std::make_exception_ptr(zkutil::KeeperException(message, code)), code, message);
}
template <typename... Args>
void setKeeperError(Coordination::Error code, fmt::format_string<Args...> fmt, Args &&... args)
{
@ -163,12 +187,14 @@ private:
using Code = Coordination::Error;
Code code = Code::ZOK;
std::string message;
std::exception_ptr exception;
};
struct UserError
{
int code = ErrorCodes::OK;
std::string message;
std::exception_ptr exception;
};
bool canTry()
@ -232,11 +258,11 @@ private:
void throwIfError() const
{
if (user_error.code != ErrorCodes::OK)
throw Exception::createDeprecated(user_error.message, user_error.code);
if (user_error.exception)
std::rethrow_exception(user_error.exception);
if (keeper_error.code != KeeperError::Code::ZOK)
throw zkutil::KeeperException(keeper_error.message, keeper_error.code);
if (keeper_error.exception)
std::rethrow_exception(keeper_error.exception);
}
void logLastError(std::string_view header)