Changed error system to CRTP, rough draft

This commit is contained in:
NaifBanana
2025-11-06 02:03:49 -06:00
parent a7428ef55f
commit 9024cce1b4
3 changed files with 85 additions and 91 deletions
+20 -17
View File
@@ -3,29 +3,32 @@
using namespace nb;
class TestError : public nb::Error {
class TestError : public ErrorBase<TestError> {
public:
TestError(unsigned int code, nb::Error* const trace=nullptr) : _type("nb::TestError"), Error(code, ErrorCodeLookup<TestError>(code), trace) {}
friend const char* ErrorCodeLookup<TestError>(unsigned int);
protected:
static const nb::ErrorCodeMap ErrorCodes;
const char* _type;
TestError(unsigned int code, std::exception* const trace=nullptr)
: ErrorBase<TestError>(code, ErrorBase<TestError>::lookup(code), trace) {}
enum ErrorCodes : unsigned int {
A, B, C, D
};
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
const ErrorCodeMap TestError::ErrorCodes{
{0, "Hey!"},
{1, "How"},
{2, "You"},
{3, "Doin"}
const std::string TestError::type="TestError";
const ErrorCodeMap TestError::ErrorMessages{
{TestError::ErrorCodes::A, "Hey!"},
{TestError::ErrorCodes::B, "How"},
{TestError::ErrorCodes::C, "You"},
{TestError::ErrorCodes::D, "Doin"}
};
int main() {
TestError a(0);
TestError b(1, &a);
TestError c(2, &b);
throw TestError(3, &c);
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);
return 0;
}