Skip to content

Commit c32d697

Browse files
gatesnbulldozer-bot[bot]
authored andcommitted
Make UnknownRemoteError safe-loggable (#374)
1 parent e49f2f3 commit c32d697

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: improvement
2+
improvement:
3+
description: Make UnknownRemoteException safe-loggable
4+
links:
5+
- https://github.com/palantir/conjure-java-runtime-api/pull/374

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616

1717
package com.palantir.conjure.java.api.errors;
1818

19+
import com.palantir.logsafe.Arg;
20+
import com.palantir.logsafe.SafeArg;
21+
import com.palantir.logsafe.SafeLoggable;
22+
import java.util.Collections;
23+
import java.util.List;
24+
1925
/**
2026
* An exception thrown by an RPC client to indicate remote/server-side failure from a non-remoting server.
2127
*/
22-
public final class UnknownRemoteException extends RuntimeException {
28+
public final class UnknownRemoteException extends RuntimeException implements SafeLoggable {
2329
private static final long serialVersionUID = 1L;
2430

2531
private final int status;
@@ -41,4 +47,14 @@ public UnknownRemoteException(int status, String body) {
4147
this.body = body;
4248
}
4349

50+
@Override
51+
public String getLogMessage() {
52+
return "Failed to parse response body as SerializableError.";
53+
}
54+
55+
@Override
56+
public List<Arg<?>> getArgs() {
57+
return Collections.singletonList(SafeArg.of("status", getStatus()));
58+
}
59+
4460
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.palantir.conjure.java.api.errors;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import com.palantir.logsafe.SafeArg;
22+
import org.junit.jupiter.api.Test;
23+
24+
class UnknownRemoteExceptionTest {
25+
26+
@Test
27+
public void testMessage() {
28+
UnknownRemoteException exception = new UnknownRemoteException(404, "not found");
29+
assertThat(exception.getMessage())
30+
.isEqualTo("Error 404. (Failed to parse response body as SerializableError.)");
31+
assertThat(exception.getLogMessage())
32+
.isEqualTo("Failed to parse response body as SerializableError.");
33+
}
34+
35+
@Test
36+
public void testArgsContainsStatus() {
37+
UnknownRemoteException exception = new UnknownRemoteException(404, "not found");
38+
assertThat(exception.getArgs()).containsOnly(SafeArg.of("status", 404));
39+
}
40+
41+
}

0 commit comments

Comments
 (0)