Skip to content

Commit

Permalink
Throw an exception when ExecuteTaskAsync fails
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Aug 19, 2013
1 parent e49f258 commit 43d41ce
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
31 changes: 31 additions & 0 deletions RestSharp.Net4/Extensions/ResponseStatusExtensions.cs
@@ -0,0 +1,31 @@
using System;
using System.Net;

namespace RestSharp.Extensions
{
public static class ResponseStatusExtensions
{
/// <summary>
/// Convert a <see cref="ResponseStatus"/> to a <see cref="WebException"/> instance.
/// </summary>
/// <param name="responseStatus">The response status.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentOutOfRangeException">responseStatus</exception>
public static WebException ToWebException(this ResponseStatus responseStatus)
{
switch (responseStatus)
{
case ResponseStatus.None:
return new WebException("The request could not be processed.", WebExceptionStatus.ServerProtocolViolation);
case ResponseStatus.Error:
return new WebException("An error occured while processing the request.", WebExceptionStatus.ServerProtocolViolation);
case ResponseStatus.TimedOut:
return new WebException("The request timed-out.", WebExceptionStatus.Timeout);
case ResponseStatus.Aborted:
return new WebException("The request was aborted.", WebExceptionStatus.Timeout);
default:
throw new ArgumentOutOfRangeException("responseStatus");
}
}
}
}
1 change: 1 addition & 0 deletions RestSharp.Net4/RestSharp.Net4.csproj
Expand Up @@ -257,6 +257,7 @@
<Compile Include="..\restsharp\validation\Validate.cs">
<Link>Validation\Validate.cs</Link>
</Compile>
<Compile Include="Extensions\ResponseStatusExtensions.cs" />
<Compile Include="Extensions\RestClientExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions RestSharp/RestClient.Async.cs
Expand Up @@ -24,6 +24,8 @@
using System.Text;
using System.Net;

using RestSharp.Extensions;

namespace RestSharp
{
public partial class RestClient
Expand Down Expand Up @@ -237,6 +239,10 @@ public virtual Task<T> ExecuteTaskAsync<T>(IRestRequest request, CancellationTok
{
taskCompletionSource.TrySetException(response.ErrorException);
}
else if (response.ResponseStatus != ResponseStatus.Completed)
{
taskCompletionSource.TrySetException(response.ResponseStatus.ToWebException());
}
else
{
taskCompletionSource.TrySetResult(response.Data);
Expand Down Expand Up @@ -327,6 +333,10 @@ public virtual Task<IRestResponse> ExecuteTaskAsync(IRestRequest request, Cancel
{
taskCompletionSource.TrySetException(response.ErrorException);
}
else if (response.ResponseStatus != ResponseStatus.Completed)
{
taskCompletionSource.TrySetException(response.ResponseStatus.ToWebException());
}
else
{
taskCompletionSource.TrySetResult(response);
Expand Down

0 comments on commit 43d41ce

Please sign in to comment.