Skip to content

Commit

Permalink
New feature: aggregate_error::throw_nested, a more concise, clear ver…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
refvalue committed Nov 29, 2024
1 parent 73cb8c3 commit 06c0e44
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 351 deletions.
26 changes: 8 additions & 18 deletions include/essence/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@
#include "abi/vector.hpp"
#include "compat.hpp"

#include <concepts>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <stdexcept>
#include <string_view>
#include <type_traits>
#include <utility>

namespace essence {
Expand All @@ -53,6 +51,14 @@ namespace essence {
[[noreturn]] ES_API(CPPESSENCE) static void flatten_and_throw(
const std::exception_ptr& root, std::int32_t indent = default_nested_exception_indent);

template <typename E>
[[noreturn]] ES_API(CPPESSENCE) static void throw_nested(
E&& ex, std::int32_t indent = default_nested_exception_indent) try {
std::throw_with_nested(std::forward<E>(ex));
} catch (std::exception&) {
flatten_and_throw(std::current_exception(), indent);
}

template <typename E, bool Reverse = false>
std::optional<E> extract() const {
auto iter = [this] {
Expand Down Expand Up @@ -92,20 +98,4 @@ namespace essence {
ES_API(CPPESSENCE)
abi::string serialize_nested_exceptions(
const std::exception_ptr& root, std::int32_t indent = default_nested_exception_indent);

#if ES_HAS_CXX20
template <typename E, std::invocable Callable>
#else
template <typename E, typename Callable, std::enable_if_t<std::is_invocable_v<Callable>>* = nullptr>
#endif
decltype(auto) throw_nested_and_flatten(
E&& ex, Callable&& callable, std::int32_t indent = default_nested_exception_indent) try {
try {
return std::forward<Callable>(callable)();
} catch (...) {
std::throw_with_nested(std::forward<E>(ex));
}
} catch (const std::exception&) {
aggregate_error::flatten_and_throw(std::current_exception(), indent);
}
} // namespace essence
119 changes: 0 additions & 119 deletions include/essence/json_extensions.hpp

This file was deleted.

19 changes: 11 additions & 8 deletions include/essence/net/http_client_abstract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,17 @@ namespace essence::net {

auto tuple = std::forward_as_tuple(std::forward<T>(message));

return throw_nested_and_flatten(
source_code_aware_runtime_error{U8("Error"), U8("Failed to commit the message."), U8("Entity"),
meta::get_literal_string_t<decayed_type,
meta::identifier_param{
.shortened = true,
}>(),
U8("Raw Message"), message_content},
[&] { return std::apply(handler, std::move(tuple)); });
try {
return std::apply(handler, std::move(tuple));
} catch (const std::exception& ex) {
aggregate_error::throw_nested(
source_code_aware_runtime_error{U8("Error"), U8("Failed to commit the message."), U8("Entity"),
meta::get_literal_string_t<decayed_type,
meta::identifier_param{
.shortened = true,
}>(),
U8("Raw Message"), message_content});
}
}

private:
Expand Down
148 changes: 0 additions & 148 deletions include/essence/reflection.hpp

This file was deleted.

2 changes: 0 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,13 @@ target_sources(
${ES_PUBLIC_INCLUDE_DIR}/essence/hashing.hpp
${ES_PUBLIC_INCLUDE_DIR}/essence/json.hpp
${ES_PUBLIC_INCLUDE_DIR}/essence/json_compat.hpp
${ES_PUBLIC_INCLUDE_DIR}/essence/json_extensions.hpp
${ES_PUBLIC_INCLUDE_DIR}/essence/managed_handle.hpp
${ES_PUBLIC_INCLUDE_DIR}/essence/native_handle.hpp
${ES_PUBLIC_INCLUDE_DIR}/essence/noncopyable.hpp
${ES_PUBLIC_INCLUDE_DIR}/essence/numeric_conversion.hpp
${ES_PUBLIC_INCLUDE_DIR}/essence/optional.hpp
${ES_PUBLIC_INCLUDE_DIR}/essence/range.hpp
${ES_PUBLIC_INCLUDE_DIR}/essence/rect.hpp
${ES_PUBLIC_INCLUDE_DIR}/essence/reflection.hpp
${ES_PUBLIC_INCLUDE_DIR}/essence/scope.hpp
${ES_PUBLIC_INCLUDE_DIR}/essence/source_location.hpp
${ES_PUBLIC_INCLUDE_DIR}/essence/span.hpp
Expand Down
9 changes: 6 additions & 3 deletions src/crypto/params/ec_keygen_param.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
#include "error_extensions.hpp"
#include "exception.hpp"

#include <exception>

namespace essence::crypto {
void* ec_keygen_param::generate_key_blob() const {
return throw_nested_and_flatten(source_code_aware_runtime_error{U8("Curve Name"), curve_name},
[this] { return evp_pkey_q_keygen(U8("EC"), curve_name.c_str()).release(); });
void* ec_keygen_param::generate_key_blob() const try {
return evp_pkey_q_keygen(U8("EC"), curve_name.c_str()).release();
} catch (const std::exception&) {
aggregate_error::throw_nested(source_code_aware_runtime_error{U8("Curve Name"), curve_name});
}
} // namespace essence::crypto
9 changes: 6 additions & 3 deletions src/crypto/params/rsa_keygen_param.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
#include "error_extensions.hpp"
#include "exception.hpp"

#include <exception>

namespace essence::crypto {
void* rsa_keygen_param::generate_key_blob() const {
return throw_nested_and_flatten(source_code_aware_runtime_error{U8("Key Bits"), key_bits},
[this] { return evp_pkey_q_keygen(U8("RSA"), key_bits).release(); });
void* rsa_keygen_param::generate_key_blob() const try {
return evp_pkey_q_keygen(U8("RSA"), key_bits).release();
} catch (const std::exception&) {
aggregate_error::throw_nested(source_code_aware_runtime_error{U8("Key Bits"), key_bits});
}
} // namespace essence::crypto
Loading

0 comments on commit 06c0e44

Please sign in to comment.