Skip to content

Commit 9da14c9

Browse files
authored
More asConjure methods (#118)
1 parent 37d9603 commit 9da14c9

File tree

10 files changed

+195
-54
lines changed

10 files changed

+195
-54
lines changed

errors/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ dependencies {
77
compile "com.google.code.findbugs:jsr305"
88
compile "com.palantir.safe-logging:safe-logging"
99
compile "javax.ws.rs:javax.ws.rs-api"
10+
compile "com.palantir.conjure.java.api:errors"
1011

1112
testCompile project(":extras:jackson-support")
1213
testCompile "org.assertj:assertj-core"

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,33 @@ public enum Code {
5252
Code(Integer httpErrorCode) {
5353
this.httpErrorCode = httpErrorCode;
5454
}
55+
56+
@SuppressWarnings("checkstyle:CyclomaticComplexity")
57+
public com.palantir.conjure.java.api.errors.ErrorType.Code asConjure() {
58+
switch (this) {
59+
case PERMISSION_DENIED:
60+
return com.palantir.conjure.java.api.errors.ErrorType.Code.PERMISSION_DENIED;
61+
case INVALID_ARGUMENT:
62+
return com.palantir.conjure.java.api.errors.ErrorType.Code.INVALID_ARGUMENT;
63+
case NOT_FOUND:
64+
return com.palantir.conjure.java.api.errors.ErrorType.Code.NOT_FOUND;
65+
case CONFLICT:
66+
return com.palantir.conjure.java.api.errors.ErrorType.Code.CONFLICT;
67+
case REQUEST_ENTITY_TOO_LARGE:
68+
return com.palantir.conjure.java.api.errors.ErrorType.Code.REQUEST_ENTITY_TOO_LARGE;
69+
case FAILED_PRECONDITION:
70+
return com.palantir.conjure.java.api.errors.ErrorType.Code.FAILED_PRECONDITION;
71+
case INTERNAL:
72+
return com.palantir.conjure.java.api.errors.ErrorType.Code.INTERNAL;
73+
case TIMEOUT:
74+
return com.palantir.conjure.java.api.errors.ErrorType.Code.TIMEOUT;
75+
case CUSTOM_CLIENT:
76+
return com.palantir.conjure.java.api.errors.ErrorType.Code.CUSTOM_CLIENT;
77+
case CUSTOM_SERVER:
78+
return com.palantir.conjure.java.api.errors.ErrorType.Code.CUSTOM_SERVER;
79+
}
80+
throw new UnsupportedOperationException("Unable to convert to Conjure ErrorType.Code");
81+
}
5582
}
5683

5784
public static final ErrorType PERMISSION_DENIED =
@@ -79,6 +106,10 @@ public enum Code {
79106
/** The HTTP error code used to convey this error to HTTP clients. */
80107
public abstract int httpErrorCode();
81108

109+
public final com.palantir.conjure.java.api.errors.ErrorType asConjure() {
110+
return com.palantir.conjure.java.api.errors.ErrorType.create(code().asConjure(), name());
111+
}
112+
82113
@Value.Check
83114
final void check() {
84115
if (!ERROR_NAME_PATTERN.matcher(name()).matches()) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ public RemoteException(SerializableError error, int status) {
4444
this.error = error;
4545
this.status = status;
4646
}
47+
48+
/** Converts this exception to an equivalent conjure-java-api RemoteException. */
49+
public com.palantir.conjure.java.api.errors.RemoteException asConjure() {
50+
return new com.palantir.conjure.java.api.errors.RemoteException(error.asConjure(), status);
51+
}
4752
}

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

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.Serializable;
2525
import java.util.Map;
2626
import java.util.Objects;
27+
import java.util.Optional;
2728
import org.immutables.value.Value;
2829

2930
/**
@@ -45,12 +46,10 @@ public abstract class SerializableError implements Serializable {
4546
* and/or name.
4647
*/
4748
@JsonProperty("errorCode")
48-
// TODO(rfink): errorCode and exceptionClass are mutual delagates so that they can be either set independently or
49-
// one inherits from the other. This is quite a hack and should be removed when we remove support for the
50-
// exceptionClass field.
5149
@Value.Default
5250
public String errorCode() {
53-
return getExceptionClass();
51+
return getExceptionClass().orElseThrow(() -> new IllegalStateException(
52+
"Expected either 'errorCode' or 'exceptionClass' to be set"));
5453
}
5554

5655
/**
@@ -59,11 +58,10 @@ public String errorCode() {
5958
* error name via {@link RemoteException#getError} and typically switch&dispatch on the error code and/or name.
6059
*/
6160
@JsonProperty("errorName")
62-
// TODO(rfink): errorName and message are mutual delagates so that they can be either set independently or one
63-
// inherits from the other. This is quite a hack and should be removed when we remove support for the message field.
6461
@Value.Default
6562
public String errorName() {
66-
return getMessage();
63+
return getMessage().orElseThrow(() -> new IllegalStateException(
64+
"Expected either 'errorName' or 'message' to be set"));
6765
}
6866

6967
/**
@@ -86,25 +84,19 @@ public String errorInstanceId() {
8684
* @deprecated Used by the serialization-mechanism for back-compat only. Do not use.
8785
*/
8886
@Deprecated
89-
@Value.Default
90-
@JsonProperty("exceptionClass")
87+
@JsonProperty(value = "exceptionClass", access = JsonProperty.Access.WRITE_ONLY)
88+
@Value.Auxiliary
9189
@SuppressWarnings("checkstyle:designforextension")
92-
// TODO(rfink): Remove once all error producers have switched to errorCode.
93-
String getExceptionClass() {
94-
return errorCode();
95-
}
90+
abstract Optional<String> getExceptionClass();
9691

9792
/**
9893
* @deprecated Used by the serialization-mechanism for back-compat only. Do not use.
9994
*/
10095
@Deprecated
101-
@Value.Default
102-
@JsonProperty("message")
96+
@JsonProperty(value = "message", access = JsonProperty.Access.WRITE_ONLY)
97+
@Value.Auxiliary
10398
@SuppressWarnings("checkstyle:designforextension")
104-
// TODO(rfink): Remove once all error producers have switched to errorName.
105-
String getMessage() {
106-
return errorName();
107-
}
99+
abstract Optional<String> getMessage();
108100

109101
/**
110102
* Creates a {@link SerializableError} representation of this exception that derives from the error code and
@@ -123,6 +115,24 @@ public static SerializableError forException(ServiceException exception) {
123115
return builder.build();
124116
}
125117

118+
public final com.palantir.conjure.java.api.errors.SerializableError asConjure() {
119+
com.palantir.conjure.java.api.errors.SerializableError.Builder builder =
120+
com.palantir.conjure.java.api.errors.SerializableError.builder();
121+
122+
if (!getExceptionClass().isPresent()) {
123+
builder.errorCode(errorCode());
124+
builder.errorName(errorName());
125+
} else {
126+
// this is for back-compat, old remoting2 exceptions have 'exceptionClass' and 'message' fields.
127+
builder.exceptionClass(getExceptionClass().get());
128+
builder.message(getMessage().get());
129+
}
130+
131+
builder.errorInstanceId(errorInstanceId());
132+
133+
return builder.build();
134+
}
135+
126136
// TODO(rfink): Remove once all error producers have switched to errorCode/errorName.
127137
public static final class Builder extends ImmutableSerializableError.Builder {}
128138

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ public void forException_arg_key_collisions_just_use_the_last_one() {
7171
@Test
7272
public void testSerializationContainsRedundantParameters() throws Exception {
7373
assertThat(mapper.writeValueAsString(ERROR))
74-
.isEqualTo("{\"errorCode\":\"code\",\"errorName\":\"name\",\"errorInstanceId\":\"\",\"parameters\":{},"
75-
+ "\"exceptionClass\":\"code\",\"message\":\"name\"}");
74+
.isEqualTo(
75+
"{\"errorCode\":\"code\",\"errorName\":\"name\",\"errorInstanceId\":\"\",\"parameters\":{}}");
7676

7777
assertThat(mapper.writeValueAsString(
7878
SerializableError.builder().from(ERROR).errorInstanceId("errorId").build()))
7979
.isEqualTo("{\"errorCode\":\"code\",\"errorName\":\"name\",\"errorInstanceId\":\"errorId\""
80-
+ ",\"parameters\":{},\"exceptionClass\":\"code\",\"message\":\"name\"}");
80+
+ ",\"parameters\":{}}");
8181
}
8282

8383
@Test
@@ -105,24 +105,16 @@ public void testDeserializationFailsWhenNeitherErrorNameNorMessageIsSet() throws
105105
String serialized = "{\"errorCode\":\"code\"}";
106106
assertThatThrownBy(() -> deserialize(serialized))
107107
.isInstanceOf(IllegalStateException.class)
108-
.hasMessage("Cannot build SerializableError, attribute initializers form cycle[errorName, message]");
108+
.hasMessage("Expected either 'errorName' or 'message' to be set");
109109
}
110110

111111
@Test
112-
public void testDeserializesWithBackupNamesOnly() throws Exception {
113-
String serialized = "{\"message\":\"name\",\"exceptionClass\":\"code\"}";
114-
assertThat(deserialize(serialized)).isEqualTo(ERROR);
115-
}
116-
117-
@Test
118-
public void testDeserializesWithWhenObsoleteExceptionClassAndMessageAreGiven() throws Exception {
119-
String serialized = "{\"errorCode\":\"code\",\"errorName\":\"name\",\"exceptionClass\":\"obsolete-class\","
120-
+ "\"message\":\"obsolete-message\"}";
121-
assertThat(deserialize(serialized)).isEqualTo(SerializableError.builder()
122-
.from(ERROR)
123-
.exceptionClass("obsolete-class")
124-
.message("obsolete-message")
125-
.build());
112+
public void asConjure_converts_json_nicely() throws IOException {
113+
assertThat(deserialize("{\"errorCode\":\"code\",\"errorName\":\"name\"}").asConjure())
114+
.isEqualTo(com.palantir.conjure.java.api.errors.SerializableError.builder()
115+
.errorCode("code")
116+
.errorName("name")
117+
.build());
126118
}
127119

128120
private static SerializableError deserialize(String serialized) throws IOException {

errors/versions.lock

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,44 @@
1313
]
1414
},
1515
"com.fasterxml.jackson.core:jackson-databind": {
16-
"locked": "2.9.5"
16+
"locked": "2.9.5",
17+
"transitive": [
18+
"com.palantir.conjure.java.api:errors"
19+
]
1720
},
1821
"com.google.code.findbugs:jsr305": {
19-
"locked": "3.0.1"
22+
"locked": "3.0.1",
23+
"transitive": [
24+
"com.palantir.conjure.java.api:errors"
25+
]
26+
},
27+
"com.google.errorprone:error_prone_annotations": {
28+
"locked": "2.1.3",
29+
"transitive": [
30+
"com.palantir.safe-logging:preconditions"
31+
]
32+
},
33+
"com.palantir.conjure.java.api:errors": {
34+
"locked": "2.0.0-rc7"
35+
},
36+
"com.palantir.safe-logging:preconditions": {
37+
"locked": "1.5.0",
38+
"transitive": [
39+
"com.palantir.conjure.java.api:errors"
40+
]
2041
},
2142
"com.palantir.safe-logging:safe-logging": {
22-
"locked": "0.1.3"
43+
"locked": "0.1.3",
44+
"transitive": [
45+
"com.palantir.conjure.java.api:errors",
46+
"com.palantir.safe-logging:preconditions"
47+
]
2348
},
2449
"javax.ws.rs:javax.ws.rs-api": {
25-
"locked": "2.0.1"
50+
"locked": "2.0.1",
51+
"transitive": [
52+
"com.palantir.conjure.java.api:errors"
53+
]
2654
}
2755
},
2856
"runtime": {
@@ -39,16 +67,44 @@
3967
]
4068
},
4169
"com.fasterxml.jackson.core:jackson-databind": {
42-
"locked": "2.9.5"
70+
"locked": "2.9.5",
71+
"transitive": [
72+
"com.palantir.conjure.java.api:errors"
73+
]
4374
},
4475
"com.google.code.findbugs:jsr305": {
45-
"locked": "3.0.1"
76+
"locked": "3.0.1",
77+
"transitive": [
78+
"com.palantir.conjure.java.api:errors"
79+
]
80+
},
81+
"com.google.errorprone:error_prone_annotations": {
82+
"locked": "2.1.3",
83+
"transitive": [
84+
"com.palantir.safe-logging:preconditions"
85+
]
86+
},
87+
"com.palantir.conjure.java.api:errors": {
88+
"locked": "2.0.0-rc7"
89+
},
90+
"com.palantir.safe-logging:preconditions": {
91+
"locked": "1.5.0",
92+
"transitive": [
93+
"com.palantir.conjure.java.api:errors"
94+
]
4695
},
4796
"com.palantir.safe-logging:safe-logging": {
48-
"locked": "0.1.3"
97+
"locked": "0.1.3",
98+
"transitive": [
99+
"com.palantir.conjure.java.api:errors",
100+
"com.palantir.safe-logging:preconditions"
101+
]
49102
},
50103
"javax.ws.rs:javax.ws.rs-api": {
51-
"locked": "2.0.1"
104+
"locked": "2.0.1",
105+
"transitive": [
106+
"com.palantir.conjure.java.api:errors"
107+
]
52108
}
53109
}
54110
}

service-config/versions.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
]
3636
},
3737
"com.palantir.conjure.java.api:service-config": {
38-
"locked": "2.0.0-rc4"
38+
"locked": "2.0.0-rc7"
3939
},
4040
"com.palantir.conjure.java.api:ssl-config": {
41-
"locked": "2.0.0-rc4",
41+
"locked": "2.0.0-rc7",
4242
"transitive": [
4343
"com.palantir.conjure.java.api:service-config",
4444
"com.palantir.remoting-api:ssl-config"
@@ -109,10 +109,10 @@
109109
]
110110
},
111111
"com.palantir.conjure.java.api:service-config": {
112-
"locked": "2.0.0-rc4"
112+
"locked": "2.0.0-rc7"
113113
},
114114
"com.palantir.conjure.java.api:ssl-config": {
115-
"locked": "2.0.0-rc4",
115+
"locked": "2.0.0-rc7",
116116
"transitive": [
117117
"com.palantir.conjure.java.api:service-config",
118118
"com.palantir.remoting-api:ssl-config"

ssl-config/versions.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
]
2020
},
2121
"com.palantir.conjure.java.api:ssl-config": {
22-
"locked": "2.0.0-rc4"
22+
"locked": "2.0.0-rc7"
2323
}
2424
},
2525
"runtime": {
@@ -42,7 +42,7 @@
4242
]
4343
},
4444
"com.palantir.conjure.java.api:ssl-config": {
45-
"locked": "2.0.0-rc4"
45+
"locked": "2.0.0-rc7"
4646
}
4747
}
4848
}

0 commit comments

Comments
 (0)