Skip to content

Commit

Permalink
Merge pull request #1515 from InRedikaWB/fix/ThrowOnAnyError
Browse files Browse the repository at this point in the history
fix: throw error if ThrowOnAnyError flag is true
  • Loading branch information
alexeyzimarev committed Oct 23, 2020
2 parents 4cd6eb5 + b87cd97 commit 6e10429
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/RestSharp/Http.Sync.cs
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
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
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
@@ -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));
}
}
}

0 comments on commit 6e10429

Please sign in to comment.