public void throws_when_transformer_is_not_defined_for_a_type_and_no_default_transformer_is_defined() { var transformer = new ExceptionTransformer(); var exception = Assert.Throws<ApplicationException>(() => transformer.Transform(new Exception())); exception.Message.ShouldContain("No tranformer is defined for type \"Exception\""); }
public void falls_back_to_default_handler() { const string title = "An error occurred."; const string message = "An error occurred. Please contact help desk."; const HttpStatusCode statusCode = HttpStatusCode.InternalServerError; var transformer = new ExceptionTransformer { DefaultTransformer = e => new ErrorResponse(statusCode, title, message) }; var errorResponse = transformer.Transform(new NullReferenceException()); errorResponse.Title.ShouldEqual(title); errorResponse.Message.ShouldEqual(message); errorResponse.HttpStatusCode.ShouldEqual(statusCode); }
public void transforms_given_exception_to_error_response() { const string title = "Foo title"; const string message = "Foo message"; const HttpStatusCode statusCode = HttpStatusCode.NotFound; var transformer = new ExceptionTransformer(); transformer.AddTransformer<ArgumentException>(e => { return new ErrorResponse(statusCode, title, e.Message); }); var errorResponse = transformer.Transform(new ArgumentException(message)); errorResponse.Title.ShouldEqual(title); errorResponse.Message.ShouldEqual(message); errorResponse.HttpStatusCode.ShouldEqual(statusCode); }
public void Configuration(IAppBuilder appBuilder) { var config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "{controller}/{id}", defaults: new { controller = "Home", id = RouteParameter.Optional } ); config.Formatters.Clear(); config.Formatters.Add(new HyperfriendlyJsonMediaTypeFormatter()); var transformer = new ExceptionTransformer{ DefaultTransformer = e => new ErrorResponse(HttpStatusCode.InternalServerError, "Error!", "Something bad happened!")}; transformer.AddTransformer<OhNoException>(e => new ErrorResponse(HttpStatusCode.BadRequest, e.Shout, e.Shout + " Something bad happened!")); config.Services.Replace(typeof(IExceptionHandler), new HyperfriendlyExceptionHandler(transformer)); appBuilder.UseWebApi(config); }
/// <summary> /// Initializes the <see cref="T:BoDi.ObjectContainer"/> class. /// </summary> static ObjectContainer() { exceptionTransformer = new ExceptionTransformer(); containerFactory = new BoDiFlexDiContainerFactory(); }
public HyperfriendlyExceptionHandler(ExceptionTransformer transformer) { _transformer = transformer; }