Skip to content

Commit c11640c

Browse files
authored
[fix] SerializableError#asConjure() handles both remoting2 and remoting3 JSON keys (#124)
1 parent 9da14c9 commit c11640c

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

errors/src/main/java/com/palantir/remoting/api/errors/SerializableError.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,16 @@ public final com.palantir.conjure.java.api.errors.SerializableError asConjure()
119119
com.palantir.conjure.java.api.errors.SerializableError.Builder builder =
120120
com.palantir.conjure.java.api.errors.SerializableError.builder();
121121

122-
if (!getExceptionClass().isPresent()) {
123-
builder.errorCode(errorCode());
124-
builder.errorName(errorName());
125-
} else {
122+
if (getExceptionClass().isPresent()) {
126123
// this is for back-compat, old remoting2 exceptions have 'exceptionClass' and 'message' fields.
127124
builder.exceptionClass(getExceptionClass().get());
128125
builder.message(getMessage().get());
129126
}
130127

128+
builder.errorName(errorName());
129+
builder.errorCode(errorCode());
131130
builder.errorInstanceId(errorInstanceId());
131+
builder.parameters(parameters());
132132

133133
return builder.build();
134134
}

errors/src/test/java/com/palantir/remoting/api/errors/SerializableErrorTest.java

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public final class SerializableErrorTest {
3434
.build();
3535
private static final ObjectMapper mapper = ObjectMappers.newServerObjectMapper();
3636

37+
private static SerializableError deserialize(String serialized) throws IOException {
38+
return mapper.readValue(serialized, SerializableError.class);
39+
}
40+
3741
@Test
3842
public void forException_should_keep_both_safe_and_unsafe_args() {
3943
ErrorType error = ErrorType.FAILED_PRECONDITION;
@@ -109,15 +113,59 @@ public void testDeserializationFailsWhenNeitherErrorNameNorMessageIsSet() throws
109113
}
110114

111115
@Test
112-
public void asConjure_converts_json_nicely() throws IOException {
116+
public void asConjure_converts_remoting3_json() throws IOException {
113117
assertThat(deserialize("{\"errorCode\":\"code\",\"errorName\":\"name\"}").asConjure())
114118
.isEqualTo(com.palantir.conjure.java.api.errors.SerializableError.builder()
115119
.errorCode("code")
116120
.errorName("name")
117121
.build());
118122
}
119123

120-
private static SerializableError deserialize(String serialized) throws IOException {
121-
return mapper.readValue(serialized, SerializableError.class);
124+
@Test
125+
public void asConjure_converts_remoting2_json() throws IOException {
126+
assertThat(deserialize("{\"exceptionClass\":\"java.lang.IllegalStateException\","
127+
+ "\"message\":\"Human readable message\"}").asConjure())
128+
.isEqualTo(com.palantir.conjure.java.api.errors.SerializableError.builder()
129+
.exceptionClass("java.lang.IllegalStateException")
130+
.message("Human readable message")
131+
.build());
132+
}
133+
134+
@Test
135+
public void asConjure_converts_json_with_both_remoting2_and_remoting3_fields() throws IOException {
136+
assertThat(deserialize("{\"errorCode\":\"PERMISSION_DENIED\",\"errorName\":\"Product:SomethingBroke\","
137+
+ "\"exceptionClass\":\"java.lang.IllegalStateException\",\"message\":\"Human readable message\","
138+
+ "\"errorInstanceId\":\"7d345390-e41d-49dd-8dcb-38dd716c0347\",\"parameters\":{\"a\":\"1\"}}")
139+
.asConjure())
140+
.isEqualTo(com.palantir.conjure.java.api.errors.SerializableError.builder()
141+
.errorCode("PERMISSION_DENIED")
142+
.errorName("Product:SomethingBroke")
143+
.errorInstanceId("7d345390-e41d-49dd-8dcb-38dd716c0347")
144+
.putParameters("a", "1")
145+
.build());
146+
}
147+
148+
@Test
149+
public void all_fields_are_preserved_when_present() throws IOException {
150+
String string = "{\n"
151+
+ " \"errorCode\":\"CONFLICT\",\n"
152+
+ " \"errorName\":\"Product:TransactionConflict\",\n"
153+
+ " \"errorInstanceId\":\"7d345390-e41d-49dd-8dcb-38dd716c0347\",\n"
154+
+ " \"parameters\":{},\n"
155+
+ " \"exceptionClass\":\"CONFLICT\",\n"
156+
+ " \"message\":\"Refer to the server logs with this errorInstanceId: "
157+
+ "7d345390-e41d-49dd-8dcb-38dd716c0347\"\n"
158+
+ "}";
159+
160+
com.palantir.conjure.java.api.errors.SerializableError expected =
161+
com.palantir.conjure.java.api.errors.SerializableError.builder()
162+
.errorCode("CONFLICT")
163+
.errorName("Product:TransactionConflict")
164+
.errorInstanceId("7d345390-e41d-49dd-8dcb-38dd716c0347")
165+
.message("Refer to the server logs with this errorInstanceId: 7d345390-e41d-49dd-8dcb-38dd716c0347")
166+
.exceptionClass("CONFLICT")
167+
.build();
168+
169+
assertThat(deserialize(string).asConjure()).isEqualTo(expected);
122170
}
123171
}

0 commit comments

Comments
 (0)