Skip to content

fix: throw error if ThrowOnAnyError flag is true #1515

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

Merged
merged 1 commit into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/RestSharp/Http.Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ HttpResponse ExecuteRequest(string httpMethod, Action<HttpWebRequest> prepareReq
}
catch (Exception ex)
{
if (ThrowOnAnyError)
throw;

return ExtractErrorResponse(ex);
}

Expand Down
2 changes: 2 additions & 0 deletions src/RestSharp/Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ static void AddRange(HttpWebRequest r, string range)
/// <inheritdoc />
public Action<HttpWebRequest>? WebRequestConfigurator { get; set; }

public bool ThrowOnAnyError { get; set; }

[Obsolete]
public static IHttp Create() => new Http();

Expand Down
3 changes: 2 additions & 1 deletion src/RestSharp/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ IHttp ConfigureHttp(IRestRequest request)
CookieContainer = CookieContainer,
AutomaticDecompression = AutomaticDecompression,
WebRequestConfigurator = WebRequestConfigurator,
Encode = Encode
Encode = Encode,
ThrowOnAnyError = ThrowOnAnyError,
};

var requestParameters = new List<Parameter>();
Expand Down
11 changes: 10 additions & 1 deletion test/RestSharp.Tests/RestRequestTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using System.Net;
using NUnit.Framework;

namespace RestSharp.Tests
{
Expand Down Expand Up @@ -26,5 +27,13 @@ public void RestRequest_Test_Already_Encoded()
Assert.AreEqual("notencoded", request.Parameters[1].Value);
Assert.AreEqual(ParameterType.QueryStringWithoutEncode, request.Parameters[1].Type);
}

[Test]
public void RestRequest_Fail_On_Exception()
{
var req = new RestRequest("nonexisting");
var client = new RestClient("http://localhost:12345") { ThrowOnAnyError = true };
Assert.Throws<WebException>(() => client.Execute(req));
}
}
}