From 8fc7533a76ea2a87994fab76f7d4a79e3fccd912 Mon Sep 17 00:00:00 2001 From: Nils Carlson Date: Wed, 7 Apr 2021 20:27:07 +0000 Subject: [PATCH] Change KeepAlive lease expiry to throw std::out_of_range Update documentation to show different exceptions used by KeepAlive. --- README.md | 6 ++++-- src/KeepAlive.cpp | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3ea730e..d5d8baf 100644 --- a/README.md +++ b/README.md @@ -540,8 +540,10 @@ is constructed. if (eptr) { std::rethrow_exception(eptr); } - } catch(const std::exception& e) { - std::cerr << "Caught exception \"" << e.what() << "\"\n"; + } catch(const std::runtime_error& e) { + std::cerr << "Connection failure \"" << e.what() << "\"\n"; + } catch(const std::out_of_range& e) { + std::cerr << "Lease expiry \"" << e.what() << "\"\n"; } }; etcd::KeepAlive keepalive(etcd, handler, ttl, lease_id); diff --git a/src/KeepAlive.cpp b/src/KeepAlive.cpp index 6ef8830..2dd7560 100644 --- a/src/KeepAlive.cpp +++ b/src/KeepAlive.cpp @@ -158,8 +158,8 @@ void etcd::KeepAlive::refresh() throw std::runtime_error("Failed to refresh lease: error code: " + std::to_string(resp.error_code()) + ", message: " + resp.error_message()); } - if (resp.value().ttl() == -1) { - throw std::runtime_error("Failed to refresh lease due to expiration: the new TTL is -1."); + if (resp.value().ttl() == 0) { + throw std::out_of_range("Failed to refresh lease due to expiration: the new TTL is 0."); } // trigger the next round; this->refresh();