From: Gunnar Beutner Date: Tue, 10 Nov 2015 16:04:49 +0000 (+0100) Subject: Improve location information for exceptions X-Git-Tag: v2.4.0~18 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2d2e7ecdb218b87c62d95373d026db48ceabcc96;p=icinga2 Improve location information for exceptions fixes #10568 --- diff --git a/lib/base/debuginfo.cpp b/lib/base/debuginfo.cpp index 777daeb20..c927e0b97 100644 --- a/lib/base/debuginfo.cpp +++ b/lib/base/debuginfo.cpp @@ -57,11 +57,13 @@ DebugInfo icinga::DebugInfoRange(const DebugInfo& start, const DebugInfo& end) #define EXTRA_LINES 2 -void icinga::ShowCodeFragment(std::ostream& out, const DebugInfo& di, bool verbose) +void icinga::ShowCodeLocation(std::ostream& out, const DebugInfo& di, bool verbose) { if (di.Path.IsEmpty()) return; + out << "Location: " << di << "\n"; + std::ifstream ifs; ifs.open(di.Path.CStr(), std::ifstream::in); diff --git a/lib/base/debuginfo.hpp b/lib/base/debuginfo.hpp index 132435a25..3181255c8 100644 --- a/lib/base/debuginfo.hpp +++ b/lib/base/debuginfo.hpp @@ -48,7 +48,7 @@ I2_BASE_API std::ostream& operator<<(std::ostream& out, const DebugInfo& val); I2_BASE_API DebugInfo DebugInfoRange(const DebugInfo& start, const DebugInfo& end); -I2_BASE_API void ShowCodeFragment(std::ostream& out, const DebugInfo& di, bool verbose = true); +I2_BASE_API void ShowCodeLocation(std::ostream& out, const DebugInfo& di, bool verbose = true); } diff --git a/lib/base/exception.cpp b/lib/base/exception.cpp index 4e0693873..c9940ef03 100644 --- a/lib/base/exception.cpp +++ b/lib/base/exception.cpp @@ -140,16 +140,14 @@ String icinga::DiagnosticInformation(const std::exception& ex, bool verbose, Sta const ValidationError *vex = dynamic_cast(&ex); if (message.IsEmpty()) - result << boost::diagnostic_information(ex); + result << boost::diagnostic_information(ex) << "\n"; else - result << "Error: " << message; + result << "Error: " << message << "\n"; const ScriptError *dex = dynamic_cast(&ex); - if (dex && !dex->GetDebugInfo().Path.IsEmpty()) { - result << "\nLocation:\n"; - ShowCodeFragment(result, dex->GetDebugInfo()); - } + if (dex && !dex->GetDebugInfo().Path.IsEmpty()) + ShowCodeLocation(result, dex->GetDebugInfo()); if (vex) { DebugInfo di; @@ -187,10 +185,8 @@ String icinga::DiagnosticInformation(const std::exception& ex, bool verbose, Sta di.LastColumn = message->Get(5); } - if (!di.Path.IsEmpty()) { - result << "\nLocation:\n"; - ShowCodeFragment(result, di); - } + if (!di.Path.IsEmpty()) + ShowCodeLocation(result, di); } const user_error *uex = dynamic_cast(&ex); diff --git a/lib/cli/consolecommand.cpp b/lib/cli/consolecommand.cpp index 8afe26016..f801ef59d 100644 --- a/lib/cli/consolecommand.cpp +++ b/lib/cli/consolecommand.cpp @@ -59,12 +59,13 @@ void ConsoleCommand::BreakpointHandler(ScriptFrame& frame, ScriptError *ex, cons if (ex && ex->IsHandledByDebugger()) return; - std::cout << "Breakpoint encountered " << di << "\n"; + std::cout << "Breakpoint encountered.\n"; if (ex) { - std::cout << "Exception: " << DiagnosticInformation(*ex); + std::cout << "Exception: " << DiagnosticInformation(*ex) << "\n"; ex->SetHandledByDebugger(true); - } + } else + ShowCodeLocation(std::cout, di); std::cout << "You can inspect expressions (such as variables) by entering them at the prompt.\n" << "To leave the debugger and continue the program use \"$continue\".\n"; @@ -358,7 +359,7 @@ incomplete: } } } else { - ShowCodeFragment(std::cout, di); + ShowCodeLocation(std::cout, di); } std::cout << ex.what() << "\n"; diff --git a/lib/config/expression.cpp b/lib/config/expression.cpp index 0aa9a484f..b1ecaf1f5 100644 --- a/lib/config/expression.cpp +++ b/lib/config/expression.cpp @@ -56,7 +56,7 @@ ExpressionResult Expression::Evaluate(ScriptFrame& frame, DebugHint *dhint) cons try { #ifdef I2_DEBUG /* std::ostringstream msgbuf; - ShowCodeFragment(msgbuf, GetDebugInfo(), false); + ShowCodeLocation(msgbuf, GetDebugInfo(), false); Log(LogDebug, "Expression") << "Executing:\n" << msgbuf.str();*/ #endif /* I2_DEBUG */