Skip to content

Commit

Permalink
Added support for array error messages (gitlab4j#436).
Browse files Browse the repository at this point in the history
  • Loading branch information
gmessner committed Sep 25, 2019
1 parent a86a9d1 commit 027fef8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 18 deletions.
17 changes: 15 additions & 2 deletions src/main/java/org/gitlab4j/api/GitLabApiException.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,23 @@ public GitLabApiException(Response response) {

if (buf.length() > 0) {
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();
} else {
this.message = jsonMessage.toString();
}

} else {
Expand Down
42 changes: 26 additions & 16 deletions src/test/java/org/gitlab4j/api/MockResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,24 @@ public class MockResponse extends Response {
private String itemJson;
private String listJson;

private int status = 200;
private Status statusInfo = Status.OK;
private MediaType mediaType = MediaType.APPLICATION_JSON_TYPE;

public MockResponse() {
}

public <T> MockResponse(Class<T> type, String itemFilename, String listFilename) throws Exception {
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 {

if (itemFilename != null) {
Expand Down Expand Up @@ -110,23 +121,32 @@ public String getHeaderString(String name) {
}
}

@Override
public boolean hasEntity() {
return (itemJson != null || listJson != null);
}

@Override
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
* stubs, but needed to do this because the Mockito Spy annotation does not work without JAXB
* on Java 11+ and did not wish to pull in the JAXB module even for testing.
**************************************************************************************************/

@Override
public StatusType getStatusInfo() {
return null;
}

@Override
public <T> T readEntity(Class<T> entityType, Annotation[] annotations) {
return null;
Expand All @@ -137,11 +157,6 @@ public <T> T readEntity(GenericType<T> entityType, Annotation[] annotations) {
return null;
}

@Override
public boolean hasEntity() {
return false;
}

@Override
public boolean bufferEntity() {
return false;
Expand All @@ -151,11 +166,6 @@ public boolean bufferEntity() {
public void close() {
}

@Override
public MediaType getMediaType() {
return null;
}

@Override
public Locale getLanguage() {
return null;
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/org/gitlab4j/api/TestGitLabApiException.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.Response.Status;

import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.Visibility;
import org.junit.AfterClass;
Expand All @@ -31,6 +33,12 @@
public class TestGitLabApiException extends AbstractIntegrationTest {

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;

public TestGitLabApiException() {
Expand Down Expand Up @@ -98,4 +106,28 @@ public void testValidationErrors() throws GitLabApiException {
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());
}
}

0 comments on commit 027fef8

Please sign in to comment.