Skip to content
Snippets Groups Projects
Commit 6927996d authored by Eric Cano's avatar Eric Cano
Browse files

Created new exception launcher for errno + exception throwing.

This will allow diagnosing instances where rados (supposedly) throws exceptions
(supposedly std::error_code).
parent 587f8ac6
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,8 @@
#pragma once
#include "Exception.hpp"
#include<functional>
#include <system_error>
namespace cta {
namespace exception {
......@@ -30,6 +32,18 @@ namespace exception {
int errorNumber() const { return m_errnum; }
std::string strError() const { return m_strerror; }
static void throwOnReturnedErrno(const int err, const std::string &context = "");
template <typename F>
static void throwOnReturnedErrnoOrThrownStdException (F f, const std::string &context = "") {
try {
throwOnReturnedErrno(f(), context);
} catch (Errnum &) {
throw; // Let the exception of throwOnReturnedErrno pass through.
} catch (std::error_code & ec) {
throw Errnum(ec.value(), context + " Got an std::error_code: " + ec.message());
} catch (std::exception & ex) {
throw Exception(context + " Got a standard exception: " + ex.what());
}
}
static void throwOnNonZero(const int status, const std::string &context = "");
static void throwOnZero(const int status, const std::string &context = "");
static void throwOnNull(const void *const f, const std::string &context = "");
......
......@@ -87,6 +87,15 @@ namespace unitTests {
ASSERT_THROW(cta::exception::Errnum::throwOnReturnedErrno(ENOSPC, "Context"),
cta::exception::Errnum);
ASSERT_NO_THROW(cta::exception::Errnum::throwOnReturnedErrnoOrThrownStdException([](){ return 0; }, "Context"));
ASSERT_THROW(cta::exception::Errnum::throwOnReturnedErrnoOrThrownStdException([](){ return ENOSPC; }, "Context"),
cta::exception::Errnum);
ASSERT_THROW(cta::exception::Errnum::throwOnReturnedErrnoOrThrownStdException([](){ throw std::error_code(ENOSPC, std::system_category()); return 0; }, "Context"),
cta::exception::Errnum);
ASSERT_THROW(cta::exception::Errnum::throwOnReturnedErrnoOrThrownStdException([](){ throw std::exception(); return 0; }, "Context"),
cta::exception::Exception);
/* throwOnNonZero */
errno = ENOENT;
ASSERT_NO_THROW(cta::exception::Errnum::throwOnNonZero(0, "Context"));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment