Skip to content

Commit a23f88c

Browse files
author
Robert Fink
authored
Add explicit SerializableError#errorInstanceId field (#16)
1 parent 9d5d674 commit a23f88c

File tree

5 files changed

+49
-12
lines changed

5 files changed

+49
-12
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,22 @@ public abstract class SerializableError implements Serializable {
5454
@JsonProperty("errorName")
5555
public abstract String errorName();
5656

57+
/**
58+
* A unique identifier for this error instance, typically used to correlate errors displayed in user-facing
59+
* applications with richer backend-level error information. In contrast to {@link #errorCode} and {@link
60+
* #errorName}, the {@link #errorInstanceId} identifies a specific occurrence of an error, not a class of errors. By
61+
* convention, this field is a UUID.
62+
*/
63+
@JsonProperty("errorId")
64+
@Value.Default
65+
@SuppressWarnings("checkstyle:designforextension")
66+
public String errorInstanceId() {
67+
return "";
68+
}
69+
5770
/** A set of parameters that further explain the error. */
5871
public abstract Map<String, String> parameters();
5972

60-
6173
/**
6274
* @deprecated Used by the serialization-mechanism for back-compat only. Do not use.
6375
*/
@@ -90,7 +102,7 @@ public static SerializableError forException(ServiceException exception) {
90102
Builder builder = new Builder()
91103
.errorCode(exception.getErrorType().code().name())
92104
.errorName(exception.getErrorType().name())
93-
.putParameters("errorId", exception.getErrorId());
105+
.errorInstanceId(exception.getErrorInstanceId());
94106
for (Arg<?> arg : exception.getArgs()) {
95107
if (arg.isSafeForLogging()) {
96108
builder.putParameters(arg.getName(), arg.getValue().toString());

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public final class ServiceException extends RuntimeException implements SafeLogg
3030
private final ErrorType errorType;
3131
private final List<Arg<?>> args; // unmodifiable
3232

33-
private final String errorId = UUID.randomUUID().toString();
33+
private final String errorInstanceId = UUID.randomUUID().toString();
3434
private final String safeMessage;
3535
private final String noArgsMessage;
3636

@@ -63,9 +63,9 @@ public ErrorType getErrorType() {
6363
return errorType;
6464
}
6565

66-
/** A unique identifier for this error. */
67-
public String getErrorId() {
68-
return errorId;
66+
/** A unique identifier for (this instance of) this error. */
67+
public String getErrorInstanceId() {
68+
return errorInstanceId;
6969
}
7070

7171
@Override

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,39 @@ public final class RemoteExceptionTest {
2525

2626
@Test
2727
public void testJavaSerialization() {
28+
// With explicit error instance
2829
SerializableError error = new SerializableError.Builder()
2930
.errorCode("errorCode")
3031
.errorName("errorName")
32+
.errorInstanceId("errorId")
3133
.build();
3234
RemoteException expected = new RemoteException(error, 500);
3335
RemoteException actual = SerializationUtils.deserialize(SerializationUtils.serialize(expected));
34-
assertThat(expected).isEqualToComparingFieldByField(actual);
36+
assertThat(actual).isEqualToComparingFieldByField(expected);
37+
38+
// Without error instance
39+
error = new SerializableError.Builder()
40+
.errorCode("errorCode")
41+
.errorName("errorName")
42+
.build();
43+
expected = new RemoteException(error, 500);
44+
actual = SerializationUtils.deserialize(SerializationUtils.serialize(expected));
45+
assertThat(actual).isEqualToComparingFieldByField(expected);
3546
}
3647

3748
@Test
3849
public void testSuperMessage() {
3950
SerializableError error = new SerializableError.Builder()
4051
.errorCode("errorCode")
4152
.errorName("errorName")
53+
.errorInstanceId("errorId")
4254
.build();
4355
assertThat(new RemoteException(error, 500).getMessage()).isEqualTo("RemoteException: errorCode (errorName)");
4456

4557
error = new SerializableError.Builder()
4658
.errorCode("errorCode")
4759
.errorName("errorCode")
60+
.errorInstanceId("errorId")
4861
.build();
4962
assertThat(new RemoteException(error, 500).getMessage()).isEqualTo("RemoteException: errorCode");
5063
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void testExceptionToError() {
4141
SerializableError expected = new SerializableError.Builder()
4242
.errorCode(error.code().name())
4343
.errorName(error.name())
44-
.putParameters("errorId", exception.getErrorId())
44+
.errorInstanceId(exception.getErrorInstanceId())
4545
.putParameters("safeKey", "42")
4646
.build();
4747
assertThat(SerializableError.forException(exception)).isEqualTo(expected);
@@ -50,17 +50,29 @@ public void testExceptionToError() {
5050
@Test
5151
public void testSerializationContainsRedundantParameters() throws Exception {
5252
assertThat(mapper.writeValueAsString(ERROR))
53-
.isEqualTo("{\"errorCode\":\"code\",\"errorName\":\"name\",\"parameters\":{},"
53+
.isEqualTo("{\"errorCode\":\"code\",\"errorName\":\"name\",\"errorId\":\"\",\"parameters\":{},"
54+
+ "\"exceptionClass\":\"code\",\"message\":\"name\"}");
55+
56+
assertThat(mapper.writeValueAsString(
57+
SerializableError.builder().from(ERROR).errorInstanceId("errorId").build()))
58+
.isEqualTo("{\"errorCode\":\"code\",\"errorName\":\"name\",\"errorId\":\"errorId\",\"parameters\":{},"
5459
+ "\"exceptionClass\":\"code\",\"message\":\"name\"}");
5560
}
5661

5762
@Test
58-
public void testDeserializesWithWhenRedundantParamerersAreGiven() throws Exception {
63+
public void testDeserializesWhenRedundantParamerersAreGiven() throws Exception {
5964
String serialized =
6065
"{\"errorCode\":\"code\",\"errorName\":\"name\",\"exceptionClass\":\"code\",\"message\":\"name\"}";
6166
assertThat(deserialize(serialized)).isEqualTo(ERROR);
6267
}
6368

69+
@Test
70+
public void testDeserializesWhenExplicitErrorIdIsGiven() throws Exception {
71+
String serialized = "{\"errorCode\":\"code\",\"errorName\":\"name\",\"errorId\":\"errorId\"}";
72+
assertThat(deserialize(serialized))
73+
.isEqualTo(SerializableError.builder().from(ERROR).errorInstanceId("errorId").build());
74+
}
75+
6476
@Test
6577
public void testDeserializesWithDefaultNamesOnly() throws Exception {
6678
String serialized = "{\"errorCode\":\"code\",\"errorName\":\"name\"}";

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public void testStatus() {
6969

7070
@Test
7171
public void testErrorIdsAreUnique() {
72-
UUID errorId1 = UUID.fromString(new ServiceException(ERROR).getErrorId());
73-
UUID errorId2 = UUID.fromString(new ServiceException(ERROR).getErrorId());
72+
UUID errorId1 = UUID.fromString(new ServiceException(ERROR).getErrorInstanceId());
73+
UUID errorId2 = UUID.fromString(new ServiceException(ERROR).getErrorInstanceId());
7474

7575
assertThat(errorId1).isNotEqualTo(errorId2);
7676
}

0 commit comments

Comments
 (0)