From 8c05bf539e9279b9b67a21e4a6015c42c7f2bb29 Mon Sep 17 00:00:00 2001
From: florian <f.spreckelsen@inidscale.com>
Date: Fri, 30 Jul 2021 10:45:52 +0200
Subject: [PATCH] ENH: Add logging messages for return codes <0

---
 src/CaosDB.jl     | 25 +++++++++++++++++++++----
 src/Connection.jl | 12 ++++++------
 test/runtests.jl  | 22 ++++++++++++++++++++++
 3 files changed, 49 insertions(+), 10 deletions(-)

diff --git a/src/CaosDB.jl b/src/CaosDB.jl
index 2ff03b8..e065ee2 100644
--- a/src/CaosDB.jl
+++ b/src/CaosDB.jl
@@ -31,8 +31,9 @@ library_name = (@static Sys.iswindows() ? "ccaosdb" : "libccaosdb")
 
 module Exceptions
 
-export evaluate_return_code, CaosDBException, GenericCaosDBException
+export evaluate_return_code, CaosDBException, GenericCaosDBException, CaosDBMessage
 
+using Logging
 using CaosDB
 
 """
@@ -50,7 +51,19 @@ struct GenericCaosDBException <: CaosDBException
     code::Cint
 end
 
-Base.showerror(io::IO, e::CaosDBException) = print(io, "CaosDBException: ", e.msg)
+Base.showerror(io::IO, e::CaosDBException) =
+    print(io, "CaosDBException: ", e.msg, " (Status code ", e.code, ")")
+
+"""
+Struct containing Messages and codes for status codes<0 that do not
+correspond to errors or success.
+"""
+struct CaosDBMessage
+    msg::String
+    code::Cint
+end
+
+Base.show(io::IO, m::CaosDBMessage) = print(io, m.msg, " (Status code ", m.code, ")")
 
 """
     function evaluate_return_code(code::Cint)
@@ -66,7 +79,11 @@ function evaluate_return_code(code::Cint)
             (Cint,),
             code,
         )
-        throw(GenericCaosDBException(unsafe_string(msg), code))
+        if code > 0
+            throw(GenericCaosDBException(unsafe_string(msg), code))
+        else
+            @info CaosDBMessage(unsafe_string(msg), code)
+        end
     end
 end
 
@@ -180,7 +197,7 @@ function create_plain_password_authenticator(
         password,
     )
 
-    CaosDB.Excepions.evaluate_return_code(err_code)
+    CaosDB.Exceptions.evaluate_return_code(err_code)
 
     return auth
 
diff --git a/src/Connection.jl b/src/Connection.jl
index ec26b66..0f8e140 100644
--- a/src/Connection.jl
+++ b/src/Connection.jl
@@ -134,7 +134,7 @@ function create_pem_file_certificate_provider(path::AbstractString)
         path,
     )
 
-    CaosDB.Excepions.evaluate_return_code(err_code)
+    CaosDB.Exceptions.evaluate_return_code(err_code)
 
     return cert_provider
 
@@ -176,7 +176,7 @@ function create_tls_connection_configuration(
         provider,
     )
 
-    CaosDB.Excepions.evaluate_return_code(err_code)
+    CaosDB.Exceptions.evaluate_return_code(err_code)
 
     return config
 
@@ -195,7 +195,7 @@ function create_insecure_connection_configuration(host::AbstractString, port::Ci
         port,
     )
 
-    CaosDB.Excepions.evaluate_return_code(err_code)
+    CaosDB.Exceptions.evaluate_return_code(err_code)
 
     return config
 end
@@ -217,7 +217,7 @@ function create_connection(config::Ref{_Configuration})
         config,
     )
 
-    CaosDB.Excepions.evaluate_return_code(err_code)
+    CaosDB.Exceptions.evaluate_return_code(err_code)
 
     return connection
 
@@ -248,7 +248,7 @@ function get_connection(name::AbstractString = "default")
         )
     end
 
-    CaosDB.Excepions.evaluate_return_code(err_code)
+    CaosDB.Exceptions.evaluate_return_code(err_code)
 
     return connection
 
@@ -273,7 +273,7 @@ function get_version_info(con::Ref{_Connection})
         con,
     )
 
-    CaosDB.Excepions.evaluate_return_code(err_code)
+    CaosDB.Exceptions.evaluate_return_code(err_code)
 
     return info
 
diff --git a/test/runtests.jl b/test/runtests.jl
index 48b7826..5fa1db1 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -34,12 +34,34 @@ using CaosDB
     end
 
     @testset "TestExceptions" begin
+        # In case of success, nothing is done
         @test CaosDB.Exceptions.evaluate_return_code(Cint(0)) == nothing
+
+        # CaosDBExceptions are thrown for return codes > 0
         @test_throws CaosDB.Exceptions.CaosDBException CaosDB.Exceptions.evaluate_return_code(
             Cint(14),
         )
         @test_throws CaosDB.Exceptions.GenericCaosDBException CaosDB.Exceptions.evaluate_return_code(
             Cint(14),
         )
+        try
+            CaosDB.Exceptions.evaluate_return_code(Cint(14))
+            # fail if this doesn't throw an error
+            @test false
+        catch e
+            @test isa(e, CaosDB.Exceptions.GenericCaosDBException)
+            @test e.code == 14
+        end
+
+        # Return codes < 0 correspond to unfinished transactions and
+        # we expect messages to be returned.
+        @test CaosDB.Exceptions.evaluate_return_code(Cint(-1)) == nothing
+        @test_logs (
+            :info,
+            CaosDB.Exceptions.CaosDBMessage(
+                "The transaction is currently being executed.",
+                Cint(-1),
+            ),
+        ) CaosDB.Exceptions.evaluate_return_code(Cint(-1))
     end
 end
-- 
GitLab