* Some refactoring of the exception handling code so that we can catch

Nix expression assertion failures.
This commit is contained in:
Eelco Dolstra 2006-03-08 14:11:19 +00:00
parent fa72ae1e9c
commit 9088dee9e2
5 changed files with 45 additions and 22 deletions

View file

@ -223,8 +223,9 @@ Expr evalExpr2(EvalState & state, Expr e)
try { try {
return evalExpr(state, substArgs(e4, formals, e2)); return evalExpr(state, substArgs(e4, formals, e2));
} catch (Error & e) { } catch (Error & e) {
throw Error(format("while evaluating the function at %1%:\n%2%") e.addPrefix(format("while evaluating the function at %1%:\n")
% showPos(pos) % e.msg()); % showPos(pos));
throw;
} }
} }
@ -234,8 +235,9 @@ Expr evalExpr2(EvalState & state, Expr e)
subs.set(name, e2); subs.set(name, e2);
return evalExpr(state, substitute(subs, e4)); return evalExpr(state, substitute(subs, e4));
} catch (Error & e) { } catch (Error & e) {
throw Error(format("while evaluating the function at %1%:\n%2%") e.addPrefix(format("while evaluating the function at %1%:\n")
% showPos(pos) % e.msg()); % showPos(pos));
throw;
} }
} }
@ -251,8 +253,9 @@ Expr evalExpr2(EvalState & state, Expr e)
try { try {
return evalExpr(state, a); return evalExpr(state, a);
} catch (Error & e) { } catch (Error & e) {
throw Error(format("while evaluating the attribute `%1%' at %2%:\n%3%") e.addPrefix(format("while evaluating the attribute `%1%' at %2%:\n")
% s1 % showPos(pos) % e.msg()); % s1 % showPos(pos));
throw;
} }
} }
@ -272,7 +275,7 @@ Expr evalExpr2(EvalState & state, Expr e)
/* Assertions. */ /* Assertions. */
if (matchAssert(e, e1, e2, pos)) { if (matchAssert(e, e1, e2, pos)) {
if (!evalBool(state, e1)) if (!evalBool(state, e1))
throw Error(format("assertion failed at %1%") % showPos(pos)); throw AssertionError(format("assertion failed at %1%") % showPos(pos));
return evalExpr(state, e2); return evalExpr(state, e2);
} }
@ -283,16 +286,18 @@ Expr evalExpr2(EvalState & state, Expr e)
e1 = evalExpr(state, e1); e1 = evalExpr(state, e1);
queryAllAttrs(e1, attrs); queryAllAttrs(e1, attrs);
} catch (Error & e) { } catch (Error & e) {
throw Error(format("while evaluating the `with' definitions at %1%:\n%2%") e.addPrefix(format("while evaluating the `with' definitions at %1%:\n")
% showPos(pos) % e.msg()); % showPos(pos));
throw;
} }
try { try {
e2 = substitute(attrs, e2); e2 = substitute(attrs, e2);
checkVarDefs(state.primOps, e2); checkVarDefs(state.primOps, e2);
return evalExpr(state, e2); return evalExpr(state, e2);
} catch (Error & e) { } catch (Error & e) {
throw Error(format("while evaluating the `with' body at %1%:\n%2%") e.addPrefix(format("while evaluating the `with' body at %1%:\n")
% showPos(pos) % e.msg()); % showPos(pos));
throw;
} }
} }
@ -391,8 +396,9 @@ Expr evalFile(EvalState & state, const Path & path)
try { try {
return evalExpr(state, e); return evalExpr(state, e);
} catch (Error & e) { } catch (Error & e) {
throw Error(format("while evaluating the file `%1%':\n%2%") e.addPrefix(format("while evaluating the file `%1%':\n")
% path % e.msg()); % path);
throw;
} }
} }

View file

@ -37,6 +37,10 @@ struct EvalState
}; };
MakeError(EvalError, Error)
MakeError(AssertionError, EvalError)
/* Evaluate an expression to normal form. */ /* Evaluate an expression to normal form. */
Expr evalExpr(EvalState & state, Expr e); Expr evalExpr(EvalState & state, Expr e);

View file

@ -244,8 +244,9 @@ static Expr primDerivationStrict(EvalState & state, const ATermVector & args)
try { try {
processBinding(state, value, drv, ss); processBinding(state, value, drv, ss);
} catch (Error & e) { } catch (Error & e) {
throw Error(format("while processing the derivation attribute `%1%' at %2%:\n%3%") e.addPrefix(format("while processing the derivation attribute `%1%' at %2%:\n")
% key % showPos(pos) % e.msg()); % key % showPos(pos));
throw;
} }
/* The `args' attribute is special: it supplies the /* The `args' attribute is special: it supplies the
@ -547,8 +548,9 @@ static Expr primDependencyClosure(EvalState & state, const ATermVector & args)
} }
} catch (Error & e) { } catch (Error & e) {
throw Error(format("while finding dependencies in `%1%':\n%2%") e.addPrefix(format("while finding dependencies in `%1%':\n")
% path % e.msg()); % path);
throw;
} }
} }

View file

@ -25,6 +25,13 @@ Error::Error(const format & f)
} }
Error & Error::addPrefix(const format & f)
{
err = f.str() + err;
return *this;
}
SysError::SysError(const format & f) SysError::SysError(const format & f)
: Error(format("%1%: %2%") % f.str() % strerror(errno)) : Error(format("%1%: %2%") % f.str() % strerror(errno))
{ {

View file

@ -26,6 +26,7 @@ public:
~Error() throw () { }; ~Error() throw () { };
const char * what() const throw () { return err.c_str(); } const char * what() const throw () { return err.c_str(); }
const string & msg() const throw () { return err; } const string & msg() const throw () { return err; }
Error & addPrefix(const format & f);
}; };
class SysError : public Error class SysError : public Error
@ -34,12 +35,15 @@ public:
SysError(const format & f); SysError(const format & f);
}; };
class UsageError : public Error #define MakeError(newClass, superClass) \
{ class newClass : public superClass \
public: { \
UsageError(const format & f) : Error(f) { }; public: \
newClass(const format & f) : superClass(f) { }; \
}; };
MakeError(UsageError, Error)
typedef list<string> Strings; typedef list<string> Strings;
typedef set<string> StringSet; typedef set<string> StringSet;