diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs index dc06aeea33..1901294def 100644 --- a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs @@ -16,9 +16,11 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling public ILogger 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("_AbpErrorFormat", "true")); context.Result = new ObjectResult( diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionHandlingMiddleware.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionHandlingMiddleware.cs index b769861cf7..852a335be0 100644 --- a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionHandlingMiddleware.cs +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionHandlingMiddleware.cs @@ -60,11 +60,11 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling _logger.LogException(exception); var errorInfoConverter = httpContext.RequestServices.GetRequiredService(); - var statusCodeFinder = httpContext.RequestServices.GetRequiredService(); + var statusCodeFinder = httpContext.RequestServices.GetRequiredService(); var jsonSerializer = httpContext.RequestServices.GetRequiredService(); 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("_AbpErrorFormat", "true")); //TODO: Constant diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/HttpExceptionStatusCodeFinder.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/DefaultHttpExceptionStatusCodeFinder.cs similarity index 60% rename from src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/HttpExceptionStatusCodeFinder.cs rename to src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/DefaultHttpExceptionStatusCodeFinder.cs index c099be0e7a..15ab80378e 100644 --- a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/HttpExceptionStatusCodeFinder.cs +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/DefaultHttpExceptionStatusCodeFinder.cs @@ -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; } } } \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/IHttpExceptionStatusCodeFinder.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/IHttpExceptionStatusCodeFinder.cs new file mode 100644 index 0000000000..0566a247e1 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/IHttpExceptionStatusCodeFinder.cs @@ -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); + } +} \ No newline at end of file