about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2006-03-08T14·11+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2006-03-08T14·11+0000
commit9088dee9e265db8176b61e53ac43a916fdd34a3d (patch)
tree42abd16be1b9bc4e4b5b4defdcd44b5bd9972740 /src
parentfa72ae1e9cad0dded28b965fe97293bbeeac031e (diff)
* Some refactoring of the exception handling code so that we can catch
  Nix expression assertion failures.

Diffstat (limited to 'src')
-rw-r--r--src/libexpr/eval.cc32
-rw-r--r--src/libexpr/eval.hh4
-rw-r--r--src/libexpr/primops.cc10
-rw-r--r--src/libutil/util.cc7
-rw-r--r--src/libutil/util.hh14
5 files changed, 45 insertions, 22 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index 9a6d414db706..045e5f632ff6 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -223,8 +223,9 @@ Expr evalExpr2(EvalState & state, Expr e)
             try {
                 return evalExpr(state, substArgs(e4, formals, e2));
             } catch (Error & e) {
-                throw Error(format("while evaluating the function at %1%:\n%2%")
-                    % showPos(pos) % e.msg());
+                e.addPrefix(format("while evaluating the function at %1%:\n")
+                    % showPos(pos));
+                throw;
             }
         }
         
@@ -234,8 +235,9 @@ Expr evalExpr2(EvalState & state, Expr e)
                 subs.set(name, e2);
                 return evalExpr(state, substitute(subs, e4));
             } catch (Error & e) {
-                throw Error(format("while evaluating the function at %1%:\n%2%")
-                    % showPos(pos) % e.msg());
+                e.addPrefix(format("while evaluating the function at %1%:\n")
+                    % showPos(pos));
+                throw;
             }
         }
         
@@ -251,8 +253,9 @@ Expr evalExpr2(EvalState & state, Expr e)
         try {
             return evalExpr(state, a);
         } catch (Error & e) {
-            throw Error(format("while evaluating the attribute `%1%' at %2%:\n%3%")
-                % s1 % showPos(pos) % e.msg());
+            e.addPrefix(format("while evaluating the attribute `%1%' at %2%:\n")
+                % s1 % showPos(pos));
+            throw;
         }
     }
 
@@ -272,7 +275,7 @@ Expr evalExpr2(EvalState & state, Expr e)
     /* Assertions. */
     if (matchAssert(e, e1, e2, pos)) {
         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);
     }
 
@@ -283,16 +286,18 @@ Expr evalExpr2(EvalState & state, Expr e)
             e1 = evalExpr(state, e1);
             queryAllAttrs(e1, attrs);
         } catch (Error & e) {
-            throw Error(format("while evaluating the `with' definitions at %1%:\n%2%")
-                % showPos(pos) % e.msg());
+            e.addPrefix(format("while evaluating the `with' definitions at %1%:\n")
+                % showPos(pos));
+            throw;
         }
         try {
             e2 = substitute(attrs, e2);
             checkVarDefs(state.primOps, e2);
             return evalExpr(state, e2);
         } catch (Error & e) {
-            throw Error(format("while evaluating the `with' body at %1%:\n%2%")
-                % showPos(pos) % e.msg());
+            e.addPrefix(format("while evaluating the `with' body at %1%:\n")
+                % showPos(pos));
+            throw;
         } 
     }
 
@@ -391,8 +396,9 @@ Expr evalFile(EvalState & state, const Path & path)
     try {
         return evalExpr(state, e);
     } catch (Error & e) {
-        throw Error(format("while evaluating the file `%1%':\n%2%")
-            % path % e.msg());
+        e.addPrefix(format("while evaluating the file `%1%':\n")
+            % path);
+        throw;
     }
 }
 
diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh
index 5562066fda97..11185159c123 100644
--- a/src/libexpr/eval.hh
+++ b/src/libexpr/eval.hh
@@ -37,6 +37,10 @@ struct EvalState
 };
 
 
+MakeError(EvalError, Error)
+MakeError(AssertionError, EvalError)
+
+
 /* Evaluate an expression to normal form. */
 Expr evalExpr(EvalState & state, Expr e);
 
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index 310bca5ae97f..31fff0b493e8 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -244,8 +244,9 @@ static Expr primDerivationStrict(EvalState & state, const ATermVector & args)
         try {
             processBinding(state, value, drv, ss);
         } catch (Error & e) {
-            throw Error(format("while processing the derivation attribute `%1%' at %2%:\n%3%")
-                % key % showPos(pos) % e.msg());
+            e.addPrefix(format("while processing the derivation attribute `%1%' at %2%:\n")
+                % key % showPos(pos));
+            throw;
         }
 
         /* The `args' attribute is special: it supplies the
@@ -547,8 +548,9 @@ static Expr primDependencyClosure(EvalState & state, const ATermVector & args)
             }
 
         } catch (Error & e) {
-            throw Error(format("while finding dependencies in `%1%':\n%2%")
-                % path % e.msg());
+            e.addPrefix(format("while finding dependencies in `%1%':\n")
+                % path);
+            throw;
         }
     }
 
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index ee34cb18a59e..0d970e69e303 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -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)
     : Error(format("%1%: %2%") % f.str() % strerror(errno))
 {
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index aac2acd1730b..05cf777f1eb6 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -26,6 +26,7 @@ public:
     ~Error() throw () { };
     const char * what() const throw () { return err.c_str(); }
     const string & msg() const throw () { return err; }
+    Error & addPrefix(const format & f);
 };
 
 class SysError : public Error
@@ -34,11 +35,14 @@ public:
     SysError(const format & f);
 };
 
-class UsageError : public Error
-{
-public:
-    UsageError(const format & f) : Error(f) { };
-};
+#define MakeError(newClass, superClass) \
+    class newClass : public superClass                  \
+    {                                                   \
+    public:                                             \
+        newClass(const format & f) : superClass(f) { }; \
+    };
+
+MakeError(UsageError, Error)
 
 
 typedef list<string> Strings;