Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -54,6 +54,13 @@ public OpenAIException(String message, int statusCode, String errorCode, String
this.responseBody = responseBody;
}

public OpenAIException(String message, String errorCode, String responseBody) {
super(message);
this.statusCode = null;
this.errorCode = errorCode;
this.responseBody = responseBody;
}

/**
* Factory method to create appropriate exception subclass based on status code.
*
Expand All @@ -64,7 +71,10 @@ public OpenAIException(String message, int statusCode, String errorCode, String
* @return Appropriate exception subclass
*/
public static OpenAIException create(
int statusCode, String message, String errorCode, String responseBody) {
Integer statusCode, String message, String errorCode, String responseBody) {
if (null == statusCode) {
return new OpenAIException(message, errorCode, responseBody);
}
return switch (statusCode) {
case 400 -> new BadRequestException(message, errorCode, responseBody);
case 401 -> new AuthenticationException(message, errorCode, responseBody);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -882,4 +882,35 @@ void testCustomEndpointPathInStreamCall() throws Exception {
recordedRequest.getPath().contains("/v4/chat/completions"),
"Stream path should contain custom endpoint path: " + recordedRequest.getPath());
}

@Test
@DisplayName("Should throw OpenAIException when handle custom endpoint path in stream call")
void testThrowOpenAIExceptionWhenCustomEndpointPathInStreamCall() {
String sseResponse = "";

mockServer.enqueue(
new MockResponse()
.setBody(sseResponse)
.setResponseCode(301)
.setHeader("Content-Type", "text/event-stream"));

OpenAIRequest request =
OpenAIRequest.builder()
.model("gpt-4")
.messages(
List.of(
OpenAIMessage.builder()
.role("user")
.content("Hello")
.build()))
.build();

// Use custom endpoint path for stream call
GenerateOptions options =
GenerateOptions.builder().endpointPath("/v4/chat/completions").build();

assertThrows(
OpenAIException.class,
() -> client.stream(TEST_API_KEY, baseUrl, request, options).collectList().block());
}
}
Loading