Skip to content

Commit ff297df

Browse files
authored
[fix] Re-add SerializableError backcompat fields as write-only (#115)
1 parent b95a60c commit ff297df

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
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+
implementation "com.palantir.safe-logging:preconditions"
1011

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

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
2222
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
2323
import com.palantir.logsafe.Arg;
24+
import com.palantir.logsafe.exceptions.SafeIllegalStateException;
2425
import java.io.Serializable;
2526
import java.util.Map;
2627
import java.util.Objects;
28+
import java.util.Optional;
2729
import org.immutables.value.Value;
2830

2931
/**
@@ -45,15 +47,23 @@ public abstract class SerializableError implements Serializable {
4547
* and/or name.
4648
*/
4749
@JsonProperty("errorCode")
48-
public abstract String errorCode();
50+
@Value.Default
51+
public String errorCode() {
52+
return getExceptionClass().orElseThrow(() -> new SafeIllegalStateException(
53+
"Expected either 'errorCode' or 'exceptionClass' to be set"));
54+
}
4955

5056
/**
5157
* A fixed name identifying the error. For errors generated from {@link ServiceException}, this corresponding to the
5258
* {@link ErrorType#name} and is part of the service's API surface. Clients are given access to the service-side
5359
* error name via {@link RemoteException#getError} and typically switch&dispatch on the error code and/or name.
5460
*/
5561
@JsonProperty("errorName")
56-
public abstract String errorName();
62+
@Value.Default
63+
public String errorName() {
64+
return getMessage().orElseThrow(() -> new SafeIllegalStateException(
65+
"Expected either 'errorName' or 'message' to be set"));
66+
}
5767

5868
/**
5969
* A unique identifier for this error instance, typically used to correlate errors displayed in user-facing
@@ -71,6 +81,24 @@ public String errorInstanceId() {
7181
/** A set of parameters that further explain the error. */
7282
public abstract Map<String, String> parameters();
7383

84+
/**
85+
* @deprecated Used by the serialization-mechanism for back-compat only. Do not use.
86+
*/
87+
@Deprecated
88+
@JsonProperty(value = "exceptionClass", access = JsonProperty.Access.WRITE_ONLY)
89+
@Value.Auxiliary
90+
@SuppressWarnings("checkstyle:designforextension")
91+
abstract Optional<String> getExceptionClass();
92+
93+
/**
94+
* @deprecated Used by the serialization-mechanism for back-compat only. Do not use.
95+
*/
96+
@Deprecated
97+
@JsonProperty(value = "message", access = JsonProperty.Access.WRITE_ONLY)
98+
@Value.Auxiliary
99+
@SuppressWarnings("checkstyle:designforextension")
100+
abstract Optional<String> getMessage();
101+
74102
/**
75103
* Creates a {@link SerializableError} representation of this exception that derives from the error code and
76104
* message, as well as the {@link Arg#isSafeForLogging safe} and unsafe {@link ServiceException#args parameters}.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void testDeserializationFailsWhenNeitherErrorNameNorMessageIsSet() throws
105105
String serialized = "{\"errorCode\":\"code\"}";
106106
assertThatThrownBy(() -> deserialize(serialized))
107107
.isInstanceOf(IllegalStateException.class)
108-
.hasMessage("Cannot build SerializableError, some of required attributes are not set [errorName]");
108+
.hasMessage("Expected either 'errorName' or 'message' to be set");
109109
}
110110

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

errors/versions.lock

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,21 @@
1818
"com.google.code.findbugs:jsr305": {
1919
"locked": "3.0.1"
2020
},
21-
"com.palantir.safe-logging:safe-logging": {
21+
"com.google.errorprone:error_prone_annotations": {
22+
"locked": "2.1.3",
23+
"transitive": [
24+
"com.palantir.safe-logging:preconditions"
25+
]
26+
},
27+
"com.palantir.safe-logging:preconditions": {
2228
"locked": "1.5.0"
2329
},
30+
"com.palantir.safe-logging:safe-logging": {
31+
"locked": "1.5.0",
32+
"transitive": [
33+
"com.palantir.safe-logging:preconditions"
34+
]
35+
},
2436
"javax.ws.rs:javax.ws.rs-api": {
2537
"locked": "2.0.1"
2638
}

test-utils/versions.lock

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,28 @@
8787
"com.palantir.conjure.java.api:errors"
8888
]
8989
},
90+
"com.google.errorprone:error_prone_annotations": {
91+
"locked": "2.1.3",
92+
"transitive": [
93+
"com.palantir.safe-logging:preconditions"
94+
]
95+
},
9096
"com.palantir.conjure.java.api:errors": {
9197
"project": true
9298
},
93-
"com.palantir.safe-logging:safe-logging": {
99+
"com.palantir.safe-logging:preconditions": {
94100
"locked": "1.5.0",
95101
"transitive": [
96102
"com.palantir.conjure.java.api:errors"
97103
]
98104
},
105+
"com.palantir.safe-logging:safe-logging": {
106+
"locked": "1.5.0",
107+
"transitive": [
108+
"com.palantir.conjure.java.api:errors",
109+
"com.palantir.safe-logging:preconditions"
110+
]
111+
},
99112
"javax.ws.rs:javax.ws.rs-api": {
100113
"locked": "2.0.1",
101114
"transitive": [

0 commit comments

Comments
 (0)