private static HttpStatusCode GetHttpStatusCode(ChristmasMotherException exception) { var t = exception.GetType(); while (!ExceptionMap.ContainsKey(t)) { t = t.BaseType; } return(ExceptionMap[t]); }
private async Task HandleChristmasMotherExceptionAsync(HttpContext context, ChristmasMotherException exception) { _logger.LogWarning(exception.Message); var result = JsonConvert.SerializeObject(new { message = exception.Message }); context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)GetHttpStatusCode(exception); await context.Response.WriteAsync(result); }
public async Task Invoke_HandlesChristmasMotherExceptions() { // Arrange var exception = new ChristmasMotherException("ChristmasMother exception message"); var sut = new ErrorHandlingMiddleware(ctx => throw exception, _loggerFactory); // Act await sut.Invoke(_httpContext); // Assert _httpResponse.Received(1).ContentType = "application/json"; _httpResponse.Received(1).StatusCode = (int)HttpStatusCode.BadRequest; _bodyStream.Length.ShouldBeGreaterThan(0); AssertStreamContains(_bodyStream, exception.Message); }