Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SerializableError.errorName @Safe by replacing messages with a fixed error name #1054

Open
wants to merge 1 commit into
base: aash/safelog-annotations
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -96,7 +97,7 @@ private String renderUnsafeMessage() {
return builder.toString();
}

@Unsafe
@Safe
@Override
public String getLogMessage() {
return stableMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down