Checked for more usual realife error cascade situation

This commit is contained in:
NaifBanana 2025-11-09 23:55:19 -06:00
parent c2cdfe7052
commit 3ac76f1fac
2 changed files with 37 additions and 10 deletions

View File

@ -8,6 +8,14 @@
#include <type_traits> #include <type_traits>
#include <unordered_map> #include <unordered_map>
#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 { namespace nb {
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap; typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
@ -98,8 +106,8 @@ public:
}; };
Error(unsigned int code) : ErrorBase<Error>(code, ErrorBase<Error>::lookup(code)) {} Error(unsigned int code) : ErrorBase<Error>(code, ErrorBase<Error>::lookup(code)) {}
Error(unsigned int code, const std::exception& trace) Error(unsigned int code, const std::exception& trace, unsigned int line=0, const std::string& filename="")
: ErrorBase<Error>(code, ErrorBase<Error>::lookup(code), trace) {} : ErrorBase<Error>(code, ErrorBase<Error>::lookup(code), trace, line, filename) {}
friend ErrorBase<Error>; friend ErrorBase<Error>;

View File

@ -1,3 +1,5 @@
#define CODE_ERROR_LOCATIONS
#include "Errors.hpp" #include "Errors.hpp"
#include <iostream> #include <iostream>
@ -5,10 +7,10 @@ using namespace nb;
class TestError : public ErrorBase<TestError> { class TestError : public ErrorBase<TestError> {
public: public:
TestError(unsigned int code) TestError(unsigned int code, unsigned int line=0, const std::string& filename="")
: ErrorBase<TestError>(code, ErrorBase<TestError>::lookup(code)) {} : ErrorBase<TestError>(code, ErrorBase<TestError>::lookup(code)) {}
TestError(unsigned int code, const std::exception& trace) TestError(unsigned int code, const std::exception& trace, unsigned int line=0, const std::string& filename="")
: ErrorBase<TestError>(code, ErrorBase<TestError>::lookup(code), trace) {} : ErrorBase<TestError>(code, ErrorBase<TestError>::lookup(code), trace, line, filename) {}
enum ErrorCodes : unsigned int { enum ErrorCodes : unsigned int {
A, B, C, D A, B, C, D
@ -26,11 +28,28 @@ const ErrorCodeMap TestError::ErrorMessages{
}; };
int main() { int main() {
TestError d(TestError::ErrorCodes::D, std::out_of_range("hey")); //TestError d(TestError::ErrorCodes::D, std::out_of_range("hey"));
TestError c(TestError::ErrorCodes::C, d); //TestError c(TestError::ErrorCodes::C, d);
TestError b(TestError::ErrorCodes::B, c); //TestError b(TestError::ErrorCodes::B, c);
TestError a(TestError::ErrorCodes::A, b); //TestError a(TestError::ErrorCodes::A, b);
throw Error(Error::ErrorCodes::UNDEFINED, a); //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; return 0;
} }