-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Only logMessage is redacted.
- Loading branch information
1 parent
89bc462
commit b408ca1
Showing
9 changed files
with
157 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
preconditions/src/main/java/com/palantir/logsafe/exceptions/SafeExceptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* (c) Copyright 2018 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.palantir.logsafe.Arg; | ||
|
||
/** | ||
* {@link SafeExceptions} provides utility functionality for SafeLoggable exception implementations. | ||
*/ | ||
final class SafeExceptions { | ||
private SafeExceptions() {} | ||
|
||
static String renderMessage(String message, Arg<?>... args) { | ||
if (args.length == 0) { | ||
return message; | ||
} | ||
|
||
StringBuilder builder = new StringBuilder(); | ||
builder.append(message).append(": {"); | ||
for (int i = 0; i < args.length; i++) { | ||
Arg<?> arg = args[i]; | ||
if (i > 0) { | ||
builder.append(", "); | ||
} | ||
|
||
builder.append(arg.getName()).append("=").append(arg.getValue()); | ||
} | ||
builder.append("}"); | ||
|
||
return builder.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
preconditions/src/test/java/com/palantir/logsafe/exceptions/SafeExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* (c) Copyright 2018 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 static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.palantir.logsafe.SafeArg; | ||
import com.palantir.logsafe.UnsafeArg; | ||
import org.junit.Test; | ||
|
||
@SuppressWarnings("ThrowableNotThrown") | ||
public class SafeExceptionTest { | ||
|
||
@Test | ||
public void testSafeIllegalArgumentException() { | ||
SafeIllegalArgumentException exception = new SafeIllegalArgumentException("Failure", | ||
SafeArg.of("value", 3), | ||
UnsafeArg.of("user", "akarp")); | ||
assertThat(exception.getMessage()).isEqualTo("Failure: {value=3, user=akarp}"); | ||
assertThat(exception.getLogMessage()).isEqualTo("Failure"); | ||
} | ||
|
||
@Test | ||
public void testSafeIllegalStateException() { | ||
SafeIllegalStateException exception = new SafeIllegalStateException("Failure", | ||
SafeArg.of("value", 3), | ||
UnsafeArg.of("user", "akarp")); | ||
assertThat(exception.getMessage()).isEqualTo("Failure: {value=3, user=akarp}"); | ||
assertThat(exception.getLogMessage()).isEqualTo("Failure"); | ||
} | ||
|
||
@Test | ||
public void testSafeIoException() { | ||
SafeIoException exception = new SafeIoException("Failure", | ||
SafeArg.of("value", 3), | ||
UnsafeArg.of("user", "akarp")); | ||
assertThat(exception.getMessage()).isEqualTo("Failure: {value=3, user=akarp}"); | ||
assertThat(exception.getLogMessage()).isEqualTo("Failure"); | ||
} | ||
|
||
@Test | ||
public void testSafeNullPointerException() { | ||
SafeNullPointerException exception = new SafeNullPointerException("Failure", | ||
SafeArg.of("value", 3), | ||
UnsafeArg.of("user", "akarp")); | ||
assertThat(exception.getMessage()).isEqualTo("Failure: {value=3, user=akarp}"); | ||
assertThat(exception.getLogMessage()).isEqualTo("Failure"); | ||
} | ||
|
||
@Test | ||
public void testSafeRuntimeException() { | ||
SafeRuntimeException exception = new SafeRuntimeException("Failure", | ||
SafeArg.of("value", 3), | ||
UnsafeArg.of("user", "akarp")); | ||
assertThat(exception.getMessage()).isEqualTo("Failure: {value=3, user=akarp}"); | ||
assertThat(exception.getLogMessage()).isEqualTo("Failure"); | ||
} | ||
} |