lib/github: introduces GithubCurl::get_and_parse to handle API error without aborting
authorAlexandre Terrasa <alexandre@moz-code.org>
Mon, 15 Dec 2014 13:13:07 +0000 (08:13 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Wed, 17 Dec 2014 20:09:46 +0000 (15:09 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/github/github_curl.nit

index 5e50474..5f6339f 100644 (file)
@@ -66,6 +66,70 @@ class GithubCurl
                        abort
                else abort
        end
+
+       # Get the requested URI, and check the HTTP response.
+       # Then convert to JSON and check for Github errors.
+       # Unlike `get_and_check`, error do not trigger an abort but
+       # are reported as `GithubError`.
+       fun get_and_parse(uri: String): nullable Jsonable
+       do
+               var request = new CurlHTTPRequest(uri, self)
+               request.user_agent = user_agent
+               request.headers = header
+               var response = request.execute
+               if response isa CurlResponseSuccess then
+                       var obj = response.body_str.parse_json
+                       if obj isa JsonObject then
+                               if obj.keys.has("message") then
+                                       var title = "GithubAPIError"
+                                       var msg = obj["message"].to_s or else ""
+                                       var err = new GithubError(msg, title)
+                                       err.json["requested_uri"] = uri
+                                       err.json["status_code"] = response.status_code
+                                       return err
+                               end
+                       end
+                       return obj
+
+               else if response isa CurlResponseFailed then
+                       var title = "CurlResponseFailed"
+                       var msg = "Request to Github API failed"
+                       var err = new GithubError(msg, title)
+                       err.json["requested_uri"] = uri
+                       err.json["error_code"] = response.error_code
+                       err.json["response"] = response.error_msg
+                       return err
+               else abort
+       end
+
+end
+
+# An error thrown by the Github API.
+#
+# Depending on the kind of error, additionnal informations can be stored in
+# the error object.
+# Check the `json` value to find them.
+class GithubError
+       super Error
+       super Jsonable
+
+       # The name of the error.
+       var name: String
+
+       # The json content of the error.
+       var json = new JsonObject
+
+       redef init do
+               super
+               json["error"] = name.to_json
+               json["message"] = message.to_json
+       end
+
+       redef fun to_json do
+               return json.to_json
+       end
+
+       redef fun to_s do return "[{name}] {super}"
 end
 
 # Return the value of `git config --get github.oauthtoken`