Skip to content

Commit

Permalink
Better handling of http connection exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jordansjones committed May 7, 2015
1 parent 0c81f45 commit c29ba46
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions source/Draft/Draft-Net45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Compile Include="Exceptions\EventIndexClearedException.cs" />
<Compile Include="Exceptions\ExceptionHandling.cs" />
<Compile Include="Exceptions\ExistingPeerAddressException.cs" />
<Compile Include="Exceptions\HttpConnectionException.cs" />
<Compile Include="Exceptions\IndexNotANumberException.cs" />
<Compile Include="Exceptions\IndexOrValueRequiredException.cs" />
<Compile Include="Exceptions\IndexValueMutexException.cs" />
Expand Down
9 changes: 8 additions & 1 deletion source/Draft/Exceptions/EtcdException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public virtual bool IsExistingPeerAddress
get { return false; }
}

/// <summary>
/// Indicates that this exception is due to an underlying http client connection error.
/// </summary>
public virtual bool IsHttpConnection
{
get { return false; }
}

/// <summary>
/// Indicates that this exception is due to etcd being unable to parse the passed index value as a number.
/// </summary>
Expand Down Expand Up @@ -310,6 +318,5 @@ public virtual bool IsWatcherCleared
/// </summary>
public string RequestUrl { get; internal set; }


}
}
31 changes: 31 additions & 0 deletions source/Draft/Exceptions/ExceptionHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static EtcdException ProcessException(this Exception This)
if (fhe.IsTimeoutException()) { return fhe.AsTimeoutException(); }
if (fhe.IsInvalidHostException()) { return fhe.AsInvalidHostException(); }
if (fhe.IsInvalidRequestException()) { return fhe.AsInvalidRequestException(); }
if (fhe.IsHttpConnectionException()) { return fhe.AsHttpConnectionException(); }

var etcdError = fhe.GetResponseJson<EtcdError>();

Expand Down Expand Up @@ -154,6 +155,36 @@ public static EtcdException ProcessException(this Exception This)
return exception;
}

#region Connection Closed Exception

private static EtcdException AsHttpConnectionException(this FlurlHttpException This)
{
var webex = This.GetBaseException() as WebException;

if (webex == null || string.IsNullOrWhiteSpace(webex.Message)) return new HttpConnectionException();
return new HttpConnectionException(webex.Message);
}

private static bool IsHttpConnectionException(this FlurlHttpException This)
{
if (This == null) return false;

var be = This.GetBaseException();
var webex = be as WebException;

return This.Call != null
&& !This.Call.Completed
&& !This.Call.HttpStatus.HasValue
&& webex != null
&& (
webex.Status == WebExceptionStatus.ConnectionClosed
|| webex.Status == WebExceptionStatus.ConnectFailure
);
}

#endregion


#region Invalid Host Exception

private static EtcdException AsInvalidHostException(this FlurlHttpException This)
Expand Down
31 changes: 31 additions & 0 deletions source/Draft/Exceptions/HttpConnectionException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Linq;

namespace Draft.Exceptions
{
/// <summary>
/// Represents an error with the underlying http client when attempting to connect to an etcd endpoint.
/// </summary>
public class HttpConnectionException : EtcdException
{

/// <summary>
/// Initializes a new <see cref="HttpConnectionException" /> instance.
/// </summary>
public HttpConnectionException() {}

/// <summary>
/// Initializes a new <see cref="HttpConnectionException" /> instance with a specified error message.
/// </summary>
public HttpConnectionException(string message) : base(message) {}

/// <summary>
/// Indicates that this exception is due to an underlying http client connection error.
/// </summary>
public override bool IsHttpConnection
{
get { return true; }
}

}
}
1 change: 1 addition & 0 deletions tests/Draft.Tests/Draft-Net45.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Reference Include="System.Reactive.PlatformServices">
<HintPath>..\..\packages\Rx-PlatformServices.2.2.5\lib\net45\System.Reactive.PlatformServices.dll</HintPath>
</Reference>
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
Expand Down
11 changes: 11 additions & 0 deletions tests/Draft.Tests/Tests/Exceptions/CoreExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ public void ShouldThrowExistingPeerAddressException()
}
}

[Fact]
public void ShouldThrowHttpConnectionException()
{
FlurlHttp.Configure(
x => { x.HttpClientFactory = new TestingHttpClientFactory(new HttpTest(), (ht, hrm) => { throw new WebException("The Message", WebExceptionStatus.ConnectFailure); }); });

CallFixture.ShouldThrow<HttpConnectionException>()
.And
.IsHttpConnection.Should().BeTrue();
}

[Fact]
public void ShouldThrowIndexNotANumberException()
{
Expand Down

0 comments on commit c29ba46

Please sign in to comment.