Could we avoid adding a public NonFatalCheckError factory? The new formatting is only used by Assert, Assume, and CHECK_NONFATAL, so a helper for the formatted string keeps the generic exception constructor as the only public error API:
diff --git a/src/test/util_check_tests.cpp b/src/test/util_check_tests.cpp
index 3a47ecb3c2..49954dff9c 100644
--- a/src/test/util_check_tests.cpp
+++ b/src/test/util_check_tests.cpp
@@ -34,13 +34,4 @@ BOOST_AUTO_TEST_CASE(check_fail)
BOOST_CHECK_EXCEPTION(Assert(this_should_be_nonnull != nullptr), NonFatalCheckError, HasReason{"Internal bug detected: Failed: 'this_should_be_nonnull != nullptr'"});
}
-BOOST_AUTO_TEST_CASE(check_exception_constructor)
-{
- const auto assert_text{"pointer != nullptr"};
- const auto location{std::source_location::current()};
-
- BOOST_CHECK(std::string_view{NonFatalCheckError("some_message", location).what()}.find("Internal bug detected: some_message\n") != std::string_view::npos);
- BOOST_CHECK(std::string_view{NonFatalCheckError::CreateFailedAssert(assert_text, location).what()}.find("Internal bug detected: Failed: 'pointer != nullptr'\n") != std::string_view::npos);
-}
-
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/util/check.cpp b/src/util/check.cpp
index 36885d6db1..e26f3d7b41 100644
--- a/src/util/check.cpp
+++ b/src/util/check.cpp
@@ -15,6 +15,11 @@
#include <string>
#include <string_view>
+std::string StrFormatFailedCheck(std::string_view assertion)
+{
+ return strprintf("Failed: '%s'", assertion);
+}
+
std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc)
{
return strprintf("Internal bug detected: %s\n%s:%d (%s)\n"
@@ -29,17 +34,12 @@ NonFatalCheckError::NonFatalCheckError(std::string_view msg, const std::source_l
{
}
-NonFatalCheckError NonFatalCheckError::CreateFailedAssert(std::string_view assertion, const std::source_location& loc)
-{
- return NonFatalCheckError(strprintf("Failed: '%s'", assertion), loc);
-}
-
bool g_detail_test_only_CheckFailuresAreExceptionsNotAborts{false};
void assertion_fail(const std::source_location& loc, std::string_view assertion)
{
if (g_detail_test_only_CheckFailuresAreExceptionsNotAborts) {
- throw NonFatalCheckError::CreateFailedAssert(assertion, loc);
+ throw NonFatalCheckError{StrFormatFailedCheck(assertion), loc};
}
auto str = strprintf("%s:%s %s: Assertion `%s' failed.\n", loc.file_name(), loc.line(), loc.function_name(), assertion);
fwrite(str.data(), 1, str.size(), stderr);
diff --git a/src/util/check.h b/src/util/check.h
index 4a7a52b6a8..3ffe8af282 100644
--- a/src/util/check.h
+++ b/src/util/check.h
@@ -57,13 +57,12 @@ struct test_only_CheckFailuresAreExceptionsNotAborts {
};
std::string StrFormatInternalBug(std::string_view msg, const std::source_location& loc);
+std::string StrFormatFailedCheck(std::string_view assertion);
class NonFatalCheckError : public std::runtime_error
{
public:
NonFatalCheckError(std::string_view msg, const std::source_location& loc);
- /// Create an instance in case of a failed assert condition, message is constructed accordingly
- static NonFatalCheckError CreateFailedAssert(std::string_view assertion, const std::source_location& loc);
};
/** Internal helper */
@@ -77,7 +76,7 @@ T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const std::source_location& lo
if constexpr (G_ABORT_ON_FAILED_ASSUME) {
assertion_fail(loc, assertion);
}
- throw NonFatalCheckError::CreateFailedAssert(assertion, loc);
+ throw NonFatalCheckError{StrFormatFailedCheck(assertion), loc};
}
return std::forward<T>(val);
}