Commit 1ae3f82a authored by Tim Olshansky's avatar Tim Olshansky

Merge pull request #92 from wikiwi/gitlab-exception

Expose HTTP Code through GitlabAPIException
parents f328e64f cb94dab0
package org.gitlab.api;
import java.io.IOException;
/**
* Gitlab API Exception
*/
public class GitlabAPIException extends IOException {
private int responseCode;
public GitlabAPIException(String message, Integer responseCode, Throwable cause) {
super(message, cause);
this.responseCode = responseCode;
}
public int getResponseCode() {
return responseCode;
}
}
......@@ -3,6 +3,7 @@ package org.gitlab.api.http;
import org.apache.commons.io.IOUtils;
import org.gitlab.api.AuthMethod;
import org.gitlab.api.GitlabAPI;
import org.gitlab.api.GitlabAPIException;
import org.gitlab.api.TokenType;
import org.gitlab.api.models.GitlabCommit;
......@@ -353,11 +354,11 @@ public class GitlabHTTPRequestor {
InputStream es = wrapStream(connection, connection.getErrorStream());
try {
String error = null;
if (es != null) {
throw (IOException) new IOException(IOUtils.toString(es, "UTF-8")).initCause(e);
} else {
throw e;
error = IOUtils.toString(es, "UTF-8");
}
throw new GitlabAPIException(error, connection.getResponseCode(), e);
} finally {
IOUtils.closeQuietly(es);
}
......
......@@ -32,10 +32,12 @@ public class GitlabAPITest {
api.dispatch().with("login", "INVALID").with("password", rand).to("session", GitlabUser.class);
} catch (ConnectException e) {
assumeNoException("GITLAB not running on '" + TEST_URL + "', skipping...", e);
} catch (IOException e) {
} catch (GitlabAPIException e) {
final String message = e.getMessage();
if (!message.equals("{\"message\":\"401 Unauthorized\"}")) {
throw new AssertionError("Expected an unauthorized message", e);
} else if(e.getResponseCode() != 401) {
throw new AssertionError("Expected 401 code", e);
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment