diff --git a/preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeExceptionData.java b/preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeExceptionData.java new file mode 100644 index 00000000..d04c1d87 --- /dev/null +++ b/preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeExceptionData.java @@ -0,0 +1,85 @@ +/* + * (c) Copyright 2025 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.logsafe.exceptions; + +import com.google.errorprone.annotations.CompileTimeConstant; +import com.palantir.logsafe.Arg; +import com.palantir.logsafe.Safe; +import com.palantir.logsafe.Unsafe; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * Helper to ease the process of defining custom {@link com.palantir.logsafe.SafeLoggable} {@link Throwable throwables}. + */ +@Unsafe +public final class SafeExceptionData { + + private static final SafeExceptionData EMPTY = new SafeExceptionData("", "", List.of()); + + @Safe + @CompileTimeConstant + private final String logMessage; + + @Unsafe + private final String unsafeMessage; + + private final List> arguments; + + /** Returns an empty {@link SafeExceptionData} instance. */ + public static SafeExceptionData of() { + return EMPTY; + } + + public static SafeExceptionData of(@Safe @CompileTimeConstant String logMessage, Arg... arguments) { + return new SafeExceptionData( + logMessage, + SafeExceptions.renderMessage(logMessage, arguments), + Collections.unmodifiableList(Arrays.asList(arguments))); + } + + private SafeExceptionData( + @Safe @CompileTimeConstant String logMessage, @Unsafe String unsafeMessage, List> arguments) { + this.logMessage = logMessage; + this.unsafeMessage = unsafeMessage; + this.arguments = arguments; + } + + @Safe + public String logMessage() { + return logMessage; + } + + @Unsafe + public String unsafeMessage() { + return unsafeMessage; + } + + public List> args() { + return arguments; + } + + @Unsafe + @Override + public String toString() { + return "SafeExceptionData{logMessage='" + + logMessage + '\'' + ", unsafeMessage='" + + unsafeMessage + '\'' + ", arguments=" + + arguments + '}'; + } +} diff --git a/preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeIllegalArgumentException.java b/preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeIllegalArgumentException.java index e54ebbfa..a0eeefe6 100755 --- a/preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeIllegalArgumentException.java +++ b/preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeIllegalArgumentException.java @@ -19,47 +19,41 @@ import com.google.errorprone.annotations.CompileTimeConstant; import com.palantir.logsafe.Arg; import com.palantir.logsafe.SafeLoggable; -import java.util.Arrays; -import java.util.Collections; import java.util.List; import javax.annotation.Nullable; public final class SafeIllegalArgumentException extends IllegalArgumentException implements SafeLoggable { - private final String logMessage; - private final List> arguments; + private final SafeExceptionData data; public SafeIllegalArgumentException() { - super(""); - this.logMessage = ""; - this.arguments = Collections.emptyList(); + this(SafeExceptionData.of(), null); } public SafeIllegalArgumentException(@CompileTimeConstant String message, Arg... arguments) { - super(SafeExceptions.renderMessage(message, arguments)); - this.logMessage = message; - this.arguments = Collections.unmodifiableList(Arrays.asList(arguments)); + this(SafeExceptionData.of(message, arguments), null); } public SafeIllegalArgumentException( @CompileTimeConstant String message, @Nullable Throwable cause, Arg... arguments) { - super(SafeExceptions.renderMessage(message, arguments), cause); - this.logMessage = message; - this.arguments = Collections.unmodifiableList(Arrays.asList(arguments)); + this(SafeExceptionData.of(message, arguments), cause); } public SafeIllegalArgumentException(@Nullable Throwable cause) { - super("", cause); - this.logMessage = ""; - this.arguments = Collections.emptyList(); + this(SafeExceptionData.of(), cause); + } + + private SafeIllegalArgumentException(SafeExceptionData data, @Nullable Throwable cause) { + super(data.unsafeMessage(), cause); + this.data = data; } @Override public String getLogMessage() { - return logMessage; + return data.logMessage(); } @Override public List> getArgs() { - return arguments; + return data.args(); } }