NBEngine/engine/NBCore/Errors.hpp
2026-05-18 01:35:28 -05:00

258 lines
6.9 KiB
C++

#pragma once
#ifndef _NB_ERROR
#define _NB_ERROR
#include <exception>
#include <memory>
#include <string>
#include <string_view>
#include <type_traits>
#include "TypeTraits.hpp"
#include <unordered_map>
#include "Utils.hpp"
/* #ifndef THROW_WITH_INFO
#ifdef CODE_ERROR_LOCATIONS
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
#else
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__)
#endif // CODE_ERROR_LOCATIONS
#endif // THROW_WITH_INFO
#ifndef THROW
#ifdef LOGGING
#define THROW_WTIH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
#else
#define THROW(...) THROW_WITH_INFO(__VA_ARGS__)
#endif // LOGGING
#endif // THROW */
namespace nb {
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
template <typename T>
using has_type = decltype(T::type);
template<class ErrorType = NoneType>
class ErrorBase;
template<>
class ErrorBase<NoneType> : public std::exception {
public:
const unsigned int code;
const std::string msg;
const std::shared_ptr<const std::exception> trace;
const bool traceIsNBError;
virtual inline std::string str() const noexcept = 0;
virtual const char* what() const noexcept override final {
const std::string& ret = str();
return ret.c_str();
}
protected:
template <
typename TraceType = std::exception,
std::enable_if_t<std::is_base_of_v<std::exception, TraceType>, bool> = true
>
ErrorBase(
const unsigned int& code_,
const std::string& msg_,
const std::shared_ptr<TraceType> trace_,
const unsigned int& line_,
std::string filename_
) noexcept :
code(code_),
msg(msg_),
trace{std::dynamic_pointer_cast<std::exception>(trace_)},
traceIsNBError( trace_ ? is_detected<has_type, TraceType>::value : false)
{
static_assert(std::is_base_of<std::exception, TraceType>::value,
"`trace_` must be a pointer to a child object of std::exception."
);
}
};
template <class ErrorType>
class ErrorBase : public ErrorBase<NoneType> {
using Base = ErrorBase<NoneType>;
public:
ErrorBase(const ErrorBase&) = default;
ErrorBase& operator=(const ErrorBase&) = delete;
virtual inline std::string str() const noexcept override {
std::string ret = msg;
if (trace) {
const std::string replace = nb::NEWLINE+" ";
std::string trace_msg;
if (traceIsNBError) {
trace_msg = std::static_pointer_cast<const ErrorBase>(trace)->str();
trace_msg = std::string(std::string_view(trace_msg).substr(
0, trace_msg.length()-nb::NEWLINE.length()
));
} else {
trace_msg = std::string(trace->what());
}
ret += replace + "Trace: " + find_and_replace<std::string>(
trace_msg,
nb::NEWLINE,
replace
);
}
return ret + nb::NEWLINE;
}
static const std::string type;
static const ErrorCodeMap ErrorMessages;
using Base::code;
using Base::msg;
using Base::trace;
using Base::traceIsNBError;
protected:
using Base::Base;
template <
typename TraceType = std::exception,
std::enable_if_t<std::is_base_of_v<std::exception, TraceType>, bool> = true
>
ErrorBase(
const unsigned int code_,
std::string msg_,
const TraceType* trace_=nullptr,
unsigned int line_=0,
std::string filename_=""
) : Base(
code_,
msg_,
trace_ ? std::make_shared<TraceType>(*trace_) : nullptr,
line_,
filename_
) {
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."
);
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."
);
}
template <
typename TraceType,
std::enable_if_t<std::is_base_of_v<std::exception, TraceType>, bool> = true
>
ErrorBase(
const unsigned int& code_,
const TraceType& trace_,
unsigned int line_=0,
std::string filename_=""
) noexcept : ErrorBase(
code_,
ErrorType::ErrorMessages.at(code_),
&trace_,
line_,
filename_
) {}
template <
typename TraceType,
std::enable_if_t<std::is_base_of_v<std::exception, TraceType>, bool> = true
>
ErrorBase(
const std::string& msg_,
const TraceType& trace_,
unsigned int line_=0,
std::string filename_=""
) noexcept : ErrorBase(
0,
msg_,
&trace_,
line_,
filename_
) {}
ErrorBase(
const unsigned int& code_,
unsigned int line_=0,
std::string filename_=""
) noexcept : ErrorBase(
code_,
ErrorType::ErrorMessages.at(code_),
static_cast<std::exception*>(nullptr),
line_,
filename_
) {}
ErrorBase(
const std::string& msg_,
unsigned int line_=0,
std::string filename_=""
) noexcept : ErrorBase(
0,
msg_,
nullptr,
line_,
filename_
) {}
};
template <typename ErrorType>
const std::string ErrorBase<ErrorType>::type = ErrorType::type;
template <typename ErrorType>
const ErrorCodeMap ErrorBase<ErrorType>::ErrorMessages = ErrorType::ErrorMessages;
template <class ErrorType = NoneType>
class Error;
template<>
class Error<NoneType>;
template <class ErrorType>
class Error : public ErrorBase<ErrorType> {
using Base = ErrorBase<ErrorType>;
public:
template <typename... Args>
Error(Args... T) : Base(T...) {}
using Base::str;
using Base::what;
using Base::code;
using Base::msg;
using Base::trace;
using Base::type;
using Base::ErrorMessages;
friend ErrorBase<Error>;
};
template<>
class Error<NoneType> : public ErrorBase<Error<NoneType>> {
using Base = ErrorBase<Error<NoneType>>;
public:
using Base::Base;
Error() : Base(0) {}
enum Codes : unsigned int {
GENERAL, UNDEFINED, BADERRORCODE
};
using Base::str;
using Base::what;
using Base::code;
using Base::msg;
using Base::trace;
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
} // namespace nb
#endif // _NB_ERROR