Skip to content

Commit 1d44b5e

Browse files
MichaelPFreykintel
authored andcommittedMar 22, 2018
writing the filename where the assertion failed (openscad#2341)
* writing the filename where the assertion failed * use just the filename and update the tests * relative path * move the path handling to the exception handler * update tests * return fs::path to prevent accidental writes (openscad#6) * using PRINTB instead of stringstream
1 parent 3e777f0 commit 1d44b5e

10 files changed

+21
-10
lines changed
 

‎src/AST.h

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Location {
1717
}
1818

1919
std::string fileName() const { return path ? path->generic_string() : ""; }
20+
fs::path filePath() const { return *path; }
2021
int firstLine() const { return first_line; }
2122
int firstColumn() const { return first_col; }
2223
int lastLine() const { return last_line; }

‎src/FileModule.cc

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
#include "modcontext.h"
3333
#include "parsersettings.h"
3434
#include "StatCache.h"
35-
35+
#include "evalcontext.h"
3636
#include <boost/algorithm/string.hpp>
3737
#include <boost/filesystem.hpp>
38+
#include "boost-utils.h"
3839
namespace fs = boost::filesystem;
3940
#include "FontCache.h"
4041
#include <sys/stat.h>
@@ -180,6 +181,12 @@ AbstractNode *FileModule::instantiateWithFileContext(FileContext *ctx, const Mod
180181
auto instantiatednodes = this->scope.instantiateChildren(ctx);
181182
node->children.insert(node->children.end(), instantiatednodes.begin(), instantiatednodes.end());
182183
}
184+
catch (AssertionFailedException &e) {
185+
auto docPath = boost::filesystem::path(ctx->documentPath());
186+
auto uncPath = boostfs_uncomplete(e.loc.filePath(), docPath);
187+
188+
PRINTB("%s failed in file %s, line %d", e.what() % uncPath.generic_string() % e.loc.firstLine());
189+
}
183190
catch (EvaluationException &e) {
184191
PRINT(e.what());
185192
}

‎src/exceptions.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <stdexcept>
44
#include <sstream>
5+
#include "expression.h"
56

67
class EvaluationException : public std::runtime_error {
78
public:
@@ -11,8 +12,11 @@ class EvaluationException : public std::runtime_error {
1112

1213
class AssertionFailedException : public EvaluationException {
1314
public:
14-
AssertionFailedException(const std::string &what_arg) : EvaluationException(what_arg) {}
15+
AssertionFailedException(const std::string &what_arg, const Location &loc) : EvaluationException(what_arg), loc(loc) {}
1516
~AssertionFailedException() throw() {}
17+
18+
public:
19+
Location loc;
1620
};
1721

1822
class RecursionException: public EvaluationException {

‎src/expr.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -728,11 +728,10 @@ void evaluate_assert(const Context &context, const class EvalContext *evalctx, c
728728
msg << "ERROR: Assertion";
729729
const Expression *expr = assignments["condition"];
730730
if (expr) msg << " '" << *expr << "'";
731-
msg << " failed, line " << loc.firstLine();
732731
const ValuePtr message = c.lookup_variable("message", true);
733732
if (message->isDefined()) {
734733
msg << ": " << message->toEchoString();
735734
}
736-
throw AssertionFailedException(msg.str());
735+
throw AssertionFailedException(msg.str(),loc);
737736
}
738737
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ERROR: Assertion 'false' failed, line 1
1+
ERROR: Assertion 'false' failed in file ../../../../../testdata/scad/functions/assert-expression-fail1-test.scad, line 1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ERROR: Assertion '((a < 20) && (b < 20))' failed, line 3: "Test! <html>&</html>"
1+
ERROR: Assertion '((a < 20) && (b < 20))': "Test! <html>&</html>" failed in file ../../../../../testdata/scad/functions/assert-expression-fail2-test.scad, line 3
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ERROR: Assertion '(f(angle) > 0)' failed, line 4
1+
ERROR: Assertion '(f(angle) > 0)' failed in file ../../../../../testdata/scad/functions/assert-expression-fail3-test.scad, line 4
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ERROR: Assertion 'false' failed, line 1
1+
ERROR: Assertion 'false' failed in file ../../../../../testdata/scad/misc/assert-fail1-test.scad, line 1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ERROR: Assertion '((a < 20) && (b < 20))' failed, line 3: "Test! <html>&</html>"
1+
ERROR: Assertion '((a < 20) && (b < 20))': "Test! <html>&</html>" failed in file ../../../../../testdata/scad/misc/assert-fail2-test.scad, line 3
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ERROR: Assertion '(f(angle) > 0)' failed, line 4
1+
ERROR: Assertion '(f(angle) > 0)' failed in file ../../../../../testdata/scad/misc/assert-fail3-test.scad, line 4

0 commit comments

Comments
 (0)
Please sign in to comment.