171 lines
4.2 KiB
C++
171 lines
4.2 KiB
C++
#pragma once
|
|
#ifndef _NB_ERRORS_IMPL
|
|
#define _NB_ERRORS_IMPL
|
|
|
|
#include <exception>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <type_traits>
|
|
#include <unordered_map>
|
|
|
|
#include "StringUtils.hpp"
|
|
|
|
namespace nb {
|
|
|
|
typedef std::unordered_map<unsigned int, std::string> ErrorCodeMap;
|
|
|
|
class ErrorBase {
|
|
protected:
|
|
|
|
public:
|
|
const unsigned int code;
|
|
const std::string msg;
|
|
const std::string type;
|
|
const std::shared_ptr<ErrorBase> trace;
|
|
|
|
ErrorBase(const ErrorBase&) = default;
|
|
ErrorBase(const std::exception&) noexcept;
|
|
virtual std::string what() const noexcept {
|
|
std::string ret = msg;
|
|
if (trace) {
|
|
std::string trace_msg = msg;
|
|
ret += nb::NEWLINE + indent_strblock(
|
|
trace_msg,
|
|
nb::TABOVER,
|
|
nb::TABOVER+"Trace: "
|
|
);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
protected:
|
|
ErrorBase(
|
|
unsigned int code_,
|
|
std::string msg_,
|
|
std::string type_,
|
|
std::shared_ptr<ErrorBase> trace_
|
|
) noexcept :
|
|
code(code_),
|
|
msg(msg_),
|
|
type(type_),
|
|
trace{trace_}
|
|
{}
|
|
};
|
|
|
|
template <class ErrorType>
|
|
class Error : public ErrorBase {
|
|
private:
|
|
void inline check_asserts() {
|
|
static_assert(std::is_same<const ErrorCodeMap, decltype(ErrorType::ErrorMessages)>::value,
|
|
"const std::unordered_map<unsigned int, const char*> ErrorMessages must be "
|
|
"a class member."
|
|
);
|
|
static_assert(std::is_enum_v<typename ErrorType::Codes>, "enum Codes must be a class member.");
|
|
static_assert(std::is_same<std::underlying_type_t<typename ErrorType::Codes>, unsigned int>::value,
|
|
"enum Codes must be of underlying type unsigned int."
|
|
);
|
|
static_assert(std::is_same<const std::string, decltype(ErrorType::type)>::value,
|
|
"const std::string type must be a class member."
|
|
);
|
|
}
|
|
|
|
protected:
|
|
using ErrorBase::ErrorBase;
|
|
|
|
public:
|
|
Error(
|
|
unsigned int code_,
|
|
const ErrorBase& trace_
|
|
) noexcept : ErrorBase(
|
|
code_,
|
|
ErrorType::ErrorMessages.at(code_),
|
|
ErrorType::type,
|
|
std::make_shared<ErrorBase>(trace_)
|
|
) { check_asserts(); }
|
|
|
|
Error(
|
|
std::string msg_,
|
|
const ErrorBase& trace_
|
|
) noexcept : ErrorBase(
|
|
0,
|
|
msg_,
|
|
ErrorType::type,
|
|
std::make_shared<ErrorBase>(trace_)
|
|
) { check_asserts(); }
|
|
|
|
Error(unsigned int code_) noexcept : ErrorBase(
|
|
code_,
|
|
ErrorType::ErrorMessages.at(code_),
|
|
ErrorType::type,
|
|
nullptr
|
|
) { check_asserts(); }
|
|
|
|
Error(std::string msg_) noexcept : ErrorBase(
|
|
0,
|
|
msg_,
|
|
ErrorType::type,
|
|
nullptr
|
|
) { check_asserts(); }
|
|
|
|
Error(
|
|
unsigned int code_,
|
|
const std::string& note_,
|
|
const ErrorBase& trace_
|
|
) noexcept : ErrorBase(
|
|
code_,
|
|
ErrorType::ErrorMessages.at(code_) + " (" + note_ + ")",
|
|
ErrorType::type,
|
|
std::make_shared<ErrorBase>(trace_)
|
|
) { check_asserts(); }
|
|
|
|
Error(unsigned int code_, const std::string& note_) noexcept : ErrorBase(
|
|
code_,
|
|
ErrorType::ErrorMessages.at(code_) + " (" + note_ + ")",
|
|
ErrorType::type,
|
|
nullptr
|
|
) { check_asserts(); }
|
|
|
|
using ErrorBase::what;
|
|
using ErrorBase::code;
|
|
using ErrorBase::msg;
|
|
using ErrorBase::trace;
|
|
using ErrorBase::type;
|
|
};
|
|
|
|
class NBError : public Error<NBError> {
|
|
protected:
|
|
using Base = Error<NBError>;
|
|
|
|
public:
|
|
enum Codes : unsigned int {
|
|
STANDARD, UNDEFINED, INDEX_ERROR
|
|
};
|
|
|
|
NBError(unsigned int code_=1) noexcept : Base(code_) {}
|
|
NBError(const std::exception& err) : Base(
|
|
STANDARD,
|
|
std::string(err.what()),
|
|
"std::exception",
|
|
nullptr
|
|
) {}
|
|
|
|
// fix
|
|
NBError(std::string msg_) noexcept : Base(
|
|
Codes::UNDEFINED,
|
|
msg_
|
|
) {}
|
|
|
|
inline static const std::string type = "nb::Error";
|
|
inline static const ErrorCodeMap ErrorMessages = {
|
|
{STANDARD, "std::exception"},
|
|
{UNDEFINED, "Error"},
|
|
{INDEX_ERROR, "Indexing error"}
|
|
};
|
|
};
|
|
|
|
// template<typename... Args>
|
|
// Error(Args...) -> Error<NoneType>;
|
|
|
|
} // namespace nb
|
|
|
|
#endif // _NB_ERRORS_IMPL
|