Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
set(DOMAIN_SOURCES | ||
application_service.cpp | ||
domain_error.cpp | ||
title.cpp | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
set(DOMAIN_TEST_SOURCES | ||
application_service_test.cpp | ||
domain_error_test.cpp | ||
title_test.cpp | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} |