Skip to content

Commit a53de59

Browse files
committed
Fix compilation
1 parent c6ab27c commit a53de59

File tree

10 files changed

+94
-43
lines changed

10 files changed

+94
-43
lines changed

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

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.sap.ai.sdk.core.common;
22

33
import com.google.common.annotations.Beta;
4+
import javax.annotation.Nonnull;
45
import javax.annotation.Nullable;
56
import lombok.AccessLevel;
67
import lombok.Getter;
7-
import lombok.Setter;
8-
import lombok.experimental.Accessors;
98
import lombok.experimental.StandardException;
109
import org.apache.hc.core5.http.ClassicHttpRequest;
1110
import org.apache.hc.core5.http.ClassicHttpResponse;
@@ -25,9 +24,7 @@ public class ClientException extends RuntimeException {
2524
*/
2625
@Nullable
2726
@Getter(onMethod_ = @Beta, value = AccessLevel.PUBLIC)
28-
@Setter(onMethod_ = @Beta, value = AccessLevel.PUBLIC)
29-
@Accessors(chain = true)
30-
ClientError clientError;
27+
private ClientError clientError;
3128

3229
/**
3330
* The original HTTP response that caused this exception, if available.
@@ -36,9 +33,7 @@ public class ClientException extends RuntimeException {
3633
*/
3734
@Nullable
3835
@Getter(onMethod_ = @Beta, value = AccessLevel.PUBLIC)
39-
@Setter(onMethod_ = @Beta, value = AccessLevel.PUBLIC)
40-
@Accessors(chain = true)
41-
ClassicHttpResponse httpResponse;
36+
private ClassicHttpResponse httpResponse;
4237

4338
/**
4439
* The original HTTP request that caused this exception, if available.
@@ -47,7 +42,52 @@ public class ClientException extends RuntimeException {
4742
*/
4843
@Nullable
4944
@Getter(onMethod_ = @Beta, value = AccessLevel.PUBLIC)
50-
@Setter(onMethod_ = @Beta, value = AccessLevel.PUBLIC)
51-
@Accessors(chain = true)
52-
ClassicHttpRequest httpRequest;
45+
private ClassicHttpRequest httpRequest;
46+
47+
/**
48+
* Sets the original HTTP request that caused this exception.
49+
*
50+
* @param clientError the original structured error payload received from the remote service, can
51+
* be null if not available.
52+
* @return the current instance of {@link ClientException} with the changed ClientError data
53+
* @param <T> the type of the exception, typically a subclass of {@link ClientException}
54+
*/
55+
@SuppressWarnings("unchecked")
56+
@Nonnull
57+
public <T extends ClientException> T setClientError(@Nullable final ClientError clientError) {
58+
this.clientError = clientError;
59+
return (T) this;
60+
}
61+
62+
/**
63+
* Sets the original HTTP request that caused this exception.
64+
*
65+
* @param httpResponse the original HTTP response that caused this exception, can be null if not
66+
* available.
67+
* @return the current instance of {@link ClientException} with the changed HTTP response
68+
* @param <T> the type of the exception, typically a subclass of {@link ClientException}
69+
*/
70+
@SuppressWarnings("unchecked")
71+
@Nonnull
72+
public <T extends ClientException> T setHttpResponse(
73+
@Nullable final ClassicHttpResponse httpResponse) {
74+
this.httpResponse = httpResponse;
75+
return (T) this;
76+
}
77+
78+
/**
79+
* Sets the original HTTP request that caused this exception.
80+
*
81+
* @param httpRequest the original HTTP request that caused this exception, can be null if not
82+
* available.
83+
* @return the current instance of {@link ClientException} with the changed HTTP request
84+
* @param <T> the type of the exception, typically a subclass of {@link ClientException}
85+
*/
86+
@SuppressWarnings("unchecked")
87+
@Nonnull
88+
public <T extends ClientException> T setHttpRequest(
89+
@Nullable final ClassicHttpRequest httpRequest) {
90+
this.httpRequest = httpRequest;
91+
return (T) this;
92+
}
5393
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ default E build(@Nonnull final String message) {
4343
* deserialized into a {@link ClientError} object.
4444
*
4545
* @param message A descriptive message for the exception.
46-
* @param clientError The structured {@link ClientError} object deserialized from the response, null if not exist.
46+
* @param clientError The structured {@link ClientError} object deserialized from the response,
47+
* null if not exist.
4748
* @param cause An optional cause of the exception, can be null if not applicable.
4849
* @return An instance of the specified {@link ClientException} type
4950
*/
5051
@Nonnull
51-
E build(@Nonnull final String message, @Nullable final R clientError, @Nullable final Throwable cause);
52+
E build(
53+
@Nonnull final String message,
54+
@Nullable final R clientError,
55+
@Nullable final Throwable cause);
5256
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
import com.fasterxml.jackson.core.JsonParseException;
1111
import java.io.IOException;
1212
import javax.annotation.Nonnull;
13+
import javax.annotation.Nullable;
1314
import lombok.Data;
1415
import lombok.SneakyThrows;
1516
import lombok.experimental.StandardException;
1617
import org.apache.hc.core5.http.ContentType;
1718
import org.apache.hc.core5.http.HttpEntity;
1819
import org.apache.hc.core5.http.io.entity.StringEntity;
1920
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
20-
import org.jetbrains.annotations.NotNull;
21-
import org.jetbrains.annotations.Nullable;
2221
import org.junit.jupiter.api.Test;
2322

2423
class ClientResponseHandlerTest {
@@ -35,10 +34,13 @@ static class MyError implements ClientError {
3534
static class MyException extends ClientException {}
3635

3736
static class MyExceptionFactory implements ClientExceptionFactory<MyException, MyError> {
38-
@NotNull
37+
@Nonnull
3938
@Override
40-
public MyException build(@NotNull String message, @Nullable MyError clientError, @Nullable Throwable cause) {
41-
return (MyException) new MyException(message, cause).setClientError(clientError);
39+
public MyException build(
40+
@Nonnull final String message,
41+
@Nullable final MyError clientError,
42+
@Nullable final Throwable cause) {
43+
return new MyException(message, cause).setClientError(clientError);
4244
}
4345
}
4446

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818
import java.nio.charset.StandardCharsets;
1919
import java.util.concurrent.atomic.AtomicInteger;
2020
import javax.annotation.Nonnull;
21+
import javax.annotation.Nullable;
2122
import lombok.SneakyThrows;
2223
import lombok.experimental.StandardException;
2324
import org.apache.hc.core5.http.ContentType;
2425
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
2526
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
26-
import org.jetbrains.annotations.NotNull;
27-
import org.jetbrains.annotations.Nullable;
2827
import org.junit.jupiter.api.DisplayName;
2928
import org.junit.jupiter.api.Test;
3029

@@ -121,10 +120,13 @@ public static class TestClientException extends ClientException {}
121120
static class TestClientExceptionFactory
122121
implements ClientExceptionFactory<TestClientException, ClientError> {
123122

124-
@NotNull
123+
@Nonnull
125124
@Override
126-
public TestClientException build(@NotNull String message, @Nullable ClientError clientError, @Nullable Throwable cause) {
127-
return (TestClientException) new TestClientException(message, cause).setClientError(clientError);
125+
public TestClientException build(
126+
@Nonnull final String message,
127+
@Nullable final ClientError clientError,
128+
@Nullable final Throwable cause) {
129+
return new TestClientException(message, cause).setClientError(clientError);
128130
}
129131
}
130132
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
import com.google.common.annotations.Beta;
44
import com.sap.ai.sdk.core.common.ClientException;
55
import com.sap.ai.sdk.foundationmodels.openai.generated.model.ErrorResponse;
6-
import javax.annotation.Nonnull;
76
import javax.annotation.Nullable;
87
import lombok.experimental.StandardException;
98

109
/** Generic exception for errors occurring when using OpenAI foundation models. */
1110
@StandardException
1211
public class OpenAiClientException extends ClientException {
1312

14-
OpenAiClientException(@Nonnull final String message, @Nullable final Throwable cause) {
15-
super(message, cause);
16-
}
17-
1813
/**
1914
* Retrieves the {@link ErrorResponse} from the OpenAI service, if available.
2015
*
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.sap.ai.sdk.foundationmodels.openai;
22

3-
import com.google.common.annotations.Beta;
43
import com.sap.ai.sdk.core.common.ClientExceptionFactory;
54
import javax.annotation.Nonnull;
65
import javax.annotation.Nullable;
@@ -9,7 +8,10 @@ class OpenAiExceptionFactory implements ClientExceptionFactory<OpenAiClientExcep
98

109
@Nonnull
1110
@Override
12-
public OpenAiClientException build(@Nonnull String message, @Nullable OpenAiError clientError, @Nullable Throwable cause) {
13-
return (OpenAiClientException) new OpenAiClientException(message, cause).setClientError(clientError);
11+
public OpenAiClientException build(
12+
@Nonnull final String message,
13+
@Nullable final OpenAiError clientError,
14+
@Nullable final Throwable cause) {
15+
return new OpenAiClientException(message, cause).setClientError(clientError);
1416
}
1517
}

foundation-models/openai/src/test/java/com/sap/ai/sdk/foundationmodels/openai/BaseOpenAiClientTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static com.github.tomakehurst.wiremock.client.WireMock.serverError;
1111
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
1212
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
13+
import static org.assertj.core.api.Assertions.assertThat;
1314
import static org.mockito.ArgumentMatchers.any;
1415
import static org.mockito.Mockito.doReturn;
1516
import static org.mockito.Mockito.mock;
@@ -118,8 +119,7 @@ static void assertForErrorHandling(@Nonnull final Runnable request) {
118119
.describedAs("Server errors should be handled")
119120
.isInstanceOf(OpenAiClientException.class)
120121
.hasMessageContaining("500")
121-
.extracting(e -> ((OpenAiClientException) e).getHttpResponse())
122-
.isNotNull();
122+
.satisfies(e -> assertThat(((OpenAiClientException) e).getHttpResponse()).isNotNull());
123123

124124
softly
125125
.assertThatThrownBy(request::run)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.sap.ai.sdk.core.common.ClientException;
55
import com.sap.ai.sdk.orchestration.model.ErrorResponse;
66
import java.util.Optional;
7-
import javax.annotation.Nonnull;
87
import javax.annotation.Nullable;
98
import lombok.experimental.StandardException;
109

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.sap.ai.sdk.orchestration;
22

3-
import com.google.common.annotations.Beta;
43
import com.sap.ai.sdk.core.common.ClientExceptionFactory;
54
import com.sap.ai.sdk.orchestration.model.ErrorResponse;
65
import com.sap.ai.sdk.orchestration.model.GenericModuleResult;
@@ -16,18 +15,24 @@ class OrchestrationExceptionFactory
1615

1716
@Nonnull
1817
@Override
19-
public OrchestrationClientException build(@Nonnull String message, @Nullable OrchestrationError clientError, @Nullable Throwable cause) {
20-
final var inputFilterDetails = extractInputFilterDetails(clientError);
21-
if (!inputFilterDetails.isEmpty()) {
22-
return (OrchestrationClientException) new OrchestrationFilterException.Input(message,cause).setFilterDetails(inputFilterDetails).setClientError(clientError);
23-
}
24-
return (OrchestrationClientException) new OrchestrationClientException(message, cause).setClientError(clientError);
18+
public OrchestrationClientException build(
19+
@Nonnull final String message,
20+
@Nullable final OrchestrationError clientError,
21+
@Nullable final Throwable cause) {
22+
final var inputFilterDetails = extractInputFilterDetails(clientError);
23+
if (!inputFilterDetails.isEmpty()) {
24+
return new OrchestrationFilterException.Input(message, cause)
25+
.setFilterDetails(inputFilterDetails)
26+
.setClientError(clientError);
27+
}
28+
return new OrchestrationClientException(message, cause).setClientError(clientError);
2529
}
2630

2731
@SuppressWarnings("unchecked")
2832
@Nonnull
2933
private Map<String, Object> extractInputFilterDetails(@Nullable final OrchestrationError error) {
30-
return Optional.ofNullable(error).map(OrchestrationError::getErrorResponse)
34+
return Optional.ofNullable(error)
35+
.map(OrchestrationError::getErrorResponse)
3136
.map(ErrorResponse::getModuleResults)
3237
.map(ModuleResults::getInputFiltering)
3338
.map(GenericModuleResult::getData)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public class OrchestrationFilterException extends OrchestrationClientException {
2424
/** Details about the filters that caused the exception. */
2525
@Accessors(chain = true)
2626
@Setter(AccessLevel.PACKAGE)
27-
@Getter @Nonnull protected Map<String, Object> filterDetails = Map.of();
27+
@Getter
28+
@Nonnull
29+
protected Map<String, Object> filterDetails = Map.of();
2830

2931
/**
3032
* Retrieves LlamaGuard 3.8b details from {@code filterDetails}, if present.

0 commit comments

Comments
 (0)