janssenr / SendCloudApi.Net

A C#/.net wrapper for the SendCloud API
MIT License
6 stars 5 forks source link

Error handling of HTML response #20

Closed davidkdb closed 2 years ago

davidkdb commented 2 years ago

Hi,

Sometimes Sendcloud returns an HTML error instead of Json.

I have updated the code to take care of this, so now it can return for example: 500 - Page is not working

Can you implement this code?

private async Task HandleResponseError(HttpResponseMessage response)
        {
            string message;
            switch (response.StatusCode)
            {
                case HttpStatusCode.NotFound:
                    message = "Page not found";
                    break;
                default:
                    var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                    try
                    {
                        var result = JsonHelper.Deserialize<SendCloudError>(responseBody, "yyyy-MM-dd HH:mm:ss");
                        message = result.Message;

                        if (!string.IsNullOrEmpty(result.Error?.Message))
                        {
                            message = result.Error.Message;
                        }
                    }
                    catch
                    {
                        // Probably HTML error, return the title of the HTML instead
                        // Locate the <title> tag. In HTML from Sendcloud it seems like it's returning <title >, so we just check for <title
                        string title = "";
                        int titleStart = responseBody.IndexOf("<title",0,StringComparison.OrdinalIgnoreCase);
                        if ( titleStart > -1)
                        {
                            // Set the start index to end of <title>. Locate the >
                            for (int index = titleStart; index < responseBody.Length; index++)
                            {
                                if (responseBody[index] == '>')
                                {
                                    // Set the position to the first char after >
                                    titleStart = index + 1;

                                    // Found the title, get the position of the </title tag
                                    int tagEnd = responseBody.IndexOf("</title", titleStart,StringComparison.OrdinalIgnoreCase);
                                    if (tagEnd > 0)
                                    {
                                        title = responseBody.Substring(titleStart, tagEnd - titleStart);
                                    }
                                    break;
                                }
                            }
                        }

                        if (!string.IsNullOrEmpty(title))
                        {
                            message = title;
                        }
                        else
                        {
                            message = "Sendcloud unknown error";
                        }
                    }

                    break;
            }
            throw new SendCloudException(message);
        }
janssenr commented 2 years ago

I solved it differently. I catch the InternalServerError like the way the PageNotFound is handled.

davidkdb commented 2 years ago

I dont think it's agood idea when we actually have an error message returned to give a fixed error message.

The solution with parsing the actual error message returned is much more informative.