Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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,30 @@
/*
* (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.SafeLoggable;

/**
* This class should not be used directly. The parent class for ServiceException and EndpointServiceException.
*/
public abstract class AbstractServiceError extends RuntimeException 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.

nit: Could we name this AbstractServiceException since it's not an Error?

Suggested change
public abstract class AbstractServiceError extends RuntimeException implements SafeLoggable {
public abstract class AbstractServiceException extends RuntimeException implements SafeLoggable {

public abstract ErrorType getErrorType();

protected AbstractServiceError(Throwable cause) {
super(cause);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we not share more implementation than this between the two existing exceptions? Or is the goal to share the minimum to start?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to make it minimal given I'm making this public and don't want people to use this. We only need the error type to update the ServiceExceptionAssert.

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
package com.palantir.conjure.java.api.errors;

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 abstract class EndpointServiceException extends AbstractServiceError {
private static final String EXCEPTION_NAME = "EndpointServiceException";
private final ErrorType errorType;
private final List<Arg<?>> args; // This is an unmodifiable list.
Expand All @@ -52,6 +51,7 @@ public EndpointServiceException(ErrorType errorType, @Nullable Throwable cause,
}

/** The {@link ErrorType} that gave rise to this exception. */
@Override
public final ErrorType getErrorType() {
return errorType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
package com.palantir.conjure.java.api.errors;

import com.palantir.logsafe.Arg;
import com.palantir.logsafe.SafeLoggable;
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 {
public final class ServiceException extends AbstractServiceError {
private static final String EXCEPTION_NAME = "ServiceException";
private final ErrorType errorType;
private final List<Arg<?>> args; // unmodifiable
Expand Down Expand Up @@ -53,6 +52,7 @@ public ServiceException(ErrorType errorType, @Nullable Throwable cause, Arg<?>..
}

/** The {@link ErrorType} that gave rise to this exception. */
@Override
public ErrorType getErrorType() {
return errorType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.palantir.conjure.java.api.testing;

import com.palantir.conjure.java.api.errors.EndpointServiceException;
import com.palantir.conjure.java.api.errors.QosException;
import com.palantir.conjure.java.api.errors.RemoteException;
import com.palantir.conjure.java.api.errors.ServiceException;
Expand All @@ -32,6 +33,10 @@ public static ServiceExceptionAssert assertThat(ServiceException actual) {
return new ServiceExceptionAssert(actual);
}

public static ServiceExceptionAssert assertThat(EndpointServiceException actual) {
return new ServiceExceptionAssert(actual);
}

public static RemoteExceptionAssert assertThat(RemoteException actual) {
return new RemoteExceptionAssert(actual);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.palantir.conjure.java.api.testing;

import com.palantir.conjure.java.api.errors.AbstractServiceError;
import com.palantir.conjure.java.api.errors.ErrorType;
import com.palantir.conjure.java.api.errors.ServiceException;
import com.palantir.logsafe.Arg;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -28,16 +28,17 @@
import org.assertj.core.api.InstanceOfAssertFactory;
import org.assertj.core.util.Throwables;

public class ServiceExceptionAssert extends AbstractThrowableAssert<ServiceExceptionAssert, ServiceException> {
public class ServiceExceptionAssert extends AbstractThrowableAssert<ServiceExceptionAssert, AbstractServiceError> {

private static final InstanceOfAssertFactory<ServiceException, ServiceExceptionAssert> INSTANCE_OF_ASSERT_FACTORY =
new InstanceOfAssertFactory<>(ServiceException.class, ServiceExceptionAssert::new);
private static final InstanceOfAssertFactory<AbstractServiceError, ServiceExceptionAssert>
INSTANCE_OF_ASSERT_FACTORY =
new InstanceOfAssertFactory<>(AbstractServiceError.class, ServiceExceptionAssert::new);

ServiceExceptionAssert(ServiceException actual) {
ServiceExceptionAssert(AbstractServiceError actual) {
super(actual, ServiceExceptionAssert.class);
}

public static InstanceOfAssertFactory<ServiceException, ServiceExceptionAssert> instanceOfAssertFactory() {
public static InstanceOfAssertFactory<AbstractServiceError, ServiceExceptionAssert> instanceOfAssertFactory() {
return INSTANCE_OF_ASSERT_FACTORY;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testAssertThatServiceExceptionThrownBy_failsIfWrongExceptionThrown()
throw new RuntimeException("My message");
}))
.hasMessageContaining(
"com.palantir.conjure.java.api.errors.ServiceException",
"com.palantir.conjure.java.api.errors.AbstractServiceError",
"java.lang.RuntimeException",
"My message");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.palantir.conjure.java.api.errors.EndpointServiceException;
import com.palantir.conjure.java.api.errors.ErrorType;
import com.palantir.conjure.java.api.errors.ServiceException;
import com.palantir.logsafe.Arg;
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.UnsafeArg;
import org.junit.jupiter.api.Test;

public class ServiceExceptionAssertTest {

private static class TestEndpointServiceException extends EndpointServiceException {
TestEndpointServiceException(ErrorType errorType, Arg<?>... safeArgs) {
super(errorType, safeArgs);
}
}

@Test
public void testSanity() {
ErrorType actualType = ErrorType.FAILED_PRECONDITION;
Expand All @@ -35,63 +43,123 @@ public void testSanity() {
.hasType(actualType)
.hasArgs(SafeArg.of("a", "b"), UnsafeArg.of("c", "d"));

Assertions.assertThat(
new TestEndpointServiceException(actualType, SafeArg.of("a", "b"), UnsafeArg.of("c", "d")))
.hasCode(actualType.code())
.hasType(actualType)
.hasArgs(SafeArg.of("a", "b"), UnsafeArg.of("c", "d"));

Assertions.assertThat(new ServiceException(actualType, SafeArg.of("a", "b"), UnsafeArg.of("c", "d")))
.hasCode(actualType.code())
.hasType(actualType)
.hasArgs(UnsafeArg.of("c", "d"), SafeArg.of("a", "b")); // Order doesn't matter

Assertions.assertThat(
new TestEndpointServiceException(actualType, SafeArg.of("a", "b"), UnsafeArg.of("c", "d")))
.hasCode(actualType.code())
.hasType(actualType)
.hasArgs(UnsafeArg.of("c", "d"), SafeArg.of("a", "b")); // Order doesn't matter

Assertions.assertThat(new ServiceException(actualType)).hasNoArgs();
Assertions.assertThat(new TestEndpointServiceException(actualType)).hasNoArgs();

assertThatThrownBy(() ->
Assertions.assertThat(new ServiceException(actualType)).hasCode(ErrorType.Code.INTERNAL))
.isInstanceOf(AssertionError.class)
.hasMessageContaining(
"Expected ErrorType.Code to be %s, but found %s", ErrorType.Code.INTERNAL, actualType.code());
assertThatThrownBy(() -> Assertions.assertThat(new TestEndpointServiceException(actualType))
.hasCode(ErrorType.Code.INTERNAL))
.isInstanceOf(AssertionError.class)
.hasMessageContaining(
"Expected ErrorType.Code to be %s, but found %s", ErrorType.Code.INTERNAL, actualType.code());

assertThatThrownBy(() ->
Assertions.assertThat(new ServiceException(actualType)).hasType(ErrorType.INTERNAL))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected ErrorType to be %s, but found %s", ErrorType.INTERNAL, actualType);
assertThatThrownBy(() -> Assertions.assertThat(new TestEndpointServiceException(actualType))
.hasType(ErrorType.INTERNAL))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected ErrorType to be %s, but found %s", ErrorType.INTERNAL, actualType);

assertThatThrownBy(() -> Assertions.assertThat(new ServiceException(actualType, SafeArg.of("a", "b")))
.hasArgs(SafeArg.of("c", "d")))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected safe args to be {c=d}, but found {a=b}");
assertThatThrownBy(
() -> Assertions.assertThat(new TestEndpointServiceException(actualType, SafeArg.of("a", "b")))
.hasArgs(SafeArg.of("c", "d")))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected safe args to be {c=d}, but found {a=b}");

assertThatThrownBy(() -> Assertions.assertThat(new ServiceException(actualType, UnsafeArg.of("a", "b")))
.hasArgs(UnsafeArg.of("c", "d")))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected unsafe args to be {c=d}, but found {a=b}");
assertThatThrownBy(() -> Assertions.assertThat(
new TestEndpointServiceException(actualType, UnsafeArg.of("a", "b")))
.hasArgs(UnsafeArg.of("c", "d")))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected unsafe args to be {c=d}, but found {a=b}");

assertThatThrownBy(() -> Assertions.assertThat(new ServiceException(actualType, SafeArg.of("a", "b")))
.hasNoArgs())
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected no args, but found {a=b}");
assertThatThrownBy(
() -> Assertions.assertThat(new TestEndpointServiceException(actualType, SafeArg.of("a", "b")))
.hasNoArgs())
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected no args, but found {a=b}");

assertThatThrownBy(() -> Assertions.assertThat(
new ServiceException(actualType, SafeArg.of("a", "b"), UnsafeArg.of("c", "d")))
.hasNoArgs())
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected no args, but found {a=b, c=d}");
assertThatThrownBy(() -> Assertions.assertThat(new TestEndpointServiceException(
actualType, SafeArg.of("a", "b"), UnsafeArg.of("c", "d")))
.hasNoArgs())
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected no args, but found {a=b, c=d}");

Assertions.assertThat(new ServiceException(actualType, UnsafeArg.of("a", "b"), UnsafeArg.of("c", "d")))
.containsArgs(UnsafeArg.of("a", "b"));
Assertions.assertThat(
new TestEndpointServiceException(actualType, UnsafeArg.of("a", "b"), UnsafeArg.of("c", "d")))
.containsArgs(UnsafeArg.of("a", "b"));

// Safety matters
assertThatThrownBy(() -> Assertions.assertThat(
new ServiceException(actualType, SafeArg.of("a", "b"), UnsafeArg.of("c", "d")))
.containsArgs(UnsafeArg.of("a", "b")))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected unsafe args to contain {a=b}, but found {c=d}");
assertThatThrownBy(() -> Assertions.assertThat(new TestEndpointServiceException(
actualType, SafeArg.of("a", "b"), UnsafeArg.of("c", "d")))
.containsArgs(UnsafeArg.of("a", "b")))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected unsafe args to contain {a=b}, but found {c=d}");

assertThatThrownBy(() -> Assertions.assertThat(new ServiceException(actualType, SafeArg.of("a", "b")))
.containsArgs(SafeArg.of("c", "d")))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected safe args to contain {c=d}, but found {a=b}");
assertThatThrownBy(
() -> Assertions.assertThat(new TestEndpointServiceException(actualType, SafeArg.of("a", "b")))
.containsArgs(SafeArg.of("c", "d")))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected safe args to contain {c=d}, but found {a=b}");

assertThatThrownBy(() -> Assertions.assertThat(new ServiceException(actualType, UnsafeArg.of("a", "b")))
.containsArgs(UnsafeArg.of("c", "d")))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected unsafe args to contain {c=d}, but found {a=b}");
assertThatThrownBy(() -> Assertions.assertThat(
new TestEndpointServiceException(actualType, UnsafeArg.of("a", "b")))
.containsArgs(UnsafeArg.of("c", "d")))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("Expected unsafe args to contain {c=d}, but found {a=b}");
}
}