From 80ed8eb505ce8448b25cea64b4f8c0121482e403 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 21 Jun 2009 20:07:11 +0000 Subject: [PATCH] Make EvalError a subclass of runtime_error and provide the std::exception with a robust description for easier debugging of callback/scripted function calls --- chaiscript/chaiscript_engine.hpp | 6 +++--- chaiscript/chaiscript_eval.hpp | 9 +++++++-- samples/callbacks.chai | 1 + 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/chaiscript/chaiscript_engine.hpp b/chaiscript/chaiscript_engine.hpp index fbbb24a..fc2fefb 100644 --- a/chaiscript/chaiscript_engine.hpp +++ b/chaiscript/chaiscript_engine.hpp @@ -29,12 +29,12 @@ namespace chaiscript try { val = dispatchkit::Cast_Helper()(vals[0]); } - catch (std::exception &e) { - throw EvalError("Can not evaluate string: " + val, langkit::TokenPtr()); - } catch (EvalError &ee) { throw EvalError("Can not evaluate string: " + val + " reason: " + ee.reason, langkit::TokenPtr()); } + catch (std::exception &e) { + throw EvalError("Can not evaluate string: " + val, langkit::TokenPtr()); + } return evaluate_string(val); } diff --git a/chaiscript/chaiscript_eval.hpp b/chaiscript/chaiscript_eval.hpp index 4021b82..fcc1a44 100644 --- a/chaiscript/chaiscript_eval.hpp +++ b/chaiscript/chaiscript_eval.hpp @@ -15,11 +15,16 @@ namespace chaiscript ParserError(const std::string &why, const langkit::TokenPtr where) : reason(why), location(where){ } }; - struct EvalError { + struct EvalError : public std::runtime_error { std::string reason; langkit::TokenPtr location; - EvalError(const std::string &why, const langkit::TokenPtr where) : reason(why), location(where) { } + EvalError(const std::string &why, const langkit::TokenPtr where) + : std::runtime_error("Eval error: \"" + why + "\" in '" + + where->filename + "' line: " + boost::lexical_cast(where->start.line+1)), + reason(why), location(where) { } + + virtual ~EvalError() throw() {} }; struct ReturnValue { diff --git a/samples/callbacks.chai b/samples/callbacks.chai index de16629..0d5394c 100644 --- a/samples/callbacks.chai +++ b/samples/callbacks.chai @@ -4,4 +4,5 @@ cb_handler.add_callbacks( function() { "DivOnePtTwo"; }, function (x) { x / 1.2; +cb_handler.add_callbacks( function() { "DivOnePtTwo"; }, function (x) { blargh; } );