Summary

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

History

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    {
 011        public string Message { get; set; }
 012        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
 20        public GlobalExceptionFilter(
 21            IHostingEnvironment environment,
 22            ILogger logger)
 23        {
 24            this._environment = environment;
 25            this._logger = logger;
 26        }
 27
 28        public void Dispose()
 29        {
 30
 31        }
 32
 33        public void OnException(ExceptionContext context)
 34        {
 35            var response = new ErrorModel()
 36            {
 37                Message = context.Exception.Message
 38            };
 39
 40            if (this._environment.IsDevelopment() ||
 41                this._environment.IsEnvironment("Test"))
 42            {
 43                response.StackTrace = context.Exception.StackTrace;
 44            }
 45
 46            this._logger.LogError(context.Exception,
 47                context.Exception.Message);
 48
 49            context.Result = new ObjectResult(response)
 50            {
 51                StatusCode = 500,
 52                DeclaredType = typeof(ErrorModel)
 53            };
 54        }
 55    }
 56}