From c2cdfe7052facf536fc7359d06d67782e4e9817b Mon Sep 17 00:00:00 2001 From: NaifBanana <30419422+NaifBanana@users.noreply.github.com> Date: Sun, 9 Nov 2025 23:36:12 -0600 Subject: [PATCH] Working error system I believe --- include/Errors.hpp | 85 +++++++++++++++++++++++++++++--------- src/NBCoreUtils/Errors.cpp | 5 +-- tests/ErrorTest/main.cpp | 14 ++++--- 3 files changed, 76 insertions(+), 28 deletions(-) diff --git a/include/Errors.hpp b/include/Errors.hpp index 2aa9abb..dc57f80 100644 --- a/include/Errors.hpp +++ b/include/Errors.hpp @@ -3,7 +3,7 @@ #define _NB_ERROR #include -#include +#include #include #include #include @@ -16,24 +16,20 @@ template class ErrorBase : public std::exception { public: static std::string lookup(unsigned int); - - virtual const char* what() const noexcept override final { - return static_cast(this)->_msg.c_str(); - }; unsigned int code() const noexcept { return static_cast(this)->_code; }; + virtual const char* what() const noexcept override final { return _msg.c_str(); }; protected: ErrorBase() = default; ErrorBase( const unsigned int code, std::string msg, - std::exception* const trace, unsigned int line=0, std::string filename="" - ) noexcept : _code{code} { - static_assert(std::is_same, decltype(ErrorType::ErrorMessages)>::value, + ) noexcept : _code{code}, _msg{""} { + static_assert(std::is_same, decltype(ErrorType::ErrorMessages)>::value, "const std::unordered_map ErrorMessages must be " "a class member." ); @@ -45,35 +41,86 @@ protected: "const std::string type must be a class member." ); - _msg = std::string(ErrorType::type); - _msg += ":" + std::to_string(code); + _msg += std::string(ErrorType::type); + _msg += "-" + std::to_string(_code); if (line && filename.size()) { _msg += " in \'" + filename + "\':" + std::to_string(line); } - _msg += "\n\t" + msg; - if (trace) { - _msg += "\nTraceback - " + std::string(trace->what()); + _msg += "\n " + msg; + } + ErrorBase( + const unsigned int code, + std::string msg, + const std::exception& trace, + unsigned int line=0, + std::string filename="" + ) noexcept : ErrorBase(code, msg, line, filename) { + std::string what_msg(trace.what()); + std::string::size_type newline_pos = what_msg.find("\n"); + while(newline_pos != std::string::npos) { + what_msg.replace(newline_pos, 1, "\n "); + newline_pos = what_msg.find("\n", newline_pos+1); } + _msg += "\n Trace: " + what_msg; } + ErrorBase( + const unsigned int code, + unsigned int line=0, + std::string filename="" + ) noexcept : ErrorBase( + code, + ErrorBase::lookup(code), + line, + filename + ) {} + + ErrorBase( + const unsigned int code, + const std::exception& trace, + unsigned int line=0, + std::string filename="" + ) noexcept : ErrorBase( + code, + ErrorBase::lookup(code), + trace, + line, + filename + ) {} - const unsigned int _code; + unsigned int _code; std::string _msg; }; - class Error : public ErrorBase { public: - Error(unsigned int code, std::exception* const trace=nullptr) - : ErrorBase(code, ErrorBase::lookup(code), trace) {} - enum ErrorCodes : unsigned int { - UNDEFINED, BADERRORCODE + GENERAL, UNDEFINED, BADERRORCODE }; + + Error(unsigned int code) : ErrorBase(code, ErrorBase::lookup(code)) {} + Error(unsigned int code, const std::exception& trace) + : ErrorBase(code, ErrorBase::lookup(code), trace) {} + + friend ErrorBase; + +protected: static const std::string type; static const ErrorCodeMap ErrorMessages; }; +/* +template +ErrorBase::ErrorBase( + unsigned int code, + std::string msg, + const std::exception& trace, + unsigned int line, + std::string filename +) noexcept + : ErrorBase(code, msg, std::make_shared(trace), line, filename) {} + */ + template std::string ErrorBase::lookup(unsigned int code) { for (auto kv : ErrorType::ErrorMessages) { diff --git a/src/NBCoreUtils/Errors.cpp b/src/NBCoreUtils/Errors.cpp index 88c5124..544346a 100644 --- a/src/NBCoreUtils/Errors.cpp +++ b/src/NBCoreUtils/Errors.cpp @@ -2,10 +2,9 @@ namespace nb { -// class Error - const std::string Error::type = "Error"; -const ErrorCodeMap Error::ErrorMessages = ErrorCodeMap{ +const ErrorCodeMap Error::ErrorMessages = { + {ErrorCodes::GENERAL, "General std::exception."}, {ErrorCodes::UNDEFINED, "Undefined / general error."}, {ErrorCodes::BADERRORCODE, "Unrecognized error code."} }; diff --git a/tests/ErrorTest/main.cpp b/tests/ErrorTest/main.cpp index c382f0e..9e5ca1e 100644 --- a/tests/ErrorTest/main.cpp +++ b/tests/ErrorTest/main.cpp @@ -5,7 +5,9 @@ using namespace nb; class TestError : public ErrorBase { public: - TestError(unsigned int code, std::exception* const trace=nullptr) + TestError(unsigned int code) + : ErrorBase(code, ErrorBase::lookup(code)) {} + TestError(unsigned int code, const std::exception& trace) : ErrorBase(code, ErrorBase::lookup(code), trace) {} enum ErrorCodes : unsigned int { @@ -24,11 +26,11 @@ const ErrorCodeMap TestError::ErrorMessages{ }; int main() { - TestError d(TestError::ErrorCodes::D); - TestError c(TestError::ErrorCodes::C, &d); - TestError b(TestError::ErrorCodes::B, &c); - TestError a(TestError::ErrorCodes::A, &b); - throw Error(Error::ErrorCodes::UNDEFINED, &a); + TestError d(TestError::ErrorCodes::D, std::out_of_range("hey")); + TestError c(TestError::ErrorCodes::C, d); + TestError b(TestError::ErrorCodes::B, c); + TestError a(TestError::ErrorCodes::A, b); + throw Error(Error::ErrorCodes::UNDEFINED, a); return 0; } \ No newline at end of file