tghamm / Anthropic.SDK

An unofficial C#/.NET SDK for accessing the Anthropic Claude API
https://www.nuget.org/packages/Anthropic.SDK
MIT License
55 stars 10 forks source link

Somewhat swallowing the HttpRequestExceptions #29

Closed nganju98 closed 4 months ago

nganju98 commented 4 months ago

In EndPointBase.cs lines 150-175 the original exceptions are being swallowed somewhat. The http status code of your rethrown exception is null, can you make it the same as the original? Also the original content coming from Anthropic is json parseable. But you write some plaintext in front of the json so it's not parseable any more. Here's an example I got (please set HttpStatusCode to 529!) and your exception handling code below that. HttpRequestException: Error at https://api.anthropic.com/v1/messages (https://api.anthropic.com/v1/messages) with HTTP status code: 529. Content: {"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}

Code:


 }
                catch (Exception e)
                {
                    resultAsString =
                        "Additionally, the following error was thrown when attempting to read the response content: " +
                        e.ToString();
                }
                if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    throw new AuthenticationException(
                        "Anthropic rejected your authorization, most likely due to an invalid API Key. Full API response follows: " +
                        resultAsString);
                }
                else if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError)
                {
                    throw new HttpRequestException(
                        "Anthropic had an internal server error, which can happen occasionally.  Please retry your request.  " +
                        GetErrorMessage(resultAsString, response, url, url));
                }
                else
                {
                    throw new HttpRequestException(GetErrorMessage(resultAsString, response, url, url));
                }
tghamm commented 4 months ago

@nganju98 I've cleaned this up some in my working branch. Now the json parseable message will come through directly in non Auth/500 instances. In Net 6+ you'll also get the statuscode in the request exception, in netstandard 2.0 it's not an option, I had to carve it out.