From c6b9cfb069f09bf3e289124101b1ccb10db2fa53 Mon Sep 17 00:00:00 2001 From: Andrew Ash Date: Mon, 6 Nov 2023 17:21:10 -0800 Subject: [PATCH] Make SerializableError.errorName @Safe by replacing messages with a fixed error name --- .../palantir/conjure/java/api/errors/RemoteException.java | 5 +++-- .../palantir/conjure/java/api/errors/SerializableError.java | 3 ++- .../conjure/java/api/errors/SerializableErrorTest.java | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/errors/src/main/java/com/palantir/conjure/java/api/errors/RemoteException.java b/errors/src/main/java/com/palantir/conjure/java/api/errors/RemoteException.java index cbdcb531d..e41e73fad 100644 --- a/errors/src/main/java/com/palantir/conjure/java/api/errors/RemoteException.java +++ b/errors/src/main/java/com/palantir/conjure/java/api/errors/RemoteException.java @@ -17,6 +17,7 @@ package com.palantir.conjure.java.api.errors; import com.palantir.logsafe.Arg; +import com.palantir.logsafe.Safe; import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.SafeLoggable; import com.palantir.logsafe.Unsafe; @@ -32,7 +33,7 @@ public final class RemoteException extends RuntimeException implements SafeLogga private static final String ERROR_CODE = "errorCode"; private static final String ERROR_NAME = "errorName"; - @Unsafe // because errorName is unsafe + @Safe private final String stableMessage; private final SerializableError error; @@ -96,7 +97,7 @@ private String renderUnsafeMessage() { return builder.toString(); } - @Unsafe + @Safe @Override public String getLogMessage() { return stableMessage; diff --git a/errors/src/main/java/com/palantir/conjure/java/api/errors/SerializableError.java b/errors/src/main/java/com/palantir/conjure/java/api/errors/SerializableError.java index 89907b4b0..f8156c580 100644 --- a/errors/src/main/java/com/palantir/conjure/java/api/errors/SerializableError.java +++ b/errors/src/main/java/com/palantir/conjure/java/api/errors/SerializableError.java @@ -65,11 +65,12 @@ public String errorCode() { * {@link ErrorType#name} and is part of the service's API surface. Clients are given access to the service-side * error name via {@link RemoteException#getError} and typically switch&dispatch on the error code and/or name. */ - @Unsafe // because message is unsafe + @Safe @JsonProperty("errorName") @Value.Default public String errorName() { return getMessage() + .map(_unsafeMessage -> "Default:EmptyErrorNameWithLegacyMessageUsage") .orElseThrow(() -> new SafeIllegalStateException("Expected either 'errorName' or 'message' to be set")); } diff --git a/errors/src/test/java/com/palantir/conjure/java/api/errors/SerializableErrorTest.java b/errors/src/test/java/com/palantir/conjure/java/api/errors/SerializableErrorTest.java index 791c11752..4a23c86e3 100644 --- a/errors/src/test/java/com/palantir/conjure/java/api/errors/SerializableErrorTest.java +++ b/errors/src/test/java/com/palantir/conjure/java/api/errors/SerializableErrorTest.java @@ -171,13 +171,13 @@ public void testSerDeRoundTripDropsMessage() throws Exception { } @Test - public void testLegacyMessageUsedAsErrorNameWhenNoErrorNameIsSet() { + public void testLegacyMessageWhenNoErrorNameIsSetUsesDefaultErrorName() { SerializableError error = SerializableError.builder() .errorCode("errorCode") .message("the secret is 42") .build(); - assertThat(error.errorName()).isEqualTo("the secret is 42"); + assertThat(error.errorName()).isEqualTo("Default:EmptyErrorNameWithLegacyMessageUsage"); } @Test