Skip to content

Solid.Http.Extensions.ExceptionMapper Usage

HX-Rd edited this page May 30, 2018 · 5 revisions

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.

Examples

    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 SolidHttpRequestException and SolidHttpRequestModelException
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 (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.

In the examples so far we are using the default mapper but we can select the mapper we want to use for the call to the API. Say that we have not set the UseModelStateExceptionsAsDefault but we want to get the model state for the call that we are going to make. We would do it like so

        try
        {
            var resource = _client.GetAsync("exceptionEndpoint")
                                  .ThrowsException<SolidHttpRequestModelException>()
                                  .As<Resource>();
        }
        catch(SolidHttpRequestModelException ex) when (ex.ModelState?["someField"] != null)
        {
            // Handle the model state exception for someField here
        }
        catch(SolidHttpRequestException ex)
        {
            // Handle the rest here
        }

Note this line .ThrowsException<SolidHttpRequestModelException>() here we are explicitly telling the extension to use this mapper. You can use your own mappers for your custom needs. See the extending documentation on how to do that.

When does the ThrowException throw an exception

The ThrowException throws by default if the StatusCode is not in the 200 range. But the function takes an predicate so you can decide when you want the function to throw.