]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/except.cpp
Move all exception classes to a common header
[libs/datafile.git] / source / except.cpp
diff --git a/source/except.cpp b/source/except.cpp
new file mode 100644 (file)
index 0000000..eea692e
--- /dev/null
@@ -0,0 +1,52 @@
+#include <typeinfo>
+#include <msp/debug/demangle.h>
+#include <msp/strings/format.h>
+#include "except.h"
+
+using namespace std;
+
+namespace Msp {
+namespace DataFile {
+
+data_error::data_error(const string &s, unsigned l, const string &w):
+       runtime_error(make_what(s, l, w)),
+       source(s),
+       line(l)
+{ }
+
+data_error::data_error(const string &s, unsigned l, const exception &e):
+       runtime_error(make_what(s, l, format("%s (%s)", Debug::demangle(typeid(e).name()), e.what()))),
+       source(s),
+       line(l)
+{ }
+
+string data_error::make_what(const string &s, unsigned l, const string &w)
+{
+       if(l)
+               return format("%s:%d: %s", s, l, w);
+       else
+               return format("%s: %s", s, w);
+}
+
+
+parse_error::parse_error(const string &t):
+       runtime_error(t.empty() ? "at end of input" : format("after '%s'", t))
+{ }
+
+
+syntax_error::syntax_error(const string &t):
+       runtime_error(t.empty() ? "at end of input" : format("at '%s'", t))
+{ }
+
+
+bad_definition::bad_definition(const string &w):
+       runtime_error(w)
+{ }
+
+
+nesting_error::nesting_error(const string &w):
+       logic_error(w)
+{ }
+
+} // namespace DataFile
+} // namespace Msp