forstio/source/forstio/error.cpp

74 lines
1.9 KiB
C++

#include "error.h"
namespace saw {
error::error() : error_{static_cast<error::code>(0)} {}
error::error(const std::string_view &msg, error::code code)
: error_message_{msg}, error_{static_cast<error::code>(code)} {}
error::error(std::string &&msg, error::code code)
: error_message_{std::move(msg)}, error_{static_cast<error::code>(code)} {}
error::error(error &&error)
: 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_);
}
bool error::failed() const {
return static_cast<std::underlying_type_t<error::code>>(error_) != 0;
}
bool error::is_critical() const {
return static_cast<std::underlying_type_t<error::code>>(error_) < 0;
}
bool error::is_recoverable() const {
return static_cast<std::underlying_type_t<error::code>>(error_) > 0;
}
error error::copy_error() const {
error error;
error.error_ = error_;
try {
error.error_message_ = error_message_;
} catch (const std::bad_alloc &) {
error.error_message_ =
std::string_view{"Error while copying Error string. Out of memory"};
}
return error;
}
error::code error::id() const { return static_cast<error::code>(error_); }
error make_error(const std::string_view &generic, error::code code) {
return error{generic, code};
}
error critical_error(const std::string_view &generic, error::code c) {
return make_error(generic, c);
}
error recoverable_error(const std::string_view &generic, error::code c) {
return make_error(generic, c);
}
error no_error() { return error{}; }
} // namespace saw