Working error system I believe
This commit is contained in:
parent
9024cce1b4
commit
c2cdfe7052
@ -3,7 +3,7 @@
|
||||
#define _NB_ERROR
|
||||
|
||||
#include <exception>
|
||||
#include <string.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
@ -16,24 +16,20 @@ template<class ErrorType>
|
||||
class ErrorBase : public std::exception {
|
||||
public:
|
||||
static std::string lookup(unsigned int);
|
||||
|
||||
virtual const char* what() const noexcept override final {
|
||||
return static_cast<const ErrorType*>(this)->_msg.c_str();
|
||||
};
|
||||
unsigned int code() const noexcept {
|
||||
return static_cast<const ErrorType*>(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<const std::unordered_map<unsigned int, const char*>, decltype(ErrorType::ErrorMessages)>::value,
|
||||
) noexcept : _code{code}, _msg{""} {
|
||||
static_assert(std::is_same<const std::unordered_map<unsigned int, const char*>, decltype(ErrorType::ErrorMessages)>::value,
|
||||
"const std::unordered_map<unsigned int, const char*> 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<ErrorType>::lookup(code),
|
||||
line,
|
||||
filename
|
||||
) {}
|
||||
|
||||
const unsigned int _code;
|
||||
ErrorBase(
|
||||
const unsigned int code,
|
||||
const std::exception& trace,
|
||||
unsigned int line=0,
|
||||
std::string filename=""
|
||||
) noexcept : ErrorBase(
|
||||
code,
|
||||
ErrorBase<ErrorType>::lookup(code),
|
||||
trace,
|
||||
line,
|
||||
filename
|
||||
) {}
|
||||
|
||||
unsigned int _code;
|
||||
std::string _msg;
|
||||
};
|
||||
|
||||
|
||||
class Error : public ErrorBase<Error> {
|
||||
public:
|
||||
Error(unsigned int code, std::exception* const trace=nullptr)
|
||||
enum ErrorCodes : unsigned int {
|
||||
GENERAL, UNDEFINED, BADERRORCODE
|
||||
};
|
||||
|
||||
Error(unsigned int code) : ErrorBase<Error>(code, ErrorBase<Error>::lookup(code)) {}
|
||||
Error(unsigned int code, const std::exception& trace)
|
||||
: ErrorBase<Error>(code, ErrorBase<Error>::lookup(code), trace) {}
|
||||
|
||||
enum ErrorCodes : unsigned int {
|
||||
UNDEFINED, BADERRORCODE
|
||||
};
|
||||
friend ErrorBase<Error>;
|
||||
|
||||
protected:
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
|
||||
};
|
||||
|
||||
/*
|
||||
template<class ErrorType>
|
||||
ErrorBase<ErrorType>::ErrorBase(
|
||||
unsigned int code,
|
||||
std::string msg,
|
||||
const std::exception& trace,
|
||||
unsigned int line,
|
||||
std::string filename
|
||||
) noexcept
|
||||
: ErrorBase<ErrorType>(code, msg, std::make_shared<Error>(trace), line, filename) {}
|
||||
*/
|
||||
|
||||
template<class ErrorType>
|
||||
std::string ErrorBase<ErrorType>::lookup(unsigned int code) {
|
||||
for (auto kv : ErrorType::ErrorMessages) {
|
||||
|
||||
@ -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."}
|
||||
};
|
||||
|
||||
@ -5,7 +5,9 @@ using namespace nb;
|
||||
|
||||
class TestError : public ErrorBase<TestError> {
|
||||
public:
|
||||
TestError(unsigned int code, std::exception* const trace=nullptr)
|
||||
TestError(unsigned int code)
|
||||
: ErrorBase<TestError>(code, ErrorBase<TestError>::lookup(code)) {}
|
||||
TestError(unsigned int code, const std::exception& trace)
|
||||
: ErrorBase<TestError>(code, ErrorBase<TestError>::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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user