Skip to content

Instantly share code, notes, and snippets.

@fubar-coder
Last active December 7, 2015 07:50
Show Gist options
  • Save fubar-coder/61fbf1a8eb449f2e8081 to your computer and use it in GitHub Desktop.
Save fubar-coder/61fbf1a8eb449f2e8081 to your computer and use it in GitHub Desktop.
Deserialize error object for RestSharp.Portable
public class JsonDeserializerOptional<TError> : IDeserializer
{
/// <summary>
/// Deserialize the response
/// </summary>
/// <typeparam name="T">Object type to deserialize the result to</typeparam>
/// <param name="response">The response to deserialize the result from</param>
/// <returns>The deserialized object</returns>
public T Deserialize<T>(IRestResponse response)
{
var input = new MemoryStream(response.RawBytes);
using (var reader = new StreamReader(input))
{
var serializer = new JsonSerializer();
ConfigureSerializer(serializer);
var jsonReader = new JsonTextReader(reader);
if (!response.IsSuccess)
{
TError errorData;
try
{
errorData = serializer.Deserialize<TError>(jsonReader);
}
catch
{
throw new MyResponseException<TError>(response);
}
throw new MyResponseException<TError>(response, errorData);
}
return serializer.Deserialize<T>(jsonReader);
}
}
/// <summary>
/// Configure the JsonSerializer
/// </summary>
/// <param name="serializer">The serializer to configure</param>
protected virtual void ConfigureSerializer(JsonSerializer serializer)
{
}
}
public class MyResponseException<TError> : Exception
{
public MyResponseException(IRestResponse response, TError error) : base(response.StatusDescription)
{
Response = response;
ErrorData = error;
}
public MyResponseException(IRestResponse response) : base(response.StatusDescription)
{
Response = response;
ErrorData = default(TError);
}
public TError ErrorData { get; }
public IRestResponse Response { get; }
}
// Init client once
var client = new RestClient(yourDestinationUrl)
{
// Ignoring the status code is required, because otherwise the JSON deserializer
// isn't run when the HTTP status code isn't 200.
IgnoreResponseStatusCode = true
};
// Use our own JSON deserializer to be able to deserialize the error object
// and throw our own exception.
client.ClearHandlers();
client.AddHandler("application/json", new JsonDeserializerOptional<JsonErrorObject>());
client.AddHandler("text/json", new JsonDeserializerOptional<JsonErrorObject>());
client.AddHandler("text/x-json", new JsonDeserializerOptional<JsonErrorObject>());
client.AddHandler("text/javascript", new JsonDeserializerOptional<JsonErrorObject>());
// Call the API function
// Init request
var request = new RestRequest(subUrl);
// Execute request
try
{
var responseData = (await client.Execute<JsonResponseObject>(request)).Data;
// Work with the JsonResponseObject
}
catch (MyResponseException<JsonErrorObject> ex)
{
// ex.ErrorData contains the JSON error object or null (e.g. due to an internal server error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment