Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Added an error catch function #2

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Changelog
0.7.3 (unreleased)
******************

New Features
============
- Added a catch function to replace the if statement pattern (:pull:`2`). By `Nathan Miller`_.

******************
0.7.2 (2023-09-27)
******************
Expand Down
20 changes: 20 additions & 0 deletions src/cpp/tardigrade_error_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@
*/
#define TARDIGRADE_ERROR_TOOLS_CATCH_NODE_POINTER(expr) TARDIGRADE_ERROR_TOOLS_CATCH_NODE_POINTER_INTERNAL(expr, __func__, __LINE__, __FILE__)

/*!
\brief A macro to check for issues that we want to throw an exception if a condition isn't met
\param condition: An expression that evaluates as a boolean
\param error_message: The output message if it fails
\param func: A standard string or char function name
\param line: The integer filename
\param file: A standard string or char filename
*/
#define TARDIGRADE_ERROR_TOOLS_CHECK_INTERNAL(condition, error_message, func, line, file) \
if ( ! condition ){ \
TARDIGRADE_ERROR_TOOLS_CATCH(throw std::runtime_error(error_message) ) \
} \

/*!
\brief A macro to check for issues that we want to throw an exception if a condition isn't met
\param condition: An expression that evaluates as a boolean
\param error_message: The output message if it fails
*/
#define TARDIGRADE_ERROR_TOOLS_CHECK(condition, error_message) TARDIGRADE_ERROR_TOOLS_CHECK_INTERNAL(condition, error_message, __func__, __LINE__, __FILE__)


namespace tardigradeErrorTools{

Expand Down
24 changes: 24 additions & 0 deletions src/cpp/tests/test_tardigrade_error_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,27 @@ BOOST_AUTO_TEST_CASE( test_form_node_stacktrace ){
BOOST_CHECK( !result.is_empty( ) );

}

void runCheck( const bool condition ){
TARDIGRADE_ERROR_TOOLS_CHECK(condition, "mymessage")
}

BOOST_AUTO_TEST_CASE( test_tardigrade_error_tools_check ){

boost::test_tools::output_test_stream result;

//Initialize test variables
cerr_redirect guard( result.rdbuf( ) );

BOOST_CHECK_NO_THROW( runCheck( true ) );

try{
TARDIGRADE_ERROR_TOOLS_CATCH( runCheck( false ) );
}
catch( std::exception &e ){
tardigradeErrorTools::printNestedExceptions( e );
}

BOOST_CHECK( !result.is_empty( ) );

}
Loading