Skip to content
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

maintain underlying error structs to allow for type conversion #293

Merged
merged 6 commits into from
May 3, 2023

Conversation

qhenkart
Copy link
Contributor

@qhenkart qhenkart commented May 1, 2023

Resolves #292, #288

Maintains underlying error structs to allow consumers to utilize type conversion and create a defensive structure as defined by the Open AI error handling documentation.

It also slightly modifies the Error() functions to that they are responsible for converting the struct into an informational string versus just a fragment of its parent

Modifies only a single test that was missing a colon when all of the other tests have colons (including the RequestError Error function that was missing the colon that the other Error function included

@codecov
Copy link

codecov bot commented May 1, 2023

Codecov Report

Merging #293 (47cac6c) into master (af9ff51) will increase coverage by 0.52%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #293      +/-   ##
==========================================
+ Coverage   92.43%   92.96%   +0.52%     
==========================================
  Files          22       22              
  Lines         661      668       +7     
==========================================
+ Hits          611      621      +10     
+ Misses         36       35       -1     
+ Partials       14       12       -2     
Impacted Files Coverage Δ
client.go 91.34% <100.00%> (+3.34%) ⬆️
error.go 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

@sashabaranov
Copy link
Owner

@qhenkart thank you for the PR! It's a bit hard for me to follow all the error cases here — could you please either add a test or code sample clearly demonstrating the benefits of this approach?

client.go Outdated
HTTPStatusCode: resp.StatusCode,
Err: err,
}
if errRes.Error != nil {
reqErr.Err = errRes.Error
reqErr.Err = fmt.Errorf(errRes.Error.Message)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't we lose the ability to use errors.Is and errors.As here? https://pkg.go.dev/errors

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right. I have updated the code and the error message to cover this case and still match the expected error result illustrated in the existing tests

@qhenkart
Copy link
Contributor Author

qhenkart commented May 2, 2023

@sashabaranov I have added a test to ensure type conversion as well as updated the README as requested in the linked issue to provide guidance on how to handle errors utilizing proper error package syntax.

README.md Outdated

Other examples:
Open-AI maintains clear documentation on how to [handle API errors](https://platform.openai.com/docs/guides/error-codes/api-errors)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we please wrap this into <details> tag as it is done below?

errRes.Error.HTTPStatusCode = resp.StatusCode
return fmt.Errorf("error, status code: %d, message: %w", resp.StatusCode, errRes.Error)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the previous version was working with the errors.As due to %w usage

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes you are right. I ran into the issue because I was using type conversion instead of errors.As. I suppose the extra information in the documentation would have negated the need for this PR. However I still think it is an improvement for the error type to handle its own messaging and formatting. Also it doesn't hurt to also allow type conversion even if it is no longer the idiomatic methodology

README.md Outdated

example:
```
e := &APIError{}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it should be openai.APIError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

malformed Error handling contradicts official documentation instruction
2 participants