Skip to content

Commit d46dd70

Browse files
authored
Add AbstractSerializableError and make RemoteException non-final (#1415)
Add `AbstractSerializableError` and make `RemoteException` non-final
1 parent 9d37a83 commit d46dd70

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* (c) Copyright 2025 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+
/**
20+
* Base class for serializable Conjure errors with generic parameter type support.
21+
* <p>
22+
* This abstract class represents serialized Conjure errors. It supports generic parameter types to allow for
23+
* deserialization of custom error parameters.
24+
* <p>
25+
* Usage example where {@code CustomErrorParameters} is a user-defined class representing the error parameters:
26+
* <pre>
27+
* {@code
28+
* class CustomError extends AbstractSerializableError<CustomErrorParameters> {
29+
* @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
30+
* CustomError(
31+
* @JsonProperty("errorCode") String errorCode,
32+
* @JsonProperty("errorName") String errorName,
33+
* @JsonProperty("errorInstanceId") String errorInstanceId,
34+
* @JsonProperty("parameters") CustomErrorParameters parameters) {
35+
* super(errorCode, errorName, errorInstanceId, parameters);
36+
* }
37+
* }
38+
* }
39+
* </pre>
40+
*/
41+
public abstract class AbstractSerializableError<T> {
42+
private final String errorCode;
43+
private final String errorName;
44+
private final String errorInstanceId;
45+
private final T parameters;
46+
47+
public final String errorCode() {
48+
return errorCode;
49+
}
50+
51+
public final String errorName() {
52+
return errorName;
53+
}
54+
55+
public final String errorInstanceId() {
56+
return errorInstanceId;
57+
}
58+
59+
public final T parameters() {
60+
return parameters;
61+
}
62+
63+
protected AbstractSerializableError(String errorCode, String errorName, String errorInstanceId, T parameters) {
64+
this.errorCode = errorCode;
65+
this.errorName = errorName;
66+
this.errorInstanceId = errorInstanceId;
67+
this.parameters = parameters;
68+
}
69+
}

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@
2323
import java.util.Collections;
2424
import java.util.List;
2525

26-
/** An exception thrown by an RPC client to indicate remote/server-side failure. */
27-
public final class RemoteException extends RuntimeException implements SafeLoggable {
26+
/**
27+
* An exception thrown by an RPC client to indicate remote/server-side failure.
28+
* <p>
29+
* WARNING: Users should not subclass this exception.
30+
* This class is not final to allow Conjure-Java to extend it and create custom RemoteExceptions.
31+
* */
32+
public class RemoteException extends RuntimeException implements SafeLoggable {
2833
private static final long serialVersionUID = 1L;
2934
private static final String ERROR_INSTANCE_ID = "errorInstanceId";
3035
private static final String ERROR_CODE = "errorCode";
@@ -60,6 +65,11 @@ public RemoteException(SerializableError error, int status) {
6065
SafeArg.of(ERROR_CODE, error.errorCode())));
6166
}
6267

68+
/**
69+
* Returns a message that includes the error code, name, instance ID and parameters (if any).
70+
* <p>
71+
* This may contain sensitive information and should not be exposed to external callers.
72+
*/
6373
@Override
6474
public String getMessage() {
6575
// This field is not used in most environments so the cost of computation may be avoided.
@@ -89,11 +99,21 @@ private String renderUnsafeMessage() {
8999
return builder.toString();
90100
}
91101

102+
/**
103+
* Returns a stable message for this exception that does not include parameters.
104+
* <p>
105+
* This message is safe to expose to external callers and will not expose sensitive information.
106+
*/
92107
@Override
93108
public String getLogMessage() {
94109
return stableMessage;
95110
}
96111

112+
/**
113+
* Returns the safe arguments associated with this exception.
114+
* <p>
115+
* The arguments include the error instance ID, error name, and error code.
116+
*/
97117
@Override
98118
public List<Arg<?>> getArgs() {
99119
// RemoteException explicitly does not support arguments because they have already been recorded

0 commit comments

Comments
 (0)