Skip to content

Commit

Permalink
Added error handling test
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesDuboisSAP committed Aug 30, 2024
1 parent 58b0bc9 commit 3366c2e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions e2e-test-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
Expand Down
5 changes: 5 additions & 0 deletions foundation-models/openai/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ private Stream<D> parseResponse(@Nonnull final ClassicHttpResponse response)
.peek(
responseLine -> {
if (!responseLine.startsWith("data: ")) {
parseErrorAndThrow(responseLine, new OpenAiClientException());
parseErrorAndThrow(
responseLine,
new OpenAiClientException("Failed to parse response from OpenAI model"));
}
})
.map(
Expand All @@ -78,7 +80,10 @@ private Stream<D> parseResponse(@Nonnull final ClassicHttpResponse response)
try {
return JACKSON.readValue(data, deltaType);
} catch (final IOException e) {
throw new OpenAiClientException("Failed to parse delta message: " + data, e);
log.error(
"Failed to parse the following response from OpenAI model: {}", responseLine);
throw new OpenAiClientException(
"Failed to parse delta message: " + responseLine, e);
}
})
.peek(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@
import java.io.IOException;
import java.util.List;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

@TestInstance(Lifecycle.PER_CLASS)
@WireMockTest
class OpenAiClientTest {
private OpenAiClient client;
Expand Down Expand Up @@ -55,8 +61,19 @@ void apiVersion() {
verify(exactly(2), postRequestedFor(anyUrl()).withoutQueryParam("api-version"));
}

@Test
void errorHandling() {
private Stream<Runnable> chatCompletionCalls() {
return Stream.of(
() -> client.chatCompletion(new OpenAiChatCompletionParameters()),
() ->
client
.streamChatCompletion(new OpenAiChatCompletionParameters())
// the stream needs to be consumed to parse the response
.forEach(System.out::println));
}

@ParameterizedTest
@MethodSource("chatCompletionCalls")
void chatCompletionErrorHandling(@Nonnull final Runnable request) {
final var errorJson =
"""
{ "error": { "code": null, "message": "foo", "type": "invalid stuff" } }
Expand Down Expand Up @@ -91,7 +108,6 @@ void errorHandling() {
.willSetStateTo("4"));
stubFor(post(anyUrl()).inScenario("Errors").whenScenarioStateIs("4").willReturn(noContent()));

final Runnable request = () -> client.chatCompletion(new OpenAiChatCompletionParameters());
final var softly = new SoftAssertions();

softly
Expand Down Expand Up @@ -255,7 +271,7 @@ void embedding() throws IOException {
postRequestedFor(urlPathEqualTo("/embeddings"))
.withRequestBody(
equalToJson("""
{"input":["Hello World"]}""")));
{"input":["Hello World"]}""")));
}
}

Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
Expand Down

0 comments on commit 3366c2e

Please sign in to comment.