256 lines
7.2 KiB
C++
256 lines
7.2 KiB
C++
#pragma once
|
|
#include <memory>
|
|
#ifndef _NB_ERROR
|
|
#define _NB_ERROR
|
|
|
|
#include <exception>
|
|
#include <iostream>
|
|
#include <string>
|
|
#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<typename StringType, typename ErrorType>
|
|
struct NBErrorWhatString;
|
|
|
|
class ErrorBase_what_str_impl : public std::exception {
|
|
public:
|
|
virtual std::string what_str_impl() const = 0;
|
|
virtual std::wstring what_wstr_impl() const = 0;
|
|
};
|
|
|
|
template<class ErrorType>
|
|
class ErrorBase : public ErrorBase_what_str_impl {
|
|
public:
|
|
ErrorBase(const ErrorBase&) = default;
|
|
ErrorBase& operator=(const ErrorBase&) = delete;
|
|
|
|
template<typename StringType = std::string>
|
|
inline const StringType what_str() const noexcept {
|
|
return NBErrorWhatString<StringType,ErrorType>::what(*this);
|
|
}
|
|
|
|
virtual const char* what() const noexcept override final {
|
|
const std::string& ret = what_str();
|
|
return ret.c_str();
|
|
}
|
|
|
|
static const std::string type;
|
|
static const ErrorCodeMap ErrorMessages;
|
|
|
|
const unsigned int code;
|
|
const std::string msg;
|
|
const std::shared_ptr<const std::exception> trace;
|
|
const bool traceIsNBError;
|
|
|
|
protected:
|
|
ErrorBase() = delete;
|
|
|
|
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_=""
|
|
) noexcept :
|
|
code(code_),
|
|
msg(msg_),
|
|
trace{trace_ ? std::make_shared<TraceType>(*trace_) : nullptr},
|
|
traceIsNBError( trace_ ? is_detected<has_type, TraceType>::value : false)
|
|
{
|
|
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."
|
|
);
|
|
static_assert(std::is_base_of<std::exception, TraceType>::value,
|
|
"`trace_` must be a pointer to a child object of std::exception."
|
|
);
|
|
}
|
|
|
|
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_
|
|
) {}
|
|
|
|
std::string what_str_impl() const override final { return what_str(); }
|
|
std::wstring what_wstr_impl() const override final { return what_str<std::wstring>(); }
|
|
};
|
|
template <typename ErrorType>
|
|
const std::string ErrorBase<ErrorType>::type = ErrorType::type;
|
|
template <typename ErrorType>
|
|
const ErrorCodeMap ErrorBase<ErrorType>::ErrorMessages = ErrorType::ErrorMessages;
|
|
|
|
template<typename ErrorType>
|
|
struct NBErrorWhatString<std::wstring, ErrorType> {
|
|
inline static const std::wstring what(const ErrorBase<ErrorType>& err) noexcept {
|
|
std::wstring ret = nb::str_to_wstr(err.msg);
|
|
const std::wstring replace = nb::WNEWLINE + L"┗-";
|
|
if (err.trace) {
|
|
std::wstring trace_msg;
|
|
if (err.traceIsNBError) {
|
|
// How can I access
|
|
trace_msg = std::static_pointer_cast<const ErrorBase_what_str_impl>(err.trace)->what_wstr_impl();
|
|
} else {
|
|
trace_msg = nb::str_to_wstr(std::string(err.trace->what()));
|
|
}
|
|
trace_msg = find_and_replace<std::wstring>(trace_msg, L"┗", L"-");
|
|
ret += replace + find_and_replace<std::wstring>(
|
|
trace_msg,
|
|
nb::WNEWLINE,
|
|
replace
|
|
);
|
|
}
|
|
return ret;
|
|
}
|
|
};
|
|
|
|
template<typename ErrorType>
|
|
struct NBErrorWhatString<std::string, ErrorType> {
|
|
inline static const std::string what(const ErrorBase<ErrorType>& err) noexcept {
|
|
std::string ret = err.msg;
|
|
const std::string replace = nb::NEWLINE+" ";
|
|
if (err.trace) {
|
|
ret += replace + find_and_replace(
|
|
std::string(err.trace->what()),
|
|
nb::NEWLINE,
|
|
replace
|
|
);
|
|
}
|
|
return ret;
|
|
}
|
|
};
|
|
|
|
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::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::what;
|
|
using Base::code;
|
|
using Base::msg;
|
|
using Base::trace;
|
|
|
|
static const std::string type;
|
|
static const ErrorCodeMap ErrorMessages;
|
|
};
|
|
|
|
} // namespace nb
|
|
|
|
#endif // _NB_ERROR
|