Skip to content

Commit

Permalink
Merge pull request #139 from rundeck/exp/error-resp-parse
Browse files Browse the repository at this point in the history
Update error response parsing to match potential input from server
  • Loading branch information
gschueler authored Dec 14, 2017
2 parents a014cd9 + 7b03c28 commit a9600cf
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/main/java/org/rundeck/client/api/model/ErrorResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.rundeck.client.util.Xml;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Path;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.*;

import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
@Root(strict = false)
Expand All @@ -39,10 +38,14 @@ public class ErrorResponse implements ErrorDetail {
@Path("error")
public String errorCode;

@Element
@Element(required = false)
@Path("error")
public String message;

@Path("error")
@ElementList(entry = "message", required = false)
public List<String> messages;

public String toCodeString() {
if (null != errorCode) {
return String.format(
Expand All @@ -61,7 +64,11 @@ public String getErrorCode() {

@Override
public String getErrorMessage() {
return message != null ? message : error;
return message != null ? message :
messages != null ? messages.size() == 1
? messages.get(0)
: messages.toString()
: error;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory
import retrofit2.converter.simplexml.SimpleXmlConverterFactory
import spock.lang.Specification
import spock.lang.Unroll

import java.lang.annotation.Annotation

Expand All @@ -15,6 +16,7 @@ import java.lang.annotation.Annotation
* @since 12/13/17
*/
class ErrorResponseSpec extends Specification {
@Unroll
def "xml parse code not required"() {
given:
def retrofit = new Retrofit.Builder().baseUrl('http://test').
Expand All @@ -34,6 +36,8 @@ class ErrorResponseSpec extends Specification {
result.errorCode == code
result.error == error
result.message == message
result.messages == null
result.errorMessage == message
result.apiVersion == version

where:
Expand All @@ -51,4 +55,88 @@ class ErrorResponseSpec extends Specification {
'blah' | 20

}

@Unroll
def "xml parse multi message"() {
given:
def retrofit = new Retrofit.Builder().baseUrl('http://test').
addConverterFactory(SimpleXmlConverterFactory.create()).
build()

when:
Converter<ResponseBody, ErrorResponse> converter = retrofit.responseBodyConverter(
ErrorResponse.class,
[] as Annotation[],
);
ErrorResponse result = converter.convert(ResponseBody.create(MediaType.parse('application/xml'), xmlText))

then:

result != null
result.errorCode == code
result.error == error
result.messages == messages
result.errorMessage == messages.toString()
result.apiVersion == version

where:
xmlText |
code |
error |
messages |
version
'<result error=\'true\' apiversion=\'20\'><error ' +
'code=\'xyz\'><messages><message>blah</message><message>blah2</message></messages></error></result>' |
'xyz' |
'true' |
['blah', 'blah2'] |
20

}

def "json parse with multiple messages"() {
given:
def retrofit = new Retrofit.Builder().baseUrl('http://test').
addConverterFactory(JacksonConverterFactory.create()).
build()

when:
Converter<ResponseBody, ErrorResponse> converter = retrofit.responseBodyConverter(
ErrorResponse.class,
[] as Annotation[],
);
ErrorResponse result = converter.convert(
ResponseBody.create(MediaType.parse('application/json'), jsonText)
)

then:

result != null
result.errorCode == code
result.error == error
result.message == message
result.messages == messages
result.apiVersion == version

where:
jsonText |
code |
error |
message |
messages |
version
'{"error":true,"apiversion":21,"errorCode":"test","messages":["a","b"]}' |
'test' |
'true' |
null |
['a', 'b'] |
21
'{"error":true,"apiversion":21,"errorCode":"api.error.unknown","message":"A message"}' |
'api.error.unknown' |
'true' |
'A message' |
null |
21

}
}

0 comments on commit a9600cf

Please sign in to comment.