Example #1
0
        public static void ConfigureExceptionHandler(this IApplicationBuilder app)
        {
            app.UseExceptionHandler(error =>
            {
                error.Run(async context =>
                {
                    var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>();
                    var exception = exceptionHandlerFeature.Error;

                    var statusCode = exception switch
                    {
                        ResourceNotFoundException _ => (int)HttpStatusCode.NotFound,
                        ModelFormatException _ => (int)HttpStatusCode.PreconditionFailed,
                        ResourceAlreadyExistsException _ => (int)HttpStatusCode.Conflict,
                        InvalidLoginException _ => (int)HttpStatusCode.Unauthorized,
                        UnauthorizedException _ => (int)HttpStatusCode.Unauthorized,
                        InvalidProductIdentifierException _ => (int)HttpStatusCode.PreconditionFailed,
                        _ => (int)HttpStatusCode.InternalServerError
                    };

                    context.Response.ContentType = "application/json";
                    context.Response.StatusCode  = statusCode;

                    await context.Response.WriteAsync(new ExceptionModel
                    {
                        StatusCode = statusCode,
                        Message    = exception.Message
                    }.ToString());
                });
            });
        }
Example #2
0
        public void ConstructorMessageShouldPopulateExceptionMessage()
        {
            var message   = "This is a really interesting message";
            var exception = new ResourceAlreadyExistsException(message);

            Assert.Equal(message, exception.Message);
        }
        public void TestResourceAlreadyExistsExceptionReturnsConflictResult()
        {
            // Arrange
            var loggerMock = new Mock <ILogger <ExceptionHandlerFilterAttribute> >();
            var logger     = loggerMock.Object;

            var loggerFactoryMock = new Mock <ILoggerFactory>();

            loggerFactoryMock.Setup(
                lf => lf.CreateLogger(It.IsAny <String>()))
            .Returns(logger);

            String exceptionMessage = "The resource already exists";
            var    exception        = new ResourceAlreadyExistsException(exceptionMessage);

            var httpContextMock  = new Mock <HttpContext>();
            var routeData        = new RouteData();
            var actionDescriptor = new ActionDescriptor();
            var actionContext    = new ActionContext(httpContextMock.Object, routeData, actionDescriptor);

            var exceptionContext = new ExceptionContext(actionContext, new List <IFilterMetadata>());

            exceptionContext.Exception        = exception;
            exceptionContext.ExceptionHandled = false;

            // Act
            var filter = new ExceptionHandlerFilterAttribute(loggerFactoryMock.Object);

            filter.OnException(exceptionContext);

            // Assert
            Assert.NotNull(exceptionContext.Result);
            Assert.IsType <ConflictObjectResult>(exceptionContext.Result);

            var resultObject = (ConflictObjectResult)(exceptionContext.Result);

            Assert.IsType <ApiMessageModel>(resultObject.Value);
            Assert.Equal(exceptionMessage, ((ApiMessageModel)resultObject.Value).Message);

            // Commented out because LogWarning is an extension method.  Need to figure out how to test
            //loggerMock.Verify(l => l.LogWarning(It.IsAny<EventId>(), exception, notFoundMessage), Times.Once);
        }