using System.Collections.Generic;
public static void Main()
var mapper = new HttpExceptionMapping();
mapper.SetMapper(typeof(Exception), (ex) => new HttpContextErrorResponse());
mapper.SetMapper(typeof(NullReferenceException), (ex) => new HttpContextErrorResponse());
mapper.SetMapper(typeof(ArgumentException), (ex) =>
var specificEx = ((ArgumentException)ex);
return new HttpContextErrorResponse()
Message = "Specific Value"+ specificEx.ParamName
Console.WriteLine("Hello World");
public class HttpContextErrorResponse {
public string Message { get; set; }
public class HttpExceptionMapping
private static Dictionary<Type, Func<Exception, HttpContextErrorResponse>> _exceptionHandlers =
new Dictionary<Type, Func<Exception, HttpContextErrorResponse>>();
public void SetMapper(Type exType, Func<Exception, HttpContextErrorResponse> mapper)
_exceptionHandlers.Add(exType, mapper);
public HttpContextErrorResponse HandleError(Exception ex)
var exType = ex.GetType();
return _exceptionHandlers[exType].Invoke(ex);