Adds ability to capture HTTP errors #328
Closed
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.
As of v4.0.0 of this gem, we lost the ability to access response headers and response status code. Access to these is critical in safely handling HTTP failures that can happen when reaching out to OpenAI's API.
This introduces a new configuration option
raise_error
that allows users to raise a newOpenAI::HTTP::Error
anytime the response status code is 4xx or 5xx, and provides access to the response'sstatus
,headers
, andbody
on the error instance that is raised.In #327, an approach to resolve this allows forcing a "raw" response to be returned. For the specific case of handling HTTP errors though, this is not a straightforward solution that I would expect as a first-time user of the library.
Since Faraday is used, they have an excellent
RaiseError
middleware (recommended in their docs) that will raise a Faraday::Error for any 4xx or 5xx response status codes. Instances of this error contain aresponse
Hash attribute that include thestatus
,headers
, andbody
of the response. This takes advantage of that error class and wraps it in a library-specific one so that end-users can rescue from this in a convenient way.Some dis-advantages to this solution is that it is coupled with Faraday. However, extending the new
OpenAI::HTTP::Error
class to conform to the same return object for theresponse
accessor (aHash
withstatus
,headers
, andbody
keys) should be fairly straightforward if it is decided to move away from Farday in the future. I have tried my best to avoid any Farday-specific mentions in the README for that reason.Fixes #255
All Submissions: