Skip to content
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
@@ -0,0 +1,92 @@
/*
* (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.conjure.java.api.errors;

import com.palantir.logsafe.Arg;
import java.util.List;
import javax.annotation.Nullable;

public abstract class BaseServiceException extends RuntimeException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be public. We don't expect any exceptions outside of ServiceException and EndpointServiceException to use this.


private final ErrorType errorType;

private final List<Arg<?>> args; // unmodifiable

private final String errorInstanceId;

private final String unsafeMessage;
private final String noArgsMessage;

/**
* Creates a new exception for the given error. All {@link com.palantir.logsafe.Arg parameters} are propagated to
* clients; they are serialized via {@link Object#toString}.
*/
BaseServiceException(ErrorType errorType, Arg<?>... parameters) {
this(errorType, null, parameters);
}

/**
* As above, but additionally records the cause of this exception.
*/
BaseServiceException(ErrorType errorType, @Nullable Throwable cause, Arg<?>... args) {
// TODO(rfink): Memoize formatting?
super(cause);

this.errorType = errorType;
this.errorInstanceId = ServiceExceptionUtils.generateErrorInstanceId(cause);

// Note that instantiators cannot mutate List<> args since it comes through copyToList in all code paths.
this.args = ServiceExceptionUtils.arrayToUnmodifiableList(args);

this.unsafeMessage = ServiceExceptionUtils.renderUnsafeMessage(exceptionName(), errorType, args);
this.noArgsMessage = ServiceExceptionUtils.renderNoArgsMessage(exceptionName(), errorType);
}

/**
* The name of the exception. Typically, the class name.
*/
protected abstract String exceptionName();

/**
* The {@link ErrorType} that gave rise to this exception.
*/
public final ErrorType getErrorType() {
return errorType;
}

/**
* A unique identifier for (this instance of) this error.
*/
public final String getErrorInstanceId() {
return errorInstanceId;
}

@Override
public final String getMessage() {
// Including all args here since any logger not configured with safe-logging will log this message.
return unsafeMessage;
}

public final String getLogMessage() {
// Not returning safe args here since the safe-logging framework will log this message + args explicitly.
return noArgsMessage;
}

public final List<Arg<?>> getArgs() {
return args;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,26 @@

import com.palantir.logsafe.Arg;
import com.palantir.logsafe.SafeLoggable;
import java.util.List;
import javax.annotation.Nullable;

/*
* This is identical to ServiceException, but is used in Conjure-generated code to indicate that an exception was thrown
* from a service endpoint.
*/
public abstract class EndpointServiceException extends RuntimeException implements SafeLoggable {
public final class EndpointServiceException extends BaseServiceException implements SafeLoggable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be abstract. EndpointServiceExceptions should not be initialized. They are extended by a specific endpoint associated error (e.g. here)


private static final String EXCEPTION_NAME = "EndpointServiceException";
private final ErrorType errorType;
private final List<Arg<?>> args; // This is an unmodifiable list.
private final String errorInstanceId;
private final String unsafeMessage;
private final String noArgsMessage;

/**
* Creates a new exception for the given error. All {@link com.palantir.logsafe.Arg parameters} are propagated to
* clients.
*/
public EndpointServiceException(ErrorType errorType, Arg<?>... parameters) {
this(errorType, null, parameters);
super(errorType, parameters);
}

/** As above, but additionally records the cause of this exception. */
public EndpointServiceException(ErrorType errorType, @Nullable Throwable cause, Arg<?>... args) {
super(cause);
this.errorInstanceId = ServiceExceptionUtils.generateErrorInstanceId(cause);
this.errorType = errorType;
this.args = ServiceExceptionUtils.arrayToUnmodifiableList(args);
this.unsafeMessage = ServiceExceptionUtils.renderUnsafeMessage(EXCEPTION_NAME, errorType, args);
this.noArgsMessage = ServiceExceptionUtils.renderNoArgsMessage(EXCEPTION_NAME, errorType);
}

/** The {@link ErrorType} that gave rise to this exception. */
public final ErrorType getErrorType() {
return errorType;
}

/** A unique identifier for (this instance of) this error. */
public final String getErrorInstanceId() {
return errorInstanceId;
}

/** A string that includes the exception name, error type, and all arguments irrespective of log-safety. */
@Override
public final String getMessage() {
// Including all args here since any logger not configured with safe-logging will log this message.
return unsafeMessage;
}

/** A string that includes the exception name and error type, without any arguments. */
@Override
public final String getLogMessage() {
return noArgsMessage;
super(errorType, cause, args);
}

/** The list of arguments. */
@Override
public final List<Arg<?>> getArgs() {
return args;
protected String exceptionName() {
return EXCEPTION_NAME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public abstract class SerializableError implements Serializable {
/**
* A fixed code word identifying the type of error. For errors generated from {@link ServiceException}, this
* corresponds to the {@link ErrorType#code} and is part of the service's API surface. Clients are given access to
* the server-side error code via {@link RemoteException#getError} and typically switch&dispatch on the error code
* and/or name.
* the server-side error code via {@link RemoteException#getError} and typically switch and dispatch on the error
* code and/or name.
*/
@JsonProperty("errorCode")
@Value.Default
Expand All @@ -62,9 +62,9 @@ public String errorCode() {
}

/**
* A fixed name identifying the error. For errors generated from {@link ServiceException}, this corresponding to the
* A fixed name identifying the error. For errors generated from {@link ServiceException}, this corresponds to the
* {@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.
* error name via {@link RemoteException#getError} and typically switch and dispatch on the error code and/or name.
*/
@JsonProperty("errorName")
@Value.Default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,62 +21,24 @@
import java.util.List;
import javax.annotation.Nullable;

/** A {@link ServiceException} thrown in server-side code to indicate server-side {@link ErrorType error states}. */
public final class ServiceException extends RuntimeException implements SafeLoggable {
private static final String EXCEPTION_NAME = "ServiceException";
private final ErrorType errorType;
private final List<Arg<?>> args; // unmodifiable
/**
* A {@link ServiceException} thrown in server-side code to indicate server-side {@link ErrorType error states}.
*/
public final class ServiceException extends BaseServiceException implements SafeLoggable {

private final String errorInstanceId;
private final String unsafeMessage;
private final String noArgsMessage;
private static final String EXCEPTION_NAME = "ServiceException";

/**
* Creates a new exception for the given error. All {@link com.palantir.logsafe.Arg parameters} are propagated to
* clients; they are serialized via {@link Object#toString}.
*/
public ServiceException(ErrorType errorType, Arg<?>... parameters) {
this(errorType, null, parameters);
super(errorType, parameters);
}

/** As above, but additionally records the cause of this exception. */
public ServiceException(ErrorType errorType, @Nullable Throwable cause, Arg<?>... args) {
// TODO(rfink): Memoize formatting?
super(cause);

this.errorInstanceId = ServiceExceptionUtils.generateErrorInstanceId(cause);
this.errorType = errorType;
// Note that instantiators cannot mutate List<> args since it comes through copyToList in all code paths.
this.args = ServiceExceptionUtils.arrayToUnmodifiableList(args);
this.unsafeMessage = ServiceExceptionUtils.renderUnsafeMessage(EXCEPTION_NAME, errorType, args);
this.noArgsMessage = ServiceExceptionUtils.renderNoArgsMessage(EXCEPTION_NAME, errorType);
}

/** The {@link ErrorType} that gave rise to this exception. */
public ErrorType getErrorType() {
return errorType;
}

/** A unique identifier for (this instance of) this error. */
public String getErrorInstanceId() {
return errorInstanceId;
}

@Override
public String getMessage() {
// Including all args here since any logger not configured with safe-logging will log this message.
return unsafeMessage;
}

@Override
public String getLogMessage() {
// Not returning safe args here since the safe-logging framework will log this message + args explicitly.
return noArgsMessage;
super(errorType, cause, args);
}

@Override
public List<Arg<?>> getArgs() {
return args;
protected String exceptionName() {
return EXCEPTION_NAME;
}

/**
Expand Down