Skip to content

Solid.Http.Extensions.ExceptionMapper Initialization

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

Initialization

HXRd.Solid.Http.Extensions.ExceptionMapper is designed to work with Solid.Http and Solid.Http.Core.

ExceptionMappingOptions

  • Mappers This is a IEnumerable<IExceptionMapper> of custom mappers. See Extending how to create a custom mapper
  • CustomDefaultMapper This is a IExceptionMapper. You can choose to have a custom mapper as a default and then if you skip the generic type arguments, this mapper will be chosen
  • UseModelStateExceptionsAsDefault This is a boolean and is false by default. If you set this to true, the exception will contain the ModelState as well.

Examples

With Solid.Http

The most basic example

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSolidHttp()
                .AddExceptionMappings();
    }
}

Using the model state exceptions

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        var customMapper = new CustomExceptionMapper();
        services.AddSolidHttp()
                .AddExceptionMappings(new ExceptionMappingOptions
                {
                    UseModelStateExceptionsAsDefault = true
                });
    }
}

Using a custom mapper and setting it as the default

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        var customMapper = new CustomExceptionMapper();
        services.AddSolidHttp()
                .AddExceptionMappings(new ExceptionMappingOptions
                {
                    Mappers = new List<IExceptionMapper>
                    {
                        customMapper
                    },
                    CustomDefaultMapper = customMapper
                });
    }
}

With Solid.Http.Core

The most basic example

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSolidHttpCore()
                .AddExceptionMappings();
    }
}

Using the model state exceptions

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        var customMapper = new CustomExceptionMapper();
        services.AddSolidHttpCore()
                .AddExceptionMappings(new ExceptionMappingOptions
                {
                    UseModelStateExceptionsAsDefault = true
                });
    }
}

Using a custom mapper and setting it as the default

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        var customMapper = new CustomExceptionMapper();
        services.AddSolidHttpCore()
                .AddExceptionMappings(new ExceptionMappingOptions
                {
                    Mappers = new List<IExceptionMapper>
                    {
                        customMapper
                    },
                    CustomDefaultMapper = customMapper
                });
    }
}