From 914c05033fc43b772a2a290b070887f549280f64 Mon Sep 17 00:00:00 2001
From: florian <f.spreckelsen@inidscale.com>
Date: Wed, 7 Jul 2021 16:51:52 +0200
Subject: [PATCH] ENH: Add basic exception handling

---
 jlcaosdb/jlcaosdb.cxx | 10 ++++++++++
 src/CaosDB.jl         | 13 +++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/jlcaosdb/jlcaosdb.cxx b/jlcaosdb/jlcaosdb.cxx
index 37543e1..96e7b58 100644
--- a/jlcaosdb/jlcaosdb.cxx
+++ b/jlcaosdb/jlcaosdb.cxx
@@ -20,6 +20,8 @@
  * <https://www.gnu.org/licenses/>.
  *
  */
+#include <string>
+
 #include <jlcxx/jlcxx.hpp>
 
 #include "caosdb/authentication.h"
@@ -33,4 +35,12 @@ JLCXX_MODULE define_julia_module(jlcxx::Module& mod)
   mod.method("get_libcaosdb_version_major", []() {return caosdb::LIBCAOSDB_VERSION_MAJOR;});
   mod.method("get_libcaosdb_version_minor", []() {return caosdb::LIBCAOSDB_VERSION_MINOR;});
   mod.method("get_libcaosdb_version_patch", []() {return caosdb::LIBCAOSDB_VERSION_PATCH;});
+
+  // exceptions inherit from abstract Julia exception types:
+  mod.add_type<caosdb::exceptions::AuthenticationError>("AuthenticationError", jlcxx::julia_type("AbstractAuthenticationError"))
+    .constructor<const std::string&>()
+    .method("what", &caosdb::exceptions::AuthenticationError::what);
+  mod.add_type<caosdb::exceptions::ConnectionError>("ConnectionError", jlcxx::julia_type("AbstractConnectionError"))
+    .constructor<const std::string&>()
+    .method("what", &caosdb::exceptions::ConnectionError::what);
 }
diff --git a/src/CaosDB.jl b/src/CaosDB.jl
index 6b725fa..17d475a 100644
--- a/src/CaosDB.jl
+++ b/src/CaosDB.jl
@@ -5,6 +5,15 @@ module CaosDB
 
 using CxxWrap
 
+"Generic super type of CaosDB exceptions"
+abstract type AbstractCaosDBException <: Exception end
+
+"An error occurred while trying to authenticate"
+abstract type AbstractAuthenticationError <: AbstractCaosDBException end
+
+"Connection couldn't be established"
+abstract type AbstractConnectionError <: AbstractCaosDBException end
+
 # Adapt this path if you haven't compiled jlcaosdb to `caosdb-julialib/build`
 @wrapmodule(joinpath(@__DIR__, "../jlcaosdb/build/lib/libjlcaosdb"))
 
@@ -26,4 +35,8 @@ function get_libcaosdb_version()
     return "$major.$minor.$patch"
 end
 
+# Override the exception printing
+Base.showerror(io::IO, e::AuthenticationError) = print(io, what(e))
+Base.showerror(io::IO, e::ConnectionError) = print(io, what(e))
+
 end # module
-- 
GitLab