summaryrefslogtreecommitdiff
path: root/forstio/core/error.cpp
blob: 757e6a8122a75f34c0ca660297da6de01512122b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#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