Skip to content

Commit 9b24006

Browse files
committed
Use full path and remove all features for specifying relative paths. Update unit tests
1 parent 6e4e637 commit 9b24006

File tree

6 files changed

+63
-72
lines changed

6 files changed

+63
-72
lines changed

src/ystdlib/error_handling/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ cpp_library(
44
PUBLIC_HEADERS
55
ErrorCode.hpp
66
TraceableException.hpp
7-
SourceLocation.hpp
7+
types.hpp
8+
utils.hpp
89
TESTS_SOURCES
910
test/constants.hpp
1011
test/test_ErrorCode.cpp

src/ystdlib/error_handling/SourceLocation.hpp

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/ystdlib/error_handling/TraceableException.hpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
#ifndef YSTDLIB_ERROR_HANDLING_TRACEABLEEXCEPTION_HPP
22
#define YSTDLIB_ERROR_HANDLING_TRACEABLEEXCEPTION_HPP
33

4-
#include <concepts>
54
#include <exception>
65
#include <source_location>
6+
#include <sstream>
77
#include <string>
88
#include <system_error>
99
#include <utility>
1010

11-
#include "SourceLocation.hpp"
11+
#include "types.hpp"
12+
#include "utils.hpp"
1213

1314
namespace ystdlib::error_handling {
14-
/**
15-
* Concept that defines a template parameter of an integer-based error code enumeration.
16-
* @tparam Type
17-
*/
18-
template <typename Type>
19-
concept ErrorCodeType
20-
= std::same_as<Type, std::error_code> || std::convertible_to<Type, std::error_code>;
21-
2215
/**
2316
* An exception class that is thrown with an `std::error_code`.
2417
*
@@ -30,21 +23,23 @@ concept ErrorCodeType
3023
* @see std::source_location::function_name()
3124
* @see std::source_location::line()
3225
*/
33-
template <typename ErrorCodeType>
26+
template <ErrorCodeType ErrorType = std::error_code>
3427
class TraceableException : public std::exception {
3528
public:
3629
// Constructors
3730
explicit TraceableException(
38-
ErrorCodeType error_code,
31+
ErrorType error_code,
3932
std::source_location const& where = std::source_location::current()
4033
)
4134
: m_error_code{std::move(error_code)},
4235
m_where{where} {
43-
m_what = m_where.str();
36+
std::ostringstream oss;
37+
oss << where;
38+
m_what = oss.str();
4439
}
4540

4641
explicit TraceableException(
47-
ErrorCodeType error_code,
42+
ErrorType error_code,
4843
std::string what,
4944
std::source_location const& where = std::source_location::current()
5045
)
@@ -56,26 +51,30 @@ class TraceableException : public std::exception {
5651
[[nodiscard]] auto what() const noexcept -> char const* override { return m_what.c_str(); }
5752

5853
// Methods
59-
[[nodiscard]] auto error_code() const -> ErrorCodeType { return m_error_code; }
54+
[[nodiscard]] auto error_code() const -> ErrorType { return m_error_code; }
6055

6156
[[nodiscard]] auto what() -> std::string& { return m_what; }
6257

63-
[[nodiscard]] auto where() const noexcept -> SourceLocation const& { return m_where; }
58+
[[nodiscard]] auto where() const noexcept -> std::source_location const& { return m_where; }
6459

6560
private:
6661
// Variables
67-
ErrorCodeType m_error_code;
62+
ErrorType m_error_code;
6863
std::string m_what;
69-
SourceLocation m_where;
64+
std::source_location m_where;
7065
};
71-
7266
} // namespace ystdlib::error_handling
7367

7468
/**
7569
* The macro to define a `TraceableException` class with the given class name T.
7670
*/
7771
// NOLINTBEGIN(bugprone-macro-parentheses, cppcoreguidelines-macro-usage)
78-
#define YSTDLIB_ERROR_HANDLING_DEFINE_TRACEABLE_EXCEPTION(T, E) \
72+
#define YSTDLIB_ERROR_HANDLING_DEFINE_TRACEABLE_EXCEPTION(T) \
73+
class T : public ystdlib::error_handling::TraceableException<> { \
74+
using ystdlib::error_handling::TraceableException<>::TraceableException; \
75+
}
76+
77+
#define YSTDLIB_ERROR_HANDLING_DEFINE_TRACEABLE_EXCEPTION_WITH_ERROR_TYPE(T, E) \
7978
class T : public ystdlib::error_handling::TraceableException<E> { \
8079
using ystdlib::error_handling::TraceableException<E>::TraceableException; \
8180
}

src/ystdlib/error_handling/test/test_TraceableException.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <cstring>
33

44
#include <ystdlib/error_handling/TraceableException.hpp>
5+
#include <ystdlib/error_handling/types.hpp>
56

67
#include <catch2/catch_test_macros.hpp>
78

@@ -24,7 +25,7 @@ constexpr auto cFailureFuncName{
2425
namespace ystdlib::error_handling::test {
2526
class Worker {
2627
public:
27-
YSTDLIB_ERROR_HANDLING_DEFINE_TRACEABLE_EXCEPTION(OperationFailed, BinaryErrorCode);
28+
YSTDLIB_ERROR_HANDLING_DEFINE_TRACEABLE_EXCEPTION(OperationFailed);
2829

2930
static auto execute_with_success() -> void {
3031
throw OperationFailed(BinaryErrorCode{BinaryErrorCodeEnum::Success});
@@ -40,14 +41,14 @@ class Worker {
4041
} // namespace ystdlib::error_handling::test
4142

4243
namespace {
43-
template <ErrorCodeType E, typename Callable>
44-
[[nodiscard]] auto capture_exception(Callable&& f) -> TraceableException<E>;
44+
template <ErrorCodeType ErrorType = std::error_code, typename Callable>
45+
[[nodiscard]] auto capture_exception(Callable&& f) -> TraceableException<ErrorType>;
4546

46-
template <ErrorCodeType E, typename Callable>
47-
auto capture_exception(Callable&& f) -> TraceableException<E> {
47+
template <ErrorCodeType ErrorType, typename Callable>
48+
auto capture_exception(Callable&& f) -> TraceableException<ErrorType> {
4849
try {
4950
std::forward<Callable>(f)();
50-
} catch (TraceableException<E>& e) {
51+
} catch (TraceableException<ErrorType>& e) {
5152
return e;
5253
}
5354
assert(false && "The function is expected to throw.");
@@ -56,13 +57,13 @@ auto capture_exception(Callable&& f) -> TraceableException<E> {
5657

5758
namespace ystdlib::error_handling::test {
5859
TEST_CASE("test_traceable_exception", "[error_handling][TraceableException]") {
59-
auto const ex_success{capture_exception<BinaryErrorCode>(Worker::execute_with_success)};
60-
REQUIRE((0 == std::strcmp(ex_success.where().file_name(), cCurrentFileName)));
60+
auto const ex_success{capture_exception<>(Worker::execute_with_success)};
61+
REQUIRE(std::string{ex_success.where().file_name()}.ends_with(cCurrentFileName));
6162
REQUIRE((0 == std::strcmp(ex_success.where().function_name(), cSuccessFuncName)));
6263

63-
auto const ex_failure{capture_exception<BinaryErrorCode>(Worker::execute_with_failure)};
64+
auto const ex_failure{capture_exception<>(Worker::execute_with_failure)};
6465
REQUIRE((0 == std::strcmp(ex_failure.what(), cCustomFailureDescription)));
65-
REQUIRE((0 == std::strcmp(ex_failure.where().file_name(), cCurrentFileName)));
66+
REQUIRE(std::string{ex_failure.where().file_name()}.ends_with(cCurrentFileName));
6667
REQUIRE((0 == std::strcmp(ex_failure.where().function_name(), cFailureFuncName)));
6768
}
6869
} // namespace ystdlib::error_handling::test
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef YSTDLIB_ERROR_HANDLING_TYPES_HPP
2+
#define YSTDLIB_ERROR_HANDLING_TYPES_HPP
3+
4+
#include <concepts>
5+
#include <system_error>
6+
7+
namespace ystdlib::error_handling {
8+
/**
9+
* Concept that defines a template parameter of an integer-based error code enumeration.
10+
* @tparam Type
11+
*/
12+
template <typename Type>
13+
concept ErrorCodeType
14+
= std::same_as<Type, std::error_code> || std::convertible_to<Type, std::error_code>;
15+
} // namespace ystdlib::error_handling
16+
17+
#endif // YSTDLIB_ERROR_HANDLING_TYPES_HPP
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef YSTDLIB_ERROR_HANDLING_UTILS_HPP
2+
#define YSTDLIB_ERROR_HANDLING_UTILS_HPP
3+
4+
#include <ostream>
5+
#include <source_location>
6+
7+
namespace ystdlib::error_handling {
8+
inline auto operator<<(std::ostream& os, std::source_location const& where) -> std::ostream& {
9+
return os << where.file_name() << "(" << where.line() << ":" << where.column()
10+
<< "), function `" << where.function_name() << "`";
11+
}
12+
} // namespace ystdlib::error_handling
13+
14+
#endif // YSTDLIB_ERROR_HANDLING_UTILS_HPP

0 commit comments

Comments
 (0)