]> granicus.if.org Git - icinga2/commitdiff
Improve location information for exceptions
authorGunnar Beutner <gunnar@beutner.name>
Tue, 10 Nov 2015 16:04:49 +0000 (17:04 +0100)
committerGunnar Beutner <gunnar@beutner.name>
Tue, 10 Nov 2015 16:05:40 +0000 (17:05 +0100)
fixes #10568

lib/base/debuginfo.cpp
lib/base/debuginfo.hpp
lib/base/exception.cpp
lib/cli/consolecommand.cpp
lib/config/expression.cpp

index 777daeb20e703a38525db83ff505fefe52456f53..c927e0b970db2a50c0033026f9a01cb3d19e0ebe 100644 (file)
@@ -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);
 
index 132435a25675cb6c4336e088c5cff18343f9178b..3181255c88d0f4c74f607f17520a9f0a612444e0 100644 (file)
@@ -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);
 
 }
 
index 4e0693873d97f194fb45fcca67fdfc57518b63f7..c9940ef0383f5a301bd75492b9113cc072a5ae3d 100644 (file)
@@ -140,16 +140,14 @@ String icinga::DiagnosticInformation(const std::exception& ex, bool verbose, Sta
        const ValidationError *vex = dynamic_cast<const ValidationError *>(&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<const ScriptError *>(&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<const user_error *>(&ex);
index 8afe26016b808185861f142e9c5c788ee55702eb..f801ef59d48c48c9546b459f0b7aaefa8ccfcf64 100644 (file)
@@ -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";
index 0aa9a484fc03daa04bbe6b2b6e51194a4d0a297d..b1ecaf1f5f6f9a5a1937ddc95cca79b1ece04ed3 100644 (file)
@@ -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 */