Skip to content

ExecuteGetAsync<T>() does NOT throw on HTTP error status codes (404) β€” documentation is misleadingΒ #2290

Open
@mstasiuk-x

Description

@mstasiuk-x

The documentation at https://restsharp.dev/docs/advanced/error-handling/ says that:

ThrowOnAnyError = true will cause RestSharp to throw exceptions
on any request failure, including when using methods like:...

However, this is not true in practice. The method does not throw on 404/500 status codes.

To Reproduce


`using System;
using System.Threading.Tasks;
using RestSharp;

class Program
{
    static async Task Main(string[] args)
    {
        var options = new RestClientOptions("https://httpstat.us")
        {
            ThrowOnAnyError = true
        };

        var client = new RestClient(options);
        var request = new RestRequest("/404");

        try
        {
            // From documentation: "πŸ‘‡ will throw if the request fails"
            var response = await client.ExecuteGetAsync<ResponseModel>(request);

            Console.WriteLine("❗ NO EXCEPTION WAS THROWN");
            Console.WriteLine($"Status: {(int)response.StatusCode}");
            Console.WriteLine($"IsSuccessful: {response.IsSuccessful}");
            Console.WriteLine($"Data is null: {response.Data == null}");
        }
        catch (HttpRequestException ex)
        {
            Console.WriteLine("HttpRequestException was thrown!");
            Console.WriteLine(ex.Message);
        }
    }

    public class ResponseModel
    {
        public string? Message { get; set; }
    }
}

`
Expected behavior
Exeption should be thrown

Stack trace
❗ NO EXCEPTION WAS THROWN Status: 404 IsSuccessful: False Data is null: False

Desktop:

  • OS: [Windows 11 PRO]
  • .NET version [e.g. .NET 8]
  • Version [e.g. 112.1.0]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions