Added backend
This commit is contained in:
55
gswi/Filters/CustomExceptionFilterAttribute.cs
Normal file
55
gswi/Filters/CustomExceptionFilterAttribute.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using NLog;
|
||||
using gswi.Model.Filters;
|
||||
|
||||
namespace gswi.Filters
|
||||
{
|
||||
public class CustomExceptionFilterAttribute : ExceptionFilterAttribute
|
||||
{
|
||||
|
||||
|
||||
private static Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public CustomExceptionFilterAttribute() { }
|
||||
|
||||
public override void OnException(ExceptionContext context)
|
||||
{
|
||||
context.Result = CreateJsonErrorResult(context);
|
||||
base.OnException(context);
|
||||
}
|
||||
|
||||
public JsonResult CreateJsonErrorResult(ExceptionContext context)
|
||||
{
|
||||
JsonResult result = null;
|
||||
|
||||
// create jsonresult
|
||||
var exception = context.Exception;
|
||||
if (exception is ServiceException)
|
||||
{
|
||||
var bex = exception as ServiceException;
|
||||
_logger.Error($"{bex.GetDetailedMessage()}");
|
||||
var error = new ErrorDto(bex.ErrorCode, bex.GetMessage());
|
||||
result = new JsonResult(error)
|
||||
{
|
||||
StatusCode = (int)HttpStatusCode.BadRequest
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
var trackingId = Guid.NewGuid();
|
||||
string userMsg = $"Tracking Id: {trackingId}";
|
||||
_logger.Error(exception, userMsg);
|
||||
var error = new ErrorDto($"Tracking Id: {trackingId}");
|
||||
result = new JsonResult(error)
|
||||
{
|
||||
StatusCode = (int)HttpStatusCode.InternalServerError
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user