-
-
Notifications
You must be signed in to change notification settings - Fork 760
Throw exceptions for failure status codes with more data about the response in them. #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http.Headers; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using System.Text.RegularExpressions; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
using System.IO; | ||
using System.Web; | ||
using HttpUtility = System.Web.HttpUtility; | ||
using System.Threading; | ||
|
||
namespace Refit | ||
|
@@ -180,7 +182,9 @@ Func<HttpClient, object[], Task> buildVoidTaskFuncForMethod(RestMethodInfo restM | |
var rq = factory(paramList); | ||
var resp = await client.SendAsync(rq); | ||
|
||
resp.EnsureSuccessStatusCode(); | ||
if (!resp.IsSuccessStatusCode) { | ||
throw await ApiException.Create(resp); | ||
} | ||
}; | ||
} | ||
|
||
|
@@ -195,7 +199,9 @@ Func<HttpClient, object[], Task<T>> buildTaskFuncForMethod<T>(RestMethodInfo res | |
return resp as T; | ||
} | ||
|
||
resp.EnsureSuccessStatusCode(); | ||
if (!resp.IsSuccessStatusCode) { | ||
throw await ApiException.Create(resp); | ||
} | ||
|
||
var content = await resp.Content.ReadAsStringAsync(); | ||
if (restMethod.SerializedReturnType == typeof(string)) { | ||
|
@@ -511,4 +517,56 @@ void determineReturnTypeInfo(MethodInfo methodInfo) | |
throw new ArgumentException("All REST Methods must return either Task<T> or IObservable<T>"); | ||
} | ||
} | ||
|
||
public class ApiException : Exception | ||
{ | ||
public HttpStatusCode StatusCode { get; private set; } | ||
public string ReasonPhrase { get; private set; } | ||
public HttpResponseHeaders Headers { get; private set; } | ||
|
||
public HttpContentHeaders ContentHeaders { get; private set; } | ||
public string Content { get; private set; } | ||
public bool HasContent | ||
{ | ||
get { return !string.IsNullOrWhiteSpace(Content); } | ||
} | ||
|
||
private ApiException(HttpStatusCode statusCode, string reasonPhrase, HttpResponseHeaders headers) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never use the private keyword There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry about that. Thought I got them all, but old habits... |
||
: base(createMessage(statusCode, reasonPhrase)) { | ||
StatusCode = statusCode; | ||
ReasonPhrase = reasonPhrase; | ||
Headers = headers; | ||
} | ||
|
||
public T GetContentAs<T>() | ||
{ | ||
return HasContent | ||
? JsonConvert.DeserializeObject<T>(Content) | ||
: default(T); | ||
} | ||
|
||
public static async Task<ApiException> Create(HttpResponseMessage response) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⬇️ |
||
var exception = new ApiException(response.StatusCode, response.ReasonPhrase, response.Headers); | ||
|
||
if (response.Content == null) return exception; | ||
|
||
try { | ||
exception.ContentHeaders = response.Content.Headers; | ||
exception.Content = await response.Content.ReadAsStringAsync(); | ||
response.Content.Dispose(); | ||
} | ||
catch { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⬆️ |
||
// We're already handling an exception at this point, | ||
// so we want to make sure we don't throw another one | ||
// that hides the real error. | ||
} | ||
|
||
return exception; | ||
} | ||
|
||
static string createMessage(HttpStatusCode statusCode, string reasonPhrase) | ||
{ | ||
return string.Format("Response status code does not indicate success: {0} ({1}).", (int)statusCode, reasonPhrase); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⬆️