Commit 027fef83 authored by Greg Messner's avatar Greg Messner
Browse files

Added support for array error messages (#436).

parent a86a9d19
...@@ -101,10 +101,23 @@ public class GitLabApiException extends Exception { ...@@ -101,10 +101,23 @@ public class GitLabApiException extends Exception {
if (buf.length() > 0) { if (buf.length() > 0) {
this.message = "The following fields have validation errors: " + buf.toString(); this.message = "The following fields have validation errors: " + buf.toString();
} }
} else { } else if (jsonMessage.isArray()) {
List<String> values = new ArrayList<>();
for (JsonNode value : jsonMessage) {
values.add(value.asText());
}
if (values.size() > 0) {
this.message = String.join("\n", values);
}
} else if (jsonMessage.isTextual()) {
this.message = jsonMessage.asText(); this.message = jsonMessage.asText();
} else {
this.message = jsonMessage.toString();
} }
} else { } else {
......
...@@ -34,6 +34,10 @@ public class MockResponse extends Response { ...@@ -34,6 +34,10 @@ public class MockResponse extends Response {
private String itemJson; private String itemJson;
private String listJson; private String listJson;
private int status = 200;
private Status statusInfo = Status.OK;
private MediaType mediaType = MediaType.APPLICATION_JSON_TYPE;
public MockResponse() { public MockResponse() {
} }
...@@ -41,6 +45,13 @@ public class MockResponse extends Response { ...@@ -41,6 +45,13 @@ public class MockResponse extends Response {
init(type, itemFilename, listFilename); init(type, itemFilename, listFilename);
} }
public <T> MockResponse(Status statusInfo, String jsonString) {
this.statusInfo = statusInfo;
this.status = statusInfo.getStatusCode();
this.itemJson = jsonString;
this.responseItem = jsonString;
}
public <T> void init(Class<T> type, String itemFilename, String listFilename) throws Exception { public <T> void init(Class<T> type, String itemFilename, String listFilename) throws Exception {
if (itemFilename != null) { if (itemFilename != null) {
...@@ -110,11 +121,25 @@ public class MockResponse extends Response { ...@@ -110,11 +121,25 @@ public class MockResponse extends Response {
} }
} }
@Override
public boolean hasEntity() {
return (itemJson != null || listJson != null);
}
@Override @Override
public int getStatus() { public int getStatus() {
return (200); return (status);
} }
@Override
public StatusType getStatusInfo() {
return (statusInfo);
}
@Override
public MediaType getMediaType() {
return (mediaType);
}
/************************************************************************************************** /**************************************************************************************************
* The remaining methods are stubbed so we can create an instance of this class. They are simply * The remaining methods are stubbed so we can create an instance of this class. They are simply
...@@ -122,11 +147,6 @@ public class MockResponse extends Response { ...@@ -122,11 +147,6 @@ public class MockResponse extends Response {
* on Java 11+ and did not wish to pull in the JAXB module even for testing. * on Java 11+ and did not wish to pull in the JAXB module even for testing.
**************************************************************************************************/ **************************************************************************************************/
@Override
public StatusType getStatusInfo() {
return null;
}
@Override @Override
public <T> T readEntity(Class<T> entityType, Annotation[] annotations) { public <T> T readEntity(Class<T> entityType, Annotation[] annotations) {
return null; return null;
...@@ -137,11 +157,6 @@ public class MockResponse extends Response { ...@@ -137,11 +157,6 @@ public class MockResponse extends Response {
return null; return null;
} }
@Override
public boolean hasEntity() {
return false;
}
@Override @Override
public boolean bufferEntity() { public boolean bufferEntity() {
return false; return false;
...@@ -151,11 +166,6 @@ public class MockResponse extends Response { ...@@ -151,11 +166,6 @@ public class MockResponse extends Response {
public void close() { public void close() {
} }
@Override
public MediaType getMediaType() {
return null;
}
@Override @Override
public Locale getLanguage() { public Locale getLanguage() {
return null; return null;
......
...@@ -10,6 +10,8 @@ import static org.junit.Assume.assumeNotNull; ...@@ -10,6 +10,8 @@ import static org.junit.Assume.assumeNotNull;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.ws.rs.core.Response.Status;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.Visibility; import org.gitlab4j.api.models.Visibility;
import org.junit.AfterClass; import org.junit.AfterClass;
...@@ -31,6 +33,12 @@ import org.junit.experimental.categories.Category; ...@@ -31,6 +33,12 @@ import org.junit.experimental.categories.Category;
public class TestGitLabApiException extends AbstractIntegrationTest { public class TestGitLabApiException extends AbstractIntegrationTest {
private static final String TEST_PROJECT_NAME_DUPLICATE = "test-gitlab4j-create-project-duplicate"; private static final String TEST_PROJECT_NAME_DUPLICATE = "test-gitlab4j-create-project-duplicate";
private static final String TEST_ERROR_MESSAGE = "Another open merge request already exists for this source branch: !6";
private static final String TEST_RESPONSE_JSON_STRING = "{\"message\": \"" + TEST_ERROR_MESSAGE + "\"}";
private static final String TEST_RESPONSE_JSON_ARRAY = "{\"message\": [\"" + TEST_ERROR_MESSAGE + "\"]}";
private static final String TEST_RESPONSE_ERROR_JSON_STRING = "{\"error\": \"" + TEST_ERROR_MESSAGE + "\"}";
private static GitLabApi gitLabApi; private static GitLabApi gitLabApi;
public TestGitLabApiException() { public TestGitLabApiException() {
...@@ -98,4 +106,28 @@ public class TestGitLabApiException extends AbstractIntegrationTest { ...@@ -98,4 +106,28 @@ public class TestGitLabApiException extends AbstractIntegrationTest {
assertFalse(validationErrors.isEmpty()); assertFalse(validationErrors.isEmpty());
} }
} }
@Test
public void testStringMessage() throws GitLabApiException {
final MockResponse response = new MockResponse(Status.BAD_REQUEST, TEST_RESPONSE_JSON_STRING);
GitLabApiException glae = new GitLabApiException(response);
assertEquals(Status.BAD_REQUEST.getStatusCode(), glae.getHttpStatus());
assertEquals(TEST_ERROR_MESSAGE, glae.getMessage());
}
@Test
public void testArrayMessage() throws GitLabApiException {
final MockResponse response = new MockResponse(Status.BAD_REQUEST, TEST_RESPONSE_JSON_ARRAY);
GitLabApiException glae = new GitLabApiException(response);
assertEquals(Status.BAD_REQUEST.getStatusCode(), glae.getHttpStatus());
assertEquals(TEST_ERROR_MESSAGE, glae.getMessage());
}
@Test
public void testError() throws GitLabApiException {
final MockResponse response = new MockResponse(Status.BAD_REQUEST, TEST_RESPONSE_ERROR_JSON_STRING);
GitLabApiException glae = new GitLabApiException(response);
assertEquals(Status.BAD_REQUEST.getStatusCode(), glae.getHttpStatus());
assertEquals(TEST_ERROR_MESSAGE, glae.getMessage());
}
} }
Supports Markdown
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