-
Notifications
You must be signed in to change notification settings - Fork 262
fix: preserve response headers on parsing and stream errors #922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sylvesterkaczmarek
wants to merge
8
commits into
openai:main
Choose a base branch
from
sylvesterkaczmarek:fix/exception-response-headers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9fd1fcd
fix: expose response headers on invalid data errors
sylvesterkaczmarek fe333a8
fix: expose response headers on I/O errors
sylvesterkaczmarek 931de30
fix: retain headers on JSON parse failures
sylvesterkaczmarek 1a3f184
fix: retain headers on streaming I/O failures
sylvesterkaczmarek 3380b3f
test: preserve headers on streaming failures
sylvesterkaczmarek df0e205
test: preserve headers on JSON parse failures
sylvesterkaczmarek d8f3f8b
fix: preserve invalid data exception constructor ABI
sylvesterkaczmarek 45e47a5
fix: preserve I/O exception constructor ABI
sylvesterkaczmarek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 11 additions & 1 deletion
12
openai-java-core/src/main/kotlin/com/openai/errors/OpenAIInvalidDataException.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,15 @@ | ||
| package com.openai.errors | ||
|
|
||
| import com.openai.core.http.Headers | ||
| import java.util.Optional | ||
|
|
||
| class OpenAIInvalidDataException | ||
| @JvmOverloads | ||
| constructor(message: String? = null, cause: Throwable? = null) : OpenAIException(message, cause) | ||
| constructor( | ||
| message: String? = null, | ||
| cause: Throwable? = null, | ||
| private val headers: Headers? = null, | ||
| ) : OpenAIException(message, cause) { | ||
|
|
||
| fun headers(): Optional<Headers> = Optional.ofNullable(headers) | ||
| } | ||
12 changes: 11 additions & 1 deletion
12
openai-java-core/src/main/kotlin/com/openai/errors/OpenAIIoException.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,15 @@ | ||
| package com.openai.errors | ||
|
|
||
| import com.openai.core.http.Headers | ||
| import java.util.Optional | ||
|
|
||
| class OpenAIIoException | ||
| @JvmOverloads | ||
| constructor(message: String? = null, cause: Throwable? = null) : OpenAIException(message, cause) | ||
| constructor( | ||
| message: String? = null, | ||
| cause: Throwable? = null, | ||
| private val headers: Headers? = null, | ||
| ) : OpenAIException(message, cause) { | ||
|
|
||
| fun headers(): Optional<Headers> = Optional.ofNullable(headers) | ||
| } |
39 changes: 39 additions & 0 deletions
39
openai-java-core/src/test/kotlin/com/openai/core/handlers/JsonHandlerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.openai.core.handlers | ||
|
|
||
| import com.fasterxml.jackson.databind.json.JsonMapper | ||
| import com.openai.core.http.Headers | ||
| import com.openai.core.http.HttpResponse | ||
| import com.openai.errors.OpenAIInvalidDataException | ||
| import java.io.InputStream | ||
| import kotlin.test.Test | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.jupiter.api.assertThrows | ||
|
|
||
| internal class JsonHandlerTest { | ||
|
|
||
| @Test | ||
| fun jsonHandler_whenBodyCannotBeRead_exposesResponseHeaders() { | ||
| val headers = Headers.builder().put("x-request-id", "req_123").build() | ||
| val handler = jsonHandler<Map<String, Any>>(JsonMapper.builder().build()) | ||
|
|
||
| val error = | ||
| assertThrows<OpenAIInvalidDataException> { | ||
| handler.handle(httpResponse("{".byteInputStream(), headers)) | ||
| } | ||
|
|
||
| assertThat(error).hasMessage("Error reading response") | ||
| assertThat(error.headers()).contains(headers) | ||
| } | ||
|
|
||
| private fun httpResponse(body: InputStream, headers: Headers): HttpResponse = | ||
| object : HttpResponse { | ||
|
|
||
| override fun statusCode(): Int = 200 | ||
|
|
||
| override fun headers(): Headers = headers | ||
|
|
||
| override fun body(): InputStream = body | ||
|
|
||
| override fun close() {} | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding
headersto the primary constructor changes its synthetic default-argument descriptor;@JvmOverloadspreserves the Java overloads but not that Kotlin ABI. A Kotlin consumer compiled against the previous release usingOpenAIInvalidDataException("message")invokes the old(String, Throwable, int, DefaultConstructorMarker)constructor and will getNoSuchMethodErrorafter upgrading without recompilation. Keep the original two-parameter primary constructor and introduce header support without replacing its default constructor; the identical change inOpenAIIoException.ktneeds the same treatment.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in the latest commits. Both exception classes now keep the original two-parameter primary constructor unchanged and use an internal three-argument secondary constructor only when response headers are available. This preserves the existing Kotlin default-argument constructor ABI while keeping the new optional
headers()accessor.