neo4j: Add a class for errors that are specific to Neo4j.
authorJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Wed, 12 Nov 2014 19:42:57 +0000 (14:42 -0500)
committerJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Fri, 14 Nov 2014 16:30:48 +0000 (11:30 -0500)
Signed-off-by: Jean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>

lib/neo4j/error.nit [new file with mode: 0644]
lib/neo4j/neo4j.nit

diff --git a/lib/neo4j/error.nit b/lib/neo4j/error.nit
new file mode 100644 (file)
index 0000000..98298c6
--- /dev/null
@@ -0,0 +1,34 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# This file is free software, which comes along with NIT. This software is
+# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. You can modify it is you want, provided this header
+# is kept unaltered, and a notification of the changes is added.
+# You are allowed to redistribute it and sell it, alone or is a part of
+# another product.
+
+# Errors thrown by the `neo4j` library.
+module neo4j::error
+
+import jsonable
+
+# An error thrown by the `neo4j` API.
+#
+#     var error = new NeoError("ErrorMessage", "ErrorName")
+#     assert error.to_json == "\{\"error\":\"ErrorName\",\"message\":\"ErrorMessage\"\}"
+class NeoError
+       super Error
+       super Jsonable
+
+       # The name of the error.
+       #
+       # Used to programmatically distinguish this kind of error from others.
+       var name: String
+
+       redef fun to_json do
+               return "\{\"error\":{name.to_json},\"message\":{message.to_json}\}"
+       end
+
+       redef fun to_s do return "[{name}] {super}"
+end
index b84a60b..496f70a 100644 (file)
@@ -64,6 +64,7 @@
 module neo4j
 
 import curl_json
+import error
 
 # Handles Neo4j server start and stop command
 #
@@ -350,15 +351,15 @@ class Neo4jClient
                                        if res.has_key("message") then
                                                msg = res["message"].to_s
                                        end
-                                       return new JsonError(error, msg.to_json)
+                                       return new NeoError(msg, error)
                                else
                                        return res
                                end
                        end
                else if response isa CurlResponseFailed then
-                       return new JsonError("Curl error", "{response.error_msg} ({response.error_code})")
+                       return new NeoError("{response.error_msg} ({response.error_code})", "CurlError")
                else
-                       return new JsonError("Curl error", "Unexpected response '{response}'")
+                       return new NeoError("Unexpected response \"{response}\".", "CurlError")
                end
        end
 end
@@ -896,7 +897,7 @@ class NeoBatch
        fun save_edges(edges: Collection[NeoEdge]) do for edge in edges do save_edge(edge)
 
        # Execute the batch and update local nodes
-       fun execute: List[JsonError] do
+       fun execute: List[NeoError] do
                var request = new JsonPOST(client.batch_url, client.curl)
                # request.headers["X-Stream"] = "true"
                var json_jobs = new JsonArray
@@ -908,16 +909,16 @@ class NeoBatch
        end
 
        # Associate data from response in original nodes and edges
-       private fun finalize_batch(response: Jsonable): List[JsonError] do
-               var errors = new List[JsonError]
+       private fun finalize_batch(response: Jsonable): List[NeoError] do
+               var errors = new List[NeoError]
                if not response isa JsonArray then
-                       errors.add(new JsonError("Neo4jError", "Unexpected batch response format"))
+                       errors.add(new NeoError("Unexpected batch response format.", "Neo4jError"))
                        return errors
                end
                # print " {res.length} jobs executed"
                for res in response do
                        if not res isa JsonObject then
-                               errors.add(new JsonError("Neo4jError", "Unexpected job format in batch response"))
+                               errors.add(new NeoError("Unexpected job format in batch response.", "Neo4jError"))
                                continue
                        end
                        var id = res["id"].as(Int)