Skip to content

Commit 3255daa

Browse files
fix: [Orchestration] Added location to server errors (#365)
* fix: [Orchestration] Added location to server errors * location only * reduce coverage for error 500 * Added test
1 parent 1cdd4e0 commit 3255daa

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import lombok.AccessLevel;
99
import lombok.AllArgsConstructor;
1010
import lombok.Value;
11-
import lombok.experimental.Delegate;
1211

1312
/**
1413
* Represents an error response from the OpenAI API.
@@ -20,7 +19,6 @@
2019
@AllArgsConstructor(onConstructor = @__({@JsonCreator}), access = AccessLevel.PROTECTED)
2120
public class OpenAiError implements ClientError {
2221
/** The original error response from the OpenAI API. */
23-
@Delegate(types = {ClientError.class})
2422
ErrorResponse originalResponse;
2523

2624
/**

orchestration/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<coverage.complexity>82%</coverage.complexity>
3535
<coverage.line>93%</coverage.line>
3636
<coverage.instruction>94%</coverage.instruction>
37-
<coverage.branch>76%</coverage.branch>
37+
<coverage.branch>75%</coverage.branch>
3838
<coverage.method>93%</coverage.method>
3939
<coverage.class>100%</coverage.class>
4040
</properties>

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import com.google.common.annotations.Beta;
55
import com.sap.ai.sdk.core.common.ClientError;
66
import com.sap.ai.sdk.orchestration.model.ErrorResponse;
7+
import javax.annotation.Nonnull;
78
import lombok.AccessLevel;
89
import lombok.AllArgsConstructor;
910
import lombok.Value;
10-
import lombok.experimental.Delegate;
1111

1212
/**
1313
* Orchestration error response.
@@ -18,6 +18,17 @@
1818
@Value
1919
@Beta
2020
public class OrchestrationError implements ClientError {
21-
@Delegate(types = {ClientError.class})
2221
ErrorResponse originalResponse;
22+
23+
/**
24+
* Gets the error message from the contained original response.
25+
*
26+
* @return the error message
27+
*/
28+
@Nonnull
29+
public String getMessage() {
30+
return originalResponse.getCode() == 500
31+
? originalResponse.getMessage() + " located in " + originalResponse.getLocation()
32+
: originalResponse.getMessage();
33+
}
2334
}

orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationUnitTest.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,21 @@ void testCompletion() {
126126
assertThat(result.getContent()).isNotEmpty();
127127
}
128128

129+
@Test
130+
void testCompletionError() {
131+
stubFor(
132+
post(urlPathEqualTo("/completion"))
133+
.willReturn(
134+
aResponse()
135+
.withStatus(500)
136+
.withBodyFile("error500Response.json")
137+
.withHeader("Content-Type", "application/json")));
138+
139+
assertThatThrownBy(() -> client.chatCompletion(prompt, config))
140+
.hasMessage(
141+
"Request failed with status 500 Server Error and error message: 'Internal Server Error located in Masking Module - Masking'");
142+
}
143+
129144
@Test
130145
void testGrounding() throws IOException {
131146
stubFor(
@@ -185,9 +200,10 @@ void testGrounding() throws IOException {
185200
"First chunk```Second chunk```Last found chunk");
186201
assertThat(groundingModule.getData()).isEqualTo(groundingData);
187202

188-
final String requestBody = new String(fileLoader.apply("groundingRequest.json").readAllBytes());
189-
verify(
190-
postRequestedFor(urlPathEqualTo("/completion")).withRequestBody(equalToJson(requestBody)));
203+
try (var requestInputStream = fileLoader.apply("groundingRequest.json")) {
204+
final String request = new String(requestInputStream.readAllBytes());
205+
verify(postRequestedFor(urlPathEqualTo("/completion")).withRequestBody(equalToJson(request)));
206+
}
191207
}
192208

193209
@Test
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"request_id": "d647775b-725b-428a-90b7-348cbf30f7f4",
3+
"code": 500,
4+
"message": "Internal Server Error",
5+
"location": "Masking Module - Masking",
6+
"module_results": {
7+
"templating": [
8+
{
9+
"role": "user",
10+
"content": "yo"
11+
}
12+
]
13+
}
14+
}

0 commit comments

Comments
 (0)