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
+5 -50
View File
@@ -3,56 +3,11 @@
namespace nb {
// class Error
const ErrorCodeMap Error::ErrorCodes = {
{0, "Undefined error."},
{666, "Unrecognized error code."}
const std::string Error::type = "Error";
const ErrorCodeMap Error::ErrorMessages = ErrorCodeMap{
{ErrorCodes::UNDEFINED, "Undefined / general error."},
{ErrorCodes::BADERRORCODE, "Unrecognized error code."}
};
Error::Error(
const unsigned int code,
std::string msg,
std::exception* const trace,
unsigned int line,
std::string filename
) noexcept : _code{code} {
_msg = std::string(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());
}
}
Error::Error(const unsigned int code, std::exception* const trace) noexcept
: Error(code, ErrorCodeLookup<Error>(code), trace) {}
Error::Error(std::string msg, std::exception* const trace) noexcept
: Error(0, msg, trace) {}
Error::Error(const Error& cp) noexcept
: _type{cp._type}, _code{cp._code}, _msg{cp._msg} {}
unsigned int Error::code() const noexcept { return _code; }
const char* Error::type() const noexcept { return this->_type; }
const char* Error::what() const noexcept { return _msg.c_str(); }
/* constexpr const char* Error::lookup(unsigned int code) noexcept {
char* ret = new char[512];
switch(code) {
case 0:
strcpy(ret, "General ");
strcat(ret, this->type());
break;
default:
strcpy(ret, this->lookup(code));
break;
}
return ret;
} */
}