Refactored.

pull/279/head
Halil İbrahim Kalkan 7 years ago
parent f89ea0c085
commit 40b9441201

@ -16,9 +16,11 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling
public ILogger<AbpExceptionFilter> Logger { get; set; }
private readonly IExceptionToErrorInfoConverter _errorInfoConverter;
private readonly HttpExceptionStatusCodeFinder _statusCodeFinder;
private readonly IHttpExceptionStatusCodeFinder _statusCodeFinder;
public AbpExceptionFilter(IExceptionToErrorInfoConverter errorInfoConverter, HttpExceptionStatusCodeFinder statusCodeFinder)
public AbpExceptionFilter(
IExceptionToErrorInfoConverter errorInfoConverter,
IHttpExceptionStatusCodeFinder statusCodeFinder)
{
_errorInfoConverter = errorInfoConverter;
_statusCodeFinder = statusCodeFinder;
@ -63,7 +65,7 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling
protected virtual void HandleAndWrapException(ExceptionContext context)
{
context.HttpContext.Response.StatusCode = _statusCodeFinder.GetStatusCode(context.HttpContext, context.Exception);
context.HttpContext.Response.StatusCode = (int)_statusCodeFinder.GetStatusCode(context.HttpContext, context.Exception);
context.HttpContext.Response.Headers.Add(new KeyValuePair<string, StringValues>("_AbpErrorFormat", "true"));
context.Result = new ObjectResult(

@ -60,11 +60,11 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling
_logger.LogException(exception);
var errorInfoConverter = httpContext.RequestServices.GetRequiredService<IExceptionToErrorInfoConverter>();
var statusCodeFinder = httpContext.RequestServices.GetRequiredService<HttpExceptionStatusCodeFinder>();
var statusCodeFinder = httpContext.RequestServices.GetRequiredService<IHttpExceptionStatusCodeFinder>();
var jsonSerializer = httpContext.RequestServices.GetRequiredService<IJsonSerializer>();
httpContext.Response.Clear();
httpContext.Response.StatusCode = statusCodeFinder.GetStatusCode(httpContext, exception);
httpContext.Response.StatusCode = (int)statusCodeFinder.GetStatusCode(httpContext, exception);
httpContext.Response.OnStarting(_clearCacheHeadersDelegate, httpContext.Response);
httpContext.Response.Headers.Add(new KeyValuePair<string, StringValues>("_AbpErrorFormat", "true")); //TODO: Constant

@ -8,35 +8,35 @@ using Volo.Abp.Validation;
namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling
{
public class HttpExceptionStatusCodeFinder : ITransientDependency
public class DefaultHttpExceptionStatusCodeFinder : IHttpExceptionStatusCodeFinder, ITransientDependency
{
public virtual int GetStatusCode(HttpContext httpContext, Exception exception)
public virtual HttpStatusCode GetStatusCode(HttpContext httpContext, Exception exception)
{
//TODO: If the exception has error code than we can determine the exception from it!
if (exception is AbpAuthorizationException)
{
return httpContext.User.Identity.IsAuthenticated
? (int)HttpStatusCode.Forbidden
: (int)HttpStatusCode.Unauthorized;
? HttpStatusCode.Forbidden
: HttpStatusCode.Unauthorized;
}
if (exception is AbpValidationException)
{
return (int)HttpStatusCode.BadRequest;
return HttpStatusCode.BadRequest;
}
if (exception is EntityNotFoundException)
{
return (int)HttpStatusCode.NotFound;
return HttpStatusCode.NotFound;
}
if (exception is IBusinessException)
{
return (int)HttpStatusCode.Forbidden;
return HttpStatusCode.Forbidden;
}
return (int)HttpStatusCode.InternalServerError;
return HttpStatusCode.InternalServerError;
}
}
}

@ -0,0 +1,11 @@
using System;
using System.Net;
using Microsoft.AspNetCore.Http;
namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling
{
public interface IHttpExceptionStatusCodeFinder
{
HttpStatusCode GetStatusCode(HttpContext httpContext, Exception exception);
}
}
Loading…
Cancel
Save