Throw all exceptions with Arbutils exception.

This commit is contained in:
Deukhoofd 2020-08-15 14:50:17 +02:00
parent 3e96d2fd76
commit f158dbf8a2
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
6 changed files with 21 additions and 13 deletions

View File

@ -1,6 +1,7 @@
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <string.h> #include <string.h>
#include "Exception.hpp"
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
@ -10,7 +11,7 @@
std::stringstream ss; \ std::stringstream ss; \
ss << "ASSERTION FAILED: [" << __FILENAME__ << " (" << __LINE__ << ")] \""; \ ss << "ASSERTION FAILED: [" << __FILENAME__ << " (" << __LINE__ << ")] \""; \
ss << #expr << "\""; \ ss << #expr << "\""; \
throw std::logic_error(ss.str()); \ throw ArbUt::Exception(ss.str()); \
} }
#define AssertForEach(iterator, assertion) \ #define AssertForEach(iterator, assertion) \
{ \ { \

View File

@ -23,7 +23,7 @@ namespace ArbUt {
[[maybe_unused]] const auto& v = _map.insert({key, value}); [[maybe_unused]] const auto& v = _map.insert({key, value});
#ifndef NO_ASSERT #ifndef NO_ASSERT
if (!v.second) if (!v.second)
throw std::logic_error("Key already exists"); throw ArbUt::Exception("Key already exists");
#endif #endif
} }

View File

@ -4,6 +4,7 @@
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <vector> #include <vector>
#include "../Exception.hpp"
namespace ArbUt { namespace ArbUt {
template <class ValueT> class List { template <class ValueT> class List {
@ -27,7 +28,7 @@ namespace ArbUt {
if (index >= _vector.size() || index < 0) { if (index >= _vector.size() || index < 0) {
std::stringstream ss; std::stringstream ss;
ss << "Index " << index << " is out of bounds."; ss << "Index " << index << " is out of bounds.";
throw std::logic_error(ss.str()); throw ArbUt::Exception(ss.str());
} }
#endif #endif
return _vector.at(index); return _vector.at(index);
@ -38,7 +39,7 @@ namespace ArbUt {
if (index >= _vector.size() || index < 0) { if (index >= _vector.size() || index < 0) {
std::stringstream ss; std::stringstream ss;
ss << "Index " << index << " is out of bounds."; ss << "Index " << index << " is out of bounds.";
throw std::logic_error(ss.str()); throw ArbUt::Exception(ss.str());
} }
#endif #endif
return _vector.at(index); return _vector.at(index);
@ -49,7 +50,7 @@ namespace ArbUt {
if (index >= _vector.size() || index < 0) { if (index >= _vector.size() || index < 0) {
std::stringstream ss; std::stringstream ss;
ss << "Index " << index << " is out of bounds."; ss << "Index " << index << " is out of bounds.";
throw std::logic_error(ss.str()); throw ArbUt::Exception(ss.str());
} }
#endif #endif
const auto& prev = _vector.at(index); const auto& prev = _vector.at(index);

View File

@ -11,20 +11,20 @@
#endif #endif
namespace ArbUt { namespace ArbUt {
class Exception : std::exception { class Exception : std::logic_error {
std::string _msg;
#if !WINDOWS #if !WINDOWS
backward::StackTrace _stack; backward::StackTrace _stack;
#endif #endif
public: public:
explicit Exception(std::string msg) : _msg(std::move(msg)) { explicit Exception(const std::string& msg) : std::logic_error(msg) {
#if !WINDOWS #if !WINDOWS
_stack.load_here(9); _stack.load_here(9);
#endif #endif
} }
[[nodiscard]] const char* what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override { return _msg.c_str(); } public:
const char* what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override { return logic_error::what(); }
[[nodiscard]] std::string GetStacktrace([[maybe_unused]] size_t depth = 6, [[nodiscard]] std::string GetStacktrace([[maybe_unused]] size_t depth = 6,
[[maybe_unused]] bool include_addr = true) const { [[maybe_unused]] bool include_addr = true) const {

View File

@ -36,7 +36,7 @@ namespace ArbUt {
if (index >= _vector.size()) { if (index >= _vector.size()) {
std::stringstream ss; std::stringstream ss;
ss << "Index " << index << " is out of bounds."; ss << "Index " << index << " is out of bounds.";
throw std::logic_error(ss.str()); throw ArbUt::Exception(ss.str());
} }
#endif #endif
return _vector[index]; return _vector[index];
@ -61,7 +61,7 @@ namespace ArbUt {
if (index >= _vector.size()) { if (index >= _vector.size()) {
std::stringstream ss; std::stringstream ss;
ss << "Index " << index << " is out of bounds."; ss << "Index " << index << " is out of bounds.";
throw std::logic_error(ss.str()); throw ArbUt::Exception(ss.str());
} }
#endif #endif
delete _vector[index]; delete _vector[index];

View File

@ -8,8 +8,14 @@ TEST_CASE("Assert succeeds if true", "[Utilities]") { REQUIRE_NOTHROW(TestWrappe
TEST_CASE("Assert throws if false", "[Utilities]") { REQUIRE_THROWS(TestWrapper(false)); } TEST_CASE("Assert throws if false", "[Utilities]") { REQUIRE_THROWS(TestWrapper(false)); }
TEST_CASE("Assert throws if false with message", "[Utilities]") { TEST_CASE("Assert throws if false with message", "[Utilities]") {
REQUIRE_THROWS_WITH(TestWrapper(false), try {
Catch::Matchers::Equals("ASSERTION FAILED: [AssertTests.cpp (3)] \"wrapperExpression\"")); TestWrapper(false);
}
catch (const ArbUt::Exception& e) {
REQUIRE(strcmp(e.what(), "ASSERTION FAILED: [AssertTests.cpp (3)] \"wrapperExpression\"") == 0);
return;
}
throw ArbUt::Exception("Didn't throw.");
} }
TEST_CASE("Multiple asserts", "[Utilities]") { TEST_CASE("Multiple asserts", "[Utilities]") {