Skip to content

Commit

Permalink
Throw more appropriate exceptions based on HTTP response codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Lechner committed May 24, 2016
1 parent 032acf7 commit b9f7ea9
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Libraries/CloseIoDotNet/CloseIoDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
<Compile Include="Rest\Entities\Responses\Enumerables\IScanEnumerable.cs" />
<Compile Include="Rest\Entities\Responses\Enumerables\ScanEnumerable.cs" />
<Compile Include="Rest\Entities\Responses\ScanResponse.cs" />
<Compile Include="Rest\Exceptions\CloseIoRequestException.cs" />
<Compile Include="Rest\Exceptions\InternalServerErrorException.cs" />
<Compile Include="Rest\RequestFactories\IRestRequestFactory.cs" />
<Compile Include="Rest\RequestFactories\RestRequestFactory.cs" />
<Compile Include="Rest\Serialization\NewtonsoftDeserializer.cs" />
Expand Down
71 changes: 71 additions & 0 deletions Libraries/CloseIoDotNet/Rest/Exceptions/CloseIoRequestException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
namespace CloseIoDotNet.Rest.Exceptions
{
using System;
using System.Net;
using RestSharp;

public class CloseIoRequestException : Exception
{
#region Constants
private const string DefaultMessage =
"Close.Io returned an unknown error to your request";
#endregion

#region Instance Variables
private HttpStatusCode? _statusCode;
private string _body;
#endregion

#region Properties
public IRestRequest RestRequest { get; set; }
public IRestResponse RestResponse { get; set; }
public HttpStatusCode ResponseStatusCode
{
get
{
if (_statusCode.HasValue == false)
{
try
{
_statusCode = RestResponse.StatusCode;
}
catch (Exception)
{
_statusCode = new HttpStatusCode();
}
}
return _statusCode.Value;
}
set { _statusCode = value; }
}
public string ResponseBody
{
get
{
if (_body == null)
{
try
{
_body = RestResponse.Content;
}
catch (Exception)
{
_body = string.Empty;
}
}
return _body;
}
set { _body = value; }
}

#endregion

#region Constructors
public CloseIoRequestException() : base(DefaultMessage) { }

public CloseIoRequestException(string message) : base(message) { }

public CloseIoRequestException(string message, Exception innerException) : base(message, innerException) { }
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace CloseIoDotNet.Rest.Exceptions
{
using System;
public class InternalServerErrorException : Exception
{
#region Constants
public const string DefaultMessage =
"Close.Io encountered an unexpected internal server error (HTTP 500) while processing your request.";
#endregion

#region Constructors
public InternalServerErrorException() : base(DefaultMessage) { }

public InternalServerErrorException(string message) : base(message) { }

public InternalServerErrorException(string message, Exception innerException) : base(message, innerException) { }
#endregion
}
}
50 changes: 48 additions & 2 deletions Libraries/CloseIoDotNet/Rest/Utilities/RestResponseValidator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace CloseIoDotNet.Rest.Utilities
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using CloseIoDotNet.Rest.Exceptions;
using RestSharp;

public class RestResponseValidator : IRestResponseValidator
Expand All @@ -27,8 +30,51 @@ public void Validate(IRestRequest request, IRestResponse response)
response.StatusCode != HttpStatusCode.PartialContent
)
{
//TODO inspect response type and body, issue specific exceptions
throw new InvalidOperationException();
//404
if (HttpStatusCode.NotFound.Equals(response.StatusCode))
{
throw new KeyNotFoundException("Close.Io returned 404 (not found) for your request.",
new CloseIoRequestException()
{
RestRequest = request,
RestResponse = response
});
}

//500
if (HttpStatusCode.InternalServerError.Equals(response.StatusCode))
{
throw new InternalServerErrorException(InternalServerErrorException.DefaultMessage,
new CloseIoRequestException()
{
RestRequest = request,
RestResponse = response
});
}

//0 (returned by RestSharp when no network connection available, or other networking issue.
if (response.StatusCode == 0)
{
throw new HttpRequestException("Network connection unavailable.", new CloseIoRequestException()
{
RestRequest = request,
RestResponse = response
});
}

//unknown
try
{
throw new CloseIoRequestException()
{
RestRequest = request,
RestResponse = response
};
}
catch (Exception)
{
throw new CloseIoRequestException();
}
}
}
#endregion
Expand Down

0 comments on commit b9f7ea9

Please sign in to comment.