package com.sudicode.fb2gh.github;

import com.sudicode.fb2gh.FB2GHException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.json.Json;
import javax.json.JsonObject;
import java.io.StringReader;

/**
 * Utility methods for the GitHub API.
 */
final class GHUtils {

    private static final Logger logger = LoggerFactory.getLogger(GHUtils.class);

    /**
     * This is a utility class which is not designed for instantiation.
     */
    private GHUtils() {
        throw new AssertionError("Cannot instantiate.");
    }

    /**
     * When the GitHub API encounters an error, it returns a JSON object which describes it. The GitHub <em>library</em>
     * will in turn throw an {@link AssertionError} with a rather verbose message that contains the JSON object.
     * <p>
     * Passing the {@link AssertionError} to this method does the following:
     * <ul>
     * <li>Logs the JSON object.</li>
     * <li>Throws an {@link FB2GHException} with the error message from GitHub API.</li>
     * </ul>
     * </p>
     * This method will <strong>never</strong> complete normally, but the compiler doesn't know this. Therefore,
     * its return type has been made a {@link RuntimeException} so that it can be invoked using
     * <code>throw GHUtils.rethrow(e)</code>.
     *
     * @param e An {@link AssertionError} <em>from the GitHub library</em>
     * @return Never returns.
     * @throws FB2GHException normally
     */
    static RuntimeException rethrow(final AssertionError e) throws FB2GHException {
        try {
            // Parse e.getMessage() for GitHub's JSON response.
            String error = e.getMessage();
            String response = StringUtils.removeEnd(error.substring(error.lastIndexOf("\n")).trim(), ">");

            // Log it.
            logger.error("GitHub error: {}", response);

            // Parse the JSON and throw FB2GHException.
            JsonObject json = Json.createReader(new StringReader(response)).readObject();
            throw new FB2GHException(json.getString("message"));
        } catch (RuntimeException ex) {
            logger.debug("Could not parse JSON response.", ex);
            throw new FB2GHException("GitHub error.");
        }
    }

}

@@ -220,22 +220,22 @@
 son.Json
-Object
+Reader
 ;%0Aimport
@@ -1789,12 +1789,12 @@
 xOf(
-%22%5Cn%22
+'%5Cn'
 )).t
@@ -1959,23 +1959,30 @@
     
-JsonObject json
+try (JsonReader reader
  = J
@@ -2029,22 +2029,11 @@
 se))
-.readObject();
+) %7B
 %0A   
@@ -2033,32 +2033,36 @@
 ) %7B%0A            
+    
 throw new FB2GHE
@@ -2070,20 +2070,35 @@
 ception(
-json
+reader.readObject()
 .getStri
@@ -2113,16 +2113,30 @@
 age%22));%0A
+            %7D%0A
         

Update GHUtils