Skip to content

Commit 6e10429

Browse files
Merge pull request restsharp#1515 from InRedikaWB/fix/ThrowOnAnyError
fix: throw error if ThrowOnAnyError flag is true
2 parents 4cd6eb5 + b87cd97 commit 6e10429

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

src/RestSharp/Http.Sync.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ HttpResponse ExecuteRequest(string httpMethod, Action<HttpWebRequest> prepareReq
118118
}
119119
catch (Exception ex)
120120
{
121+
if (ThrowOnAnyError)
122+
throw;
123+
121124
return ExtractErrorResponse(ex);
122125
}
123126

src/RestSharp/Http.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ static void AddRange(HttpWebRequest r, string range)
229229
/// <inheritdoc />
230230
public Action<HttpWebRequest>? WebRequestConfigurator { get; set; }
231231

232+
public bool ThrowOnAnyError { get; set; }
233+
232234
[Obsolete]
233235
public static IHttp Create() => new Http();
234236

src/RestSharp/RestClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ IHttp ConfigureHttp(IRestRequest request)
421421
CookieContainer = CookieContainer,
422422
AutomaticDecompression = AutomaticDecompression,
423423
WebRequestConfigurator = WebRequestConfigurator,
424-
Encode = Encode
424+
Encode = Encode,
425+
ThrowOnAnyError = ThrowOnAnyError,
425426
};
426427

427428
var requestParameters = new List<Parameter>();

test/RestSharp.Tests/RestRequestTests.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NUnit.Framework;
1+
using System.Net;
2+
using NUnit.Framework;
23

34
namespace RestSharp.Tests
45
{
@@ -26,5 +27,13 @@ public void RestRequest_Test_Already_Encoded()
2627
Assert.AreEqual("notencoded", request.Parameters[1].Value);
2728
Assert.AreEqual(ParameterType.QueryStringWithoutEncode, request.Parameters[1].Type);
2829
}
30+
31+
[Test]
32+
public void RestRequest_Fail_On_Exception()
33+
{
34+
var req = new RestRequest("nonexisting");
35+
var client = new RestClient("http://localhost:12345") { ThrowOnAnyError = true };
36+
Assert.Throws<WebException>(() => client.Execute(req));
37+
}
2938
}
3039
}

0 commit comments

Comments
 (0)