Skip to content

Commit 410cea8

Browse files
authored
Explicit log safety annotations in ServiceException (#1497)
Explicit log safety annotations in ServiceException
1 parent a81c1bc commit 410cea8

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package com.palantir.conjure.java.api.errors;
1818

1919
import com.palantir.logsafe.Arg;
20+
import com.palantir.logsafe.Safe;
2021
import com.palantir.logsafe.SafeLoggable;
22+
import com.palantir.logsafe.Unsafe;
2123
import java.util.List;
2224
import javax.annotation.Nullable;
2325

@@ -27,8 +29,13 @@ public final class ServiceException extends RuntimeException implements SafeLogg
2729
private final ErrorType errorType;
2830
private final List<Arg<?>> args; // unmodifiable
2931

32+
@Safe
3033
private final String errorInstanceId;
34+
35+
@Unsafe
3136
private final String unsafeMessage;
37+
38+
@Safe
3239
private final String noArgsMessage;
3340

3441
/**
@@ -58,16 +65,19 @@ public ErrorType getErrorType() {
5865
}
5966

6067
/** A unique identifier for (this instance of) this error. */
68+
@Safe
6169
public String getErrorInstanceId() {
6270
return errorInstanceId;
6371
}
6472

73+
@Unsafe
6574
@Override
6675
public String getMessage() {
6776
// Including all args here since any logger not configured with safe-logging will log this message.
6877
return unsafeMessage;
6978
}
7079

80+
@Safe
7181
@Override
7282
public String getLogMessage() {
7383
// Not returning safe args here since the safe-logging framework will log this message + args explicitly.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.palantir.conjure.java.api.errors;
1818

1919
import com.palantir.logsafe.Arg;
20+
import com.palantir.logsafe.Safe;
21+
import com.palantir.logsafe.Unsafe;
2022
import com.palantir.tritium.ids.UniqueIds;
2123
import java.util.ArrayList;
2224
import java.util.Collections;
@@ -61,6 +63,7 @@ static <T> List<T> arrayToUnmodifiableList(T[] elements) {
6163
* @param args the arguments to be included in the message
6264
* @return a message string that includes the exception name, error type, and arguments
6365
*/
66+
@Unsafe
6467
static String renderUnsafeMessage(String exceptionName, ErrorType errorType, Arg<?>... args) {
6568
String message = renderNoArgsMessage(exceptionName, errorType);
6669

@@ -94,6 +97,7 @@ static String renderUnsafeMessage(String exceptionName, ErrorType errorType, Arg
9497
* @param errorType the error type the exception represents
9598
* @return a message string
9699
*/
100+
@Safe
97101
static String renderNoArgsMessage(String exceptionName, ErrorType errorType) {
98102
return exceptionName + ": " + errorType.code() + " (" + errorType.name() + ")";
99103
}
@@ -104,6 +108,7 @@ static String renderNoArgsMessage(String exceptionName, ErrorType errorType) {
104108
* suppressed causes}.
105109
*/
106110
// VisibleForTesting
111+
@Safe
107112
static String generateErrorInstanceId(@Nullable Throwable cause) {
108113
return generateErrorInstanceId(cause, Collections.newSetFromMap(new IdentityHashMap<>()));
109114
}

0 commit comments

Comments
 (0)