55 lines
1.2 KiB
C++
55 lines
1.2 KiB
C++
#pragma once
|
|
#ifndef _NB_ERROR
|
|
#define _NB_ERROR
|
|
|
|
#include <exception>
|
|
#include <string.h>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
namespace nb {
|
|
|
|
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
|
|
|
|
class Error : public std::exception {
|
|
public:
|
|
Error(const unsigned int code=0, std::exception* const trace=nullptr) noexcept;
|
|
Error(std::string, std::exception* const=nullptr) noexcept;
|
|
Error(const Error&) noexcept;
|
|
|
|
unsigned int code() const noexcept;
|
|
virtual const char* type() const noexcept final;
|
|
virtual const char* what() const noexcept override final;
|
|
|
|
template<typename NBError>
|
|
friend const char* ErrorCodeLookup(unsigned int);
|
|
protected:
|
|
static const ErrorCodeMap ErrorCodes;
|
|
Error(
|
|
const unsigned int,
|
|
std::string,
|
|
std::exception* const,
|
|
unsigned int line=0,
|
|
std::string filename=""
|
|
) noexcept;
|
|
|
|
const char* _type;
|
|
const unsigned int _code;
|
|
std::string _msg;
|
|
};
|
|
|
|
template<typename NBError>
|
|
const char* ErrorCodeLookup(unsigned int code) {
|
|
for (auto kv : NBError::ErrorCodes) {
|
|
if (kv.first == code) {
|
|
return kv.second;
|
|
}
|
|
}
|
|
|
|
throw Error(666);
|
|
}
|
|
|
|
} // namespace nb
|
|
|
|
|
|
#endif // _NB_ERROR
|