Improve exception handling:

* Self-document the code by renaming Throw to Rethrow.
* Write a message to the debug log when we throw or
  rethrow an exception.
This commit is contained in:
Nik Bougalis
2016-05-18 13:00:11 -07:00
parent a039e7593a
commit 97c89168f7
10 changed files with 47 additions and 26 deletions

View File

@@ -603,7 +603,7 @@ Ledger::setup (Config const& config)
} }
catch (std::exception const&) catch (std::exception const&)
{ {
Throw(); Rethrow();
} }
try try
@@ -616,7 +616,7 @@ Ledger::setup (Config const& config)
} }
catch (std::exception const&) catch (std::exception const&)
{ {
Throw(); Rethrow();
} }
return ret; return ret;
@@ -639,7 +639,6 @@ Ledger::peek (Keylet const& k) const
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
bool Ledger::walkLedger (beast::Journal j) const bool Ledger::walkLedger (beast::Journal j) const
{ {
std::vector <SHAMapMissingNode> missingNodes1; std::vector <SHAMapMissingNode> missingNodes1;

View File

@@ -487,7 +487,7 @@ void LedgerConsensusImp::mapComplete (
leaveConsensus(); leaveConsensus();
JLOG (j_.error()) << JLOG (j_.error()) <<
"Missing node processing complete map " << mn; "Missing node processing complete map " << mn;
Throw(); Rethrow();
} }
} }
@@ -693,7 +693,7 @@ void LedgerConsensusImp::timerEntry ()
leaveConsensus (); leaveConsensus ();
JLOG (j_.error()) << JLOG (j_.error()) <<
"Missing node during consensus process " << mn; "Missing node during consensus process " << mn;
Throw(); Rethrow();
} }
} }

View File

@@ -143,7 +143,7 @@ RippleCalc::Output RippleCalc::rippleCalculate (
{ {
JLOG (j.trace()) << "Exception from flow" << e.what (); JLOG (j.trace()) << "Exception from flow" << e.what ();
if (!useFlowV1Output) if (!useFlowV1Output)
Throw(); Rethrow();
} }
if (j.debug()) if (j.debug())
@@ -151,12 +151,12 @@ RippleCalc::Output RippleCalc::rippleCalculate (
auto logResult = [&] (std::string const& algoName, Output const& result) auto logResult = [&] (std::string const& algoName, Output const& result)
{ {
j.debug() << "RippleCalc Result> " << j.debug() << "RippleCalc Result> " <<
" actualIn: " << result.actualAmountIn << " actualIn: " << result.actualAmountIn <<
", actualOut: " << result.actualAmountOut << ", actualOut: " << result.actualAmountOut <<
", result: " << result.result () << ", result: " << result.result () <<
", dstAmtReq: " << saDstAmountReq << ", dstAmtReq: " << saDstAmountReq <<
", sendMax: " << saMaxAmountReq << ", sendMax: " << saMaxAmountReq <<
", algo: " << algoName; ", algo: " << algoName;
}; };
if (callFlowV1) if (callFlowV1)
{ {

View File

@@ -33,11 +33,22 @@ namespace ripple {
preconditions, postconditions, and invariants. preconditions, postconditions, and invariants.
*/ */
/** Generates and logs a call stack */
void
LogThrow (std::string const& title);
/** Rethrow the exception currently being handled.
When called from within a catch block, it will pass
control to the next matching exception handler, if any.
Otherwise, std::terminate will be called.
*/
[[noreturn]] [[noreturn]]
inline inline
void void
Throw () Rethrow ()
{ {
LogThrow ("Re-throwing exception");
throw; throw;
} }
@@ -49,7 +60,10 @@ Throw (Args&&... args)
{ {
static_assert (std::is_convertible<E*, std::exception*>::value, static_assert (std::is_convertible<E*, std::exception*>::value,
"Exception must derive from std::exception."); "Exception must derive from std::exception.");
throw E(std::forward<Args>(args)...);
E e(std::forward<Args>(args)...);
LogThrow (std::string("Throwing exception: ") + e.what());
throw e;
} }
/** Called when faulty logic causes a broken invariant. */ /** Called when faulty logic causes a broken invariant. */

View File

@@ -19,6 +19,7 @@
#include <BeastConfig.h> #include <BeastConfig.h>
#include <ripple/basics/contract.h> #include <ripple/basics/contract.h>
#include <ripple/basics/Log.h>
#include <cstdlib> #include <cstdlib>
#include <exception> #include <exception>
#include <iostream> #include <iostream>
@@ -39,10 +40,17 @@ accessViolation() noexcept
} // detail } // detail
void
LogThrow (std::string const& title)
{
JLOG(debugLog()) << title;
}
[[noreturn]] [[noreturn]]
void void
LogicError (std::string const& s) noexcept LogicError (std::string const& s) noexcept
{ {
JLOG(debugLog()) << s;
std::cerr << "Logic error: " << s << std::endl; std::cerr << "Logic error: " << s << std::endl;
detail::accessViolation(); detail::accessViolation();
} }

View File

@@ -39,7 +39,7 @@ public:
try try
{ {
Throw(); Rethrow();
} }
catch (std::runtime_error const& e) catch (std::runtime_error const& e)
{ {

View File

@@ -317,7 +317,7 @@ public:
#if NODESTORE_TIMING_DO_VERIFY #if NODESTORE_TIMING_DO_VERIFY
backend->verify(); backend->verify();
#endif #endif
Throw(); Rethrow();
} }
backend->close(); backend->close();
} }
@@ -378,7 +378,7 @@ public:
#if NODESTORE_TIMING_DO_VERIFY #if NODESTORE_TIMING_DO_VERIFY
backend->verify(); backend->verify();
#endif #endif
Throw(); Rethrow();
} }
backend->close(); backend->close();
} }
@@ -441,7 +441,7 @@ public:
#if NODESTORE_TIMING_DO_VERIFY #if NODESTORE_TIMING_DO_VERIFY
backend->verify(); backend->verify();
#endif #endif
Throw(); Rethrow();
} }
backend->close(); backend->close();
} }
@@ -519,7 +519,7 @@ public:
#if NODESTORE_TIMING_DO_VERIFY #if NODESTORE_TIMING_DO_VERIFY
backend->verify(); backend->verify();
#endif #endif
Throw(); Rethrow();
} }
backend->close(); backend->close();
} }
@@ -631,7 +631,7 @@ public:
#if NODESTORE_TIMING_DO_VERIFY #if NODESTORE_TIMING_DO_VERIFY
backend->verify(); backend->verify();
#endif #endif
Throw(); Rethrow();
} }
backend->close(); backend->close();
} }

View File

@@ -154,7 +154,7 @@ parse_Port (ParsedPort& port, Section const& section, std::ostream& log)
{ {
log << "Invalid value '" << result.first << log << "Invalid value '" << result.first <<
"' for key 'ip' in [" << section.name() << "]\n"; "' for key 'ip' in [" << section.name() << "]\n";
Throw(); Rethrow();
} }
} }
} }
@@ -177,7 +177,7 @@ parse_Port (ParsedPort& port, Section const& section, std::ostream& log)
log << log <<
"Invalid value '" << result.first << "' for key " << "Invalid value '" << result.first << "' for key " <<
"'port' in [" << section.name() << "]\n"; "'port' in [" << section.name() << "]\n";
Throw(); Rethrow();
} }
} }
} }
@@ -207,7 +207,7 @@ parse_Port (ParsedPort& port, Section const& section, std::ostream& log)
log << log <<
"Invalid value '" << lim << "' for key " << "Invalid value '" << lim << "' for key " <<
"'limit' in [" << section.name() << "]\n"; "'limit' in [" << section.name() << "]\n";
Throw(); Rethrow();
} }
} }
} }

View File

@@ -450,7 +450,7 @@ Env::autofill (JTx& jt)
test.log << test.log <<
"parse failed:\n" << "parse failed:\n" <<
pretty(jv); pretty(jv);
Throw(); Rethrow();
} }
} }
@@ -469,7 +469,7 @@ Env::st (JTx const& jt)
test.log << test.log <<
"Exception: parse_error\n" << "Exception: parse_error\n" <<
pretty(jt.jv); pretty(jt.jv);
Throw(); Rethrow();
} }
try try

View File

@@ -89,7 +89,7 @@ msig::operator()(Env& env, JTx& jt) const
catch(parse_error const&) catch(parse_error const&)
{ {
env.test.log << pretty(jt.jv); env.test.log << pretty(jt.jv);
Throw(); Rethrow();
} }
auto& js = jt[sfSigners.getJsonName()]; auto& js = jt[sfSigners.getJsonName()];
js.resize(mySigners.size()); js.resize(mySigners.size());