about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/aterm.cc13
-rw-r--r--src/libutil/aterm.hh5
2 files changed, 18 insertions, 0 deletions
diff --git a/src/libutil/aterm.cc b/src/libutil/aterm.cc
index de7c359521a7..dc6abf9e70e8 100644
--- a/src/libutil/aterm.cc
+++ b/src/libutil/aterm.cc
@@ -91,3 +91,16 @@ ATMatcher & operator >> (ATMatcher & pos, ATermList & out)
     out = (ATermList) t;
     return pos;
 }
+
+
+Error badTerm(const format & f, ATerm t)
+{
+    char * s = ATwriteToString(t);
+    if (!s) throw Error("cannot print term");
+    if (strlen(s) > 1000) {
+        int len;
+        s = ATwriteToSharedString(t, &len);
+        if (!s) throw Error("cannot print term");
+    }
+    return Error(format("%1%, in `%2%'") % f.str() % (string) s);
+}
diff --git a/src/libutil/aterm.hh b/src/libutil/aterm.hh
index 16d8d6bb6dfd..d38d8e3f4d7a 100644
--- a/src/libutil/aterm.hh
+++ b/src/libutil/aterm.hh
@@ -74,4 +74,9 @@ ATMatcher & operator >> (ATMatcher & pos, const string & s);
 ATMatcher & operator >> (ATMatcher & pos, ATermList & out);
 
 
+/* Throw an exception with an error message containing the given
+   aterm. */
+Error badTerm(const format & f, ATerm t);
+
+
 #endif /* !__ATERM_H */