diff --git a/include/Errors.hpp b/include/Errors.hpp index dc57f80..151bca0 100644 --- a/include/Errors.hpp +++ b/include/Errors.hpp @@ -8,6 +8,14 @@ #include #include +#ifndef THROW +#ifdef CODE_ERROR_LOCATIONS +#define THROW(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__) +#else +#define THROW(type, ...) throw type(__VA_ARGS__) +#endif +#endif + namespace nb { typedef std::unordered_map ErrorCodeMap; @@ -98,8 +106,8 @@ public: }; Error(unsigned int code) : ErrorBase(code, ErrorBase::lookup(code)) {} - Error(unsigned int code, const std::exception& trace) - : ErrorBase(code, ErrorBase::lookup(code), trace) {} + Error(unsigned int code, const std::exception& trace, unsigned int line=0, const std::string& filename="") + : ErrorBase(code, ErrorBase::lookup(code), trace, line, filename) {} friend ErrorBase; diff --git a/tests/ErrorTest/main.cpp b/tests/ErrorTest/main.cpp index 9e5ca1e..25e426d 100644 --- a/tests/ErrorTest/main.cpp +++ b/tests/ErrorTest/main.cpp @@ -1,3 +1,5 @@ +#define CODE_ERROR_LOCATIONS + #include "Errors.hpp" #include @@ -5,10 +7,10 @@ using namespace nb; class TestError : public ErrorBase { public: - TestError(unsigned int code) + TestError(unsigned int code, unsigned int line=0, const std::string& filename="") : ErrorBase(code, ErrorBase::lookup(code)) {} - TestError(unsigned int code, const std::exception& trace) - : ErrorBase(code, ErrorBase::lookup(code), trace) {} + TestError(unsigned int code, const std::exception& trace, unsigned int line=0, const std::string& filename="") + : ErrorBase(code, ErrorBase::lookup(code), trace, line, filename) {} enum ErrorCodes : unsigned int { A, B, C, D @@ -26,11 +28,28 @@ const ErrorCodeMap TestError::ErrorMessages{ }; int main() { - 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); + //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); + try { + try { + try { + try { + THROW(TestError, TestError::ErrorCodes::D); + } catch (const std::exception& e){ + THROW(TestError, TestError::ErrorCodes::C, e); + } + } catch(const std::exception& e) { + THROW(TestError, TestError::ErrorCodes::B, e); + } + } catch(const std::exception& e) { + THROW(TestError, TestError::ErrorCodes::A, e); + } + } catch(const std::exception& e) { + THROW(Error, Error::ErrorCodes::UNDEFINED, e); + } return 0; } \ No newline at end of file