Skip to content

Commit

Permalink
FEAT: Added a capture function for nested exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
NateAM committed Apr 2, 2024
1 parent c4da9c2 commit ef930a8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
7 changes: 6 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ Changelog
#########

******************
0.7.3 (unreleased)
0.8.0 (2024-04-02)
******************

New Features
============
- Added a catch function to replace the if statement pattern (:pull:`2`). By `Nathan Miller`_.
- Added parenthesis to the catch function to try and prevent issues (:pull:`3`). By `Nathan Miller`_.
- Added a function to capture the error message in a standard string (:pull:`4`). By `Nathan Miller`_.

Breaking Changes
================
- Changed the printNestedExceptions interface to only accept the exception and a header message. This will affect almost no-one but it is breaking (:pull:`4`). By `Nathan Miller`_.

******************
0.7.2 (2023-09-27)
Expand Down
25 changes: 19 additions & 6 deletions src/cpp/tardigrade_error_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ namespace tardigradeErrorTools{
return;
}

void printNestedExceptions( const std::exception &e, std::size_t depth, std::string message){
void captureNestedExceptions( const std::exception &e, std::string &message, std::size_t depth ){
/*!
* Print the nested exceptions starting with the one deepest (closest to the error) and ending
* with the outermost function call. Users should call this function by just passing in the
* exception. The other arguments are for recursion.
* Capture the nested exceptions starting with the one deepest (closest to the error) and ending
* with the outermost function call. Users should typically call this function by just passing in the
* exception and a std::string for the message. The depth is for recursion.
*
* \param &e: The nested exceptions
* \param depth: The current depth (defaults to zero)
Expand All @@ -94,14 +94,27 @@ namespace tardigradeErrorTools{
try {

std::rethrow_if_nested( e );
std::cerr << message;

}
catch ( const std::exception &nested ){

printNestedExceptions( nested, depth + 1, message );
captureNestedExceptions( nested, message, depth + 1 );

}
}

void printNestedExceptions( const std::exception &e, std::string message ){
/*!
* Print the nested exceptions starting with the one deepest (closest to the error) and ending
* with the outermost function call. Users should call this function by just passing in the
* exception.
*
* \param &e: The nested exceptions
* \param message: The output message (defaults to "")
*/

captureNestedExceptions( e, message );
std::cerr << message;
}

}
4 changes: 3 additions & 1 deletion src/cpp/tardigrade_error_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ namespace tardigradeErrorTools{
void print( const bool header = true );
};

void printNestedExceptions( const std::exception &e, std::size_t depth = 0, std::string message = "");
void captureNestedExceptions( const std::exception &e, std::string &message, std::size_t depth = 0);

void printNestedExceptions( const std::exception &e, std::string message = "" );

}

Expand Down

0 comments on commit ef930a8

Please sign in to comment.