-
Notifications
You must be signed in to change notification settings - Fork 0
Solid.Http.Extensions.ExceptionMapper Usage
Using the extension is quite easy. The best way to explain it is to see some examples.
Let's start with the basics. I'll set the example up like we are going to be using this in an web api environment for completeness but you can use this with other application types as well of cause.
public class Startup
{
public Startup(IConfiguration configuration)
{
_configuration = configuration;
}
public IConfiguration _configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services
.AddSolidHttpCore()
.AddExceptionMappings();
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
}
}Now that we have the startup class, lets setup a controller and use the extension
public class ValuesController : Controller
{
private SolidHttpClient _client;
public NuspecController(ISolidHttpClientFactory factory)
{
_client = factory.CreateWithBaseAddress("https://<SOME_FAULTY_API_THAT_THROWS_EXCEPTIONS");
}
[HttpGet]
public async Task GetAsync()
{
try
{
var resource = _client.GetAsync("exceptionEndpoint")
.ThrowsException()
.As<Resource>();
}
catch(SolidHttpRequestException ex)
{
// Handle the exception here
}
}
}Now this is the most strait forward way of using the extension. Note that SolidHttpRequestException has a lot of useful member variables set that can help with the exception. See here TODO for what those are.
But this example does not show of the true potential of the extension. Let's keep going. Lets handle Unauthorized exceptions special. ( From here on out i will only show the relevant code, not the whole controller ).
try
{
var resource = _client.GetAsync("exceptionEndpoint")
.ThrowsException()
.As<Resource>();
}
catch(SolidHttpRequestException ex) when ((int)ex.StatusCode == 401)
{
// Handle the Unauthorized exception here
}
catch(SolidHttpRequestException ex)
{
// Handle the rest here
}Now we are getting some thing useful out of the extension.
Now we can take this further and handle ModleState errors as well. If the API that we are calling returns ModleState errors then it would be best to set that as the default behavior ( see Initialization for how to do that ).
Lets look at how we might handle ModleState errors
try
{
var resource = _client.GetAsync("exceptionEndpoint")
.ThrowsException()
.As<Resource>();
}
catch(SolidHttpRequestModelException ex) when when (ex.ModelState?["someField"] != null)
{
// Handle the model state exception for someField here
}
catch(SolidHttpRequestException ex)
{
// Handle the rest here
}Here we handle a ModelState error and we even drill down into the model state. Note that the exception that we are catching is different. It is of type SolidHttpRequestModelException, but the exception that we are catching below is of type SolidHttpRequestException. I left the example like this to point out that SolidHttpRequestModelException inherits from SolidHttpRequestException but adds the model state.