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