Summary

Class:ProjectX.PublicApi.Filters.GlobalExceptionFilter
Assembly:ProjectX.PublicApi
File(s):C:\private\projects\projectx\backend\src\ProjectX.PublicApi\Filters\GlobalExceptionFilter.cs
Covered lines:6
Uncovered lines:16
Coverable lines:22
Total lines:56
Line coverage:27.2%
Branch coverage:0%

History

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
.ctor(...)10100100
Dispose()1000
OnException(...)3400

File(s)

C:\private\projects\projectx\backend\src\ProjectX.PublicApi\Filters\GlobalExceptionFilter.cs

#LineLine coverage
 1using System;
 2using Microsoft.AspNetCore.Hosting;
 3using Microsoft.AspNetCore.Mvc;
 4using Microsoft.AspNetCore.Mvc.Filters;
 5using Microsoft.Extensions.Logging;
 6
 7namespace ProjectX.PublicApi.Filters
 8{
 9    public class ErrorModel
 10    {
 11        public string Message { get; set; }
 12        public string StackTrace { get; set; }
 13    }
 14
 15    public class GlobalExceptionFilter : IExceptionFilter, IDisposable
 16    {
 17        private readonly IHostingEnvironment _environment;
 18        private readonly ILogger _logger;
 19
 2420        public GlobalExceptionFilter(
 2421            IHostingEnvironment environment,
 2422            ILogger logger)
 23        {
 2424            this._environment = environment;
 2425            this._logger = logger;
 2426        }
 27
 28        public void Dispose()
 29        {
 30
 031        }
 32
 33        public void OnException(ExceptionContext context)
 34        {
 035            var response = new ErrorModel()
 036            {
 037                Message = context.Exception.Message
 038            };
 39
 040             if (this._environment.IsDevelopment() ||
 041                this._environment.IsEnvironment("Test"))
 42            {
 043                response.StackTrace = context.Exception.StackTrace;
 44            }
 45
 046            this._logger.LogError(context.Exception,
 047                context.Exception.Message);
 48
 049            context.Result = new ObjectResult(response)
 050            {
 051                StatusCode = 500,
 052                DeclaredType = typeof(ErrorModel)
 053            };
 054        }
 55    }
 56}