forstio/source/kelgin/error.cpp

74 lines
1.9 KiB
C++
Raw Normal View History

2020-07-28 01:23:38 +02:00
#include "error.h"
namespace gin {
Error::Error() : error_{static_cast<Error::Code>(0)} {}
2020-08-09 02:03:09 +02:00
Error::Error(const std::string_view &msg, Error::Code code)
: error_message{msg}, error_{static_cast<Error::Code>(code)} {}
2020-08-09 02:03:09 +02:00
Error::Error(std::string &&msg, Error::Code code)
: error_message{std::move(msg)}, error_{static_cast<Error::Code>(code)} {}
2021-03-13 22:22:01 +01:00
Error::Error(Error &&error)
2020-08-20 00:36:21 +02:00
: error_message{std::move(error.error_message)}, error_{std::move(
error.error_)} {}
const std::string_view Error::message() const {
return std::visit(
[this](auto &&arg) -> const std::string_view {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, std::string>) {
return std::string_view{arg};
} else if constexpr (std::is_same_v<T, std::string_view>) {
return arg;
} else {
return "Error in class Error. Good luck :)";
}
},
error_message);
2021-03-17 11:24:18 +01:00
}
2020-08-09 02:03:09 +02:00
bool Error::failed() const {
return static_cast<std::underlying_type_t<Error::Code>>(error_) != 0;
}
2020-08-09 02:03:09 +02:00
bool Error::isCritical() const {
return static_cast<std::underlying_type_t<Error::Code>>(error_) < 0;
}
2020-08-09 02:03:09 +02:00
bool Error::isRecoverable() const {
return static_cast<std::underlying_type_t<Error::Code>>(error_) > 0;
}
2020-08-09 02:03:09 +02:00
2021-03-13 22:22:01 +01:00
Error Error::copyError() const {
Error error;
error.error_ = error_;
try {
error.error_message = error_message;
2021-04-20 15:26:30 +02:00
} catch (const std::bad_alloc &) {
error.error_message =
std::string_view{"Error while copying Error string. Out of memory"};
2021-03-13 22:22:01 +01:00
}
return error;
}
2020-08-09 02:03:09 +02:00
Error::Code Error::code() const { return static_cast<Error::Code>(error_); }
2021-06-04 23:21:25 +02:00
Error makeError(const std::string_view &generic, Error::Code code) {
return Error{generic, code};
}
Error criticalError(const std::string_view &generic, Error::Code c) {
return makeError(generic, c);
2021-03-17 11:34:37 +01:00
}
Error recoverableError(const std::string_view &generic, Error::Code c) {
return makeError(generic, c);
2021-03-17 11:34:37 +01:00
}
2020-08-09 02:03:09 +02:00
Error noError() { return Error{}; }
2021-03-13 22:22:01 +01:00
2020-08-09 02:04:48 +02:00
} // namespace gin