-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor nit-* cli main functions with guards
- Loading branch information
Showing
4 changed files
with
194 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <exception> | ||
#include <iostream> | ||
|
||
#include <boost/exception/all.hpp> | ||
|
||
#include <nit/error.h> | ||
|
||
/// Guards a main function from exceptions. | ||
/// | ||
/// @tparam T Function type returning integer code. | ||
/// | ||
/// @param[in] runner The body of the main function. | ||
/// | ||
/// @returns The return of the runner if no exceptions are encountered. | ||
/// 1 if an exception is encountered. | ||
template <class T> | ||
int guard(T&& runner) noexcept { | ||
try { | ||
return runner(); | ||
} catch (const nit::Error& nit_err) { | ||
std::cerr << boost::diagnostic_information(nit_err); | ||
return 1; | ||
} catch (const boost::exception& boost_err) { | ||
std::cerr << "Unexpected Boost Exception:\n" | ||
<< boost::diagnostic_information(boost_err); | ||
return 1; | ||
} catch (const std::exception& err) { | ||
std::cerr << "Unexpected Exception:\n" << err.what(); | ||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.