Skip to content

Commit 73469cb

Browse files
committed
add domain_error enum with std::error_code support
I want to us std::expected<T, std::error_code> for error handling, so proper support of std::error_code in terms of the fix bounded context is needed
1 parent fefb96b commit 73469cb

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

src/domain/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set(DOMAIN_SOURCES
22
application_service.cpp
3+
domain_error.cpp
34
title.cpp
45
)
56

src/domain/domain_error.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "domain_error.hpp"
2+
3+
namespace fix::domain {
4+
namespace {
5+
6+
std::string error_message(fix::domain::domain_error error) {
7+
switch (error) {
8+
case domain_error::MISSING_IMPLEMENTATION:
9+
return "feature is not implemented yet";
10+
}
11+
return "unknown domain error";
12+
}
13+
14+
// =========================================================
15+
// wiring to enable use of std::error_code with domain_error
16+
// =========================================================
17+
18+
struct fix_error_category_t : std::error_category {
19+
[[nodiscard]] char const* name() const noexcept override {
20+
return "fix_domain_error";
21+
}
22+
23+
[[nodiscard]] std::string message(int condition) const override {
24+
return error_message(static_cast<fix::domain::domain_error>(condition));
25+
};
26+
};
27+
28+
const fix_error_category_t fix_error_category;
29+
} // namespace
30+
31+
std::error_code make_error_code(domain_error error) {
32+
return {static_cast<int>(error), fix_error_category};
33+
}
34+
35+
} // namespace fix::domain

src/domain/domain_error.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef FIX_SRC_DOMAIN_DOMAIN_ERROR_HPP
2+
#define FIX_SRC_DOMAIN_DOMAIN_ERROR_HPP
3+
4+
#include <system_error>
5+
6+
namespace fix::domain {
7+
8+
enum class domain_error {
9+
MISSING_IMPLEMENTATION = 1,
10+
};
11+
12+
}
13+
14+
// =========================================================
15+
// wiring to enable use of std::error_code with domain_error
16+
// =========================================================
17+
namespace std {
18+
template<>
19+
struct is_error_code_enum<fix::domain::domain_error> : true_type {};
20+
} // namespace std
21+
22+
namespace fix::domain {
23+
std::error_code make_error_code(domain_error);
24+
}
25+
26+
#endif // FIX_SRC_DOMAIN_DOMAIN_ERROR_HPP

test/domain/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
set(DOMAIN_TEST_SOURCES
22
application_service_test.cpp
3+
domain_error_test.cpp
34
title_test.cpp
45
)
56

test/domain/domain_error_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <catch2/catch.hpp>
2+
3+
#include "domain_error.hpp"
4+
5+
using namespace fix::domain;
6+
7+
TEST_CASE("Domain errors can be used with std::error_code") {
8+
std::error_code error_code = domain_error::MISSING_IMPLEMENTATION;
9+
10+
CHECK(error_code.message() == "feature is not implemented yet");
11+
}

0 commit comments

Comments
 (0)