Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
arnemertz committed Aug 21, 2021
1 parent fefb96b commit 73469cb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/domain/CMakeLists.txt
@@ -1,5 +1,6 @@
set(DOMAIN_SOURCES
application_service.cpp
domain_error.cpp
title.cpp
)

Expand Down
35 changes: 35 additions & 0 deletions src/domain/domain_error.cpp
@@ -0,0 +1,35 @@
#include "domain_error.hpp"

namespace fix::domain {
namespace {

std::string error_message(fix::domain::domain_error error) {
switch (error) {
case domain_error::MISSING_IMPLEMENTATION:
return "feature is not implemented yet";
}
return "unknown domain error";
}

// =========================================================
// wiring to enable use of std::error_code with domain_error
// =========================================================

struct fix_error_category_t : std::error_category {
[[nodiscard]] char const* name() const noexcept override {
return "fix_domain_error";
}

[[nodiscard]] std::string message(int condition) const override {
return error_message(static_cast<fix::domain::domain_error>(condition));
};
};

const fix_error_category_t fix_error_category;
} // namespace

std::error_code make_error_code(domain_error error) {
return {static_cast<int>(error), fix_error_category};
}

} // namespace fix::domain
26 changes: 26 additions & 0 deletions src/domain/domain_error.hpp
@@ -0,0 +1,26 @@
#ifndef FIX_SRC_DOMAIN_DOMAIN_ERROR_HPP
#define FIX_SRC_DOMAIN_DOMAIN_ERROR_HPP

#include <system_error>

namespace fix::domain {

enum class domain_error {
MISSING_IMPLEMENTATION = 1,
};

}

// =========================================================
// wiring to enable use of std::error_code with domain_error
// =========================================================
namespace std {
template<>
struct is_error_code_enum<fix::domain::domain_error> : true_type {};
} // namespace std

namespace fix::domain {
std::error_code make_error_code(domain_error);
}

#endif // FIX_SRC_DOMAIN_DOMAIN_ERROR_HPP
1 change: 1 addition & 0 deletions test/domain/CMakeLists.txt
@@ -1,5 +1,6 @@
set(DOMAIN_TEST_SOURCES
application_service_test.cpp
domain_error_test.cpp
title_test.cpp
)

Expand Down
11 changes: 11 additions & 0 deletions test/domain/domain_error_test.cpp
@@ -0,0 +1,11 @@
#include <catch2/catch.hpp>

#include "domain_error.hpp"

using namespace fix::domain;

TEST_CASE("Domain errors can be used with std::error_code") {
std::error_code error_code = domain_error::MISSING_IMPLEMENTATION;

CHECK(error_code.message() == "feature is not implemented yet");
}

0 comments on commit 73469cb

Please sign in to comment.