Search code examples
c#aspnetboilerplate

Overcoming ErrorInfo limitations in ASP.NET Boilerplate Framework exception conversion


I implemented custom IExceptionToErrorInfoConverter for ASP.NET Boilerplate to convert custom exceptions in Web API.

Problem is that ASP.NET Boilerplate has a strict interface, that must return the ErrorInfo type:

ErrorInfo Convert(Exception exception);

The problem is that the ErrorInfo structure does not fit my requirements so I would like to have my own error DTO.

Anyone has an idea how to circumvent ASP.NET Boilerplate's exception conversion?


Solution

  • You can try one trick. When ASP.NET Boilerplate creates a JSON response, it might serialize the error with all available properties via reflection and wrap it into MvcAjaxResponse object with other stuff.

    You can try to create your own class, derived from ErrorInfo and replace it at IExceptionToErrorInfoConverter implementation:

    [Serializable]
    public class MyErrorInfo : ErrorInfo
    {
        public string MyProperty1 { get; set; }
        public int MyProperty2 { get; set; }
    }
    
    public class MyExceptionToErrorInfoConverter : IExceptionToErrorInfoConverter
    {
        public IExceptionToErrorInfoConverter Next { set { } }        
    
        public ErrorInfo Convert(Exception exception)
        {
            return new MyErrorInfo{ MyProperty1 = "test", MyProperty2  = 1};
        }
    }