Skip to content

Commit c6ab27c

Browse files
committed
Reduce complexity of exception factory
1 parent ec016d2 commit c6ab27c

File tree

12 files changed

+42
-96
lines changed

12 files changed

+42
-96
lines changed

core/src/main/java/com/sap/ai/sdk/core/common/ClientException.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ public class ClientException extends RuntimeException {
2424
* used to extract more detailed error information.
2525
*/
2626
@Nullable
27-
@Getter(onMethod_ = @Beta, value = AccessLevel.PROTECTED)
28-
@Setter(onMethod_ = @Beta, value = AccessLevel.PROTECTED)
27+
@Getter(onMethod_ = @Beta, value = AccessLevel.PUBLIC)
28+
@Setter(onMethod_ = @Beta, value = AccessLevel.PUBLIC)
29+
@Accessors(chain = true)
2930
ClientError clientError;
3031

3132
/**

core/src/main/java/com/sap/ai/sdk/core/common/ClientExceptionFactory.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* @param <R> The subtype of {@link ClientError} payload that can be processed by this factory.
1313
*/
1414
@Beta
15+
@FunctionalInterface
1516
public interface ClientExceptionFactory<E extends ClientException, R extends ClientError> {
1617

1718
/**
@@ -22,7 +23,9 @@ public interface ClientExceptionFactory<E extends ClientException, R extends Cli
2223
* @return An instance of the specified {@link ClientException} type
2324
*/
2425
@Nonnull
25-
E build(@Nonnull final String message, @Nullable final Throwable cause);
26+
default E build(@Nonnull final String message, @Nullable final Throwable cause) {
27+
return build(message, null, cause);
28+
}
2629

2730
/**
2831
* Creates an exception with a message and optional cause.
@@ -40,9 +43,10 @@ default E build(@Nonnull final String message) {
4043
* deserialized into a {@link ClientError} object.
4144
*
4245
* @param message A descriptive message for the exception.
43-
* @param clientError The structured {@link ClientError} object deserialized from the response.
46+
* @param clientError The structured {@link ClientError} object deserialized from the response, null if not exist.
47+
* @param cause An optional cause of the exception, can be null if not applicable.
4448
* @return An instance of the specified {@link ClientException} type
4549
*/
4650
@Nonnull
47-
E buildFromClientError(@Nonnull final String message, @Nonnull final R clientError);
51+
E build(@Nonnull final String message, @Nullable final R clientError, @Nullable final Throwable cause);
4852
}

core/src/main/java/com/sap/ai/sdk/core/common/ClientResponseHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public T handleResponse(@Nonnull final ClassicHttpResponse response) throws E {
7979
private T parseSuccess(@Nonnull final ClassicHttpResponse response) throws E {
8080
final HttpEntity responseEntity = response.getEntity();
8181
if (responseEntity == null) {
82-
throw exceptionFactory.build("The HTTP Response is empty", null).setHttpResponse(response);
82+
throw exceptionFactory.build("The HTTP Response is empty").setHttpResponse(response);
8383
}
8484

8585
val content =
@@ -161,7 +161,7 @@ protected void parseErrorResponseAndThrow(
161161
}
162162
final R clientError = maybeClientError.get();
163163
val message = getErrorMessage(httpResponse, clientError.getMessage());
164-
throw exceptionFactory.buildFromClientError(message, clientError).setHttpResponse(httpResponse);
164+
throw exceptionFactory.build(message, clientError, null).setHttpResponse(httpResponse);
165165
}
166166

167167
private static String getErrorMessage(

core/src/test/java/com/sap/ai/sdk/core/common/ClientResponseHandlerTest.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.apache.hc.core5.http.HttpEntity;
1818
import org.apache.hc.core5.http.io.entity.StringEntity;
1919
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
20+
import org.jetbrains.annotations.NotNull;
21+
import org.jetbrains.annotations.Nullable;
2022
import org.junit.jupiter.api.Test;
2123

2224
class ClientResponseHandlerTest {
@@ -33,18 +35,10 @@ static class MyError implements ClientError {
3335
static class MyException extends ClientException {}
3436

3537
static class MyExceptionFactory implements ClientExceptionFactory<MyException, MyError> {
36-
@Nonnull
38+
@NotNull
3739
@Override
38-
public MyException build(@Nonnull String message, Throwable cause) {
39-
return new MyException(message, cause);
40-
}
41-
42-
@Nonnull
43-
@Override
44-
public MyException buildFromClientError(@Nonnull String message, @Nonnull MyError clientError) {
45-
var ex = new MyException(message);
46-
ex.clientError = clientError;
47-
return ex;
40+
public MyException build(@NotNull String message, @Nullable MyError clientError, @Nullable Throwable cause) {
41+
return (MyException) new MyException(message, cause).setClientError(clientError);
4842
}
4943
}
5044

core/src/test/java/com/sap/ai/sdk/core/common/IterableStreamConverterTest.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.apache.hc.core5.http.ContentType;
2424
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
2525
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
26+
import org.jetbrains.annotations.NotNull;
27+
import org.jetbrains.annotations.Nullable;
2628
import org.junit.jupiter.api.DisplayName;
2729
import org.junit.jupiter.api.Test;
2830

@@ -119,19 +121,10 @@ public static class TestClientException extends ClientException {}
119121
static class TestClientExceptionFactory
120122
implements ClientExceptionFactory<TestClientException, ClientError> {
121123

122-
@Nonnull
124+
@NotNull
123125
@Override
124-
public TestClientException build(@Nonnull String message, Throwable cause) {
125-
return new TestClientException(message, cause);
126-
}
127-
128-
@Nonnull
129-
@Override
130-
public TestClientException buildFromClientError(
131-
@Nonnull String message, @Nonnull ClientError clientError) {
132-
TestClientException exception = new TestClientException(message);
133-
exception.clientError = clientError;
134-
return exception;
126+
public TestClientException build(@NotNull String message, @Nullable ClientError clientError, @Nullable Throwable cause) {
127+
return (TestClientException) new TestClientException(message, cause).setClientError(clientError);
135128
}
136129
}
137130
}

foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/OpenAiClientException.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
@StandardException
1212
public class OpenAiClientException extends ClientException {
1313

14-
OpenAiClientException(@Nonnull final String message, @Nonnull final OpenAiError clientError) {
15-
super(message);
16-
setClientError(clientError);
14+
OpenAiClientException(@Nonnull final String message, @Nullable final Throwable cause) {
15+
super(message, cause);
1716
}
1817

1918
/**

foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/OpenAiExceptionFactory.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,11 @@
55
import javax.annotation.Nonnull;
66
import javax.annotation.Nullable;
77

8-
@Beta
98
class OpenAiExceptionFactory implements ClientExceptionFactory<OpenAiClientException, OpenAiError> {
109

1110
@Nonnull
1211
@Override
13-
public OpenAiClientException build(
14-
@Nonnull final String message, @Nullable final Throwable cause) {
15-
return new OpenAiClientException(message, cause);
16-
}
17-
18-
@Nonnull
19-
@Override
20-
public OpenAiClientException buildFromClientError(
21-
@Nonnull final String message, @Nonnull final OpenAiError openAiError) {
22-
return new OpenAiClientException(message, openAiError);
12+
public OpenAiClientException build(@Nonnull String message, @Nullable OpenAiError clientError, @Nullable Throwable cause) {
13+
return (OpenAiClientException) new OpenAiClientException(message, cause).setClientError(clientError);
2314
}
2415
}

orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationChatResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public String getContent() throws OrchestrationFilterException.Output {
4444
if ("content_filter".equals(choice.getFinishReason())) {
4545
final var filterDetails = Try.of(this::getOutputFilteringChoices).getOrElseGet(e -> Map.of());
4646
final var message = "Content filter filtered the output.";
47-
throw new OrchestrationFilterException.Output(message, filterDetails);
47+
throw new OrchestrationFilterException.Output(message).setFilterDetails(filterDetails);
4848
}
4949
return choice.getMessage().getContent();
5050
}

orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private static void throwOnContentFilter(@Nonnull final OrchestrationChatComplet
120120
final var filterDetails =
121121
Try.of(() -> getOutputFilteringChoices(delta)).getOrElseGet(e -> Map.of());
122122
final var message = "Content filter filtered the output.";
123-
throw new OrchestrationFilterException.Output(message, filterDetails);
123+
throw new OrchestrationFilterException.Output(message).setFilterDetails(filterDetails);
124124
}
125125
}
126126

orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationClientException.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212
@StandardException
1313
public class OrchestrationClientException extends ClientException {
1414

15-
OrchestrationClientException(
16-
@Nonnull final String message, @Nonnull final OrchestrationError clientError) {
17-
super(message);
18-
setClientError(clientError);
19-
}
20-
2115
/**
2216
* Retrieves the {@link ErrorResponse} from the orchestration service, if available.
2317
*

0 commit comments

Comments
 (0)