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

Preserve upstream anthropic errors in a wrapper #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pigeonlaser
Copy link
Contributor

@madebywelch

I have a need to consume the raw errors we're getting from Anthropic/Bedrock.

Instead of suppressing the errors and returning the predefined strings, I have created a wrapper class that implements the error interface, but keeps the predefined strings in place to support backwards compatibility. Checking the error string or utilizing errors.Is will remain the same.

However, by utilizing this wrapper, it enables us to use errors.As and extract the upstream error from Anthropic/Bedrock.

Let me know your thoughts.

@@ -22,7 +23,9 @@ func (c *Client) doRequest(request *http.Request) (*http.Response, error) {
}

if response.StatusCode != http.StatusOK {
err = anthropic.MapHTTPStatusCodeToError(response.StatusCode)
defer response.Body.Close()
body, _ := io.ReadAll(response.Body)
Copy link

Choose a reason for hiding this comment

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

Did you want to JSON decode into this? https://docs.anthropic.com/en/api/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.

While the native Anthropic client will return errors in a JSON format, the bedrock client does not. Instead, we get errors that look like:

Bedrock Runtime: InvokeModel, https response error StatusCode: 403, RequestID: ----, api error ExpiredTokenException: The security token included in the request is expired

Given this, I left errResponse as a string to handle all cases.

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.

2 participants