diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ExceptionHandling/UserExceptionInformer.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ExceptionHandling/UserExceptionInformer.cs index c409c38319..31388b7bad 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ExceptionHandling/UserExceptionInformer.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ExceptionHandling/UserExceptionInformer.cs @@ -1,5 +1,4 @@ using System; -using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.AspNetCore.ExceptionHandling; diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultHttpExceptionStatusCodeFinder.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultHttpExceptionStatusCodeFinder.cs index cf49642459..2e1ef471a2 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultHttpExceptionStatusCodeFinder.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultHttpExceptionStatusCodeFinder.cs @@ -22,7 +22,13 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling public virtual HttpStatusCode GetStatusCode(HttpContext httpContext, Exception exception) { - if (exception is IHasErrorCode exceptionWithErrorCode && + if (exception is IHasHttpStatusCode exceptionWithHttpStatusCode && + exceptionWithHttpStatusCode.HttpStatusCode > 0) + { + return (HttpStatusCode) exceptionWithHttpStatusCode.HttpStatusCode; + } + + if (exception is IHasErrorCode exceptionWithErrorCode && !exceptionWithErrorCode.Code.IsNullOrWhiteSpace()) { if (Options.ErrorCodeToHttpStatusCodeMappings.TryGetValue(exceptionWithErrorCode.Code, out var status)) @@ -39,7 +45,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling } //TODO: Handle SecurityException..? - + if (exception is AbpValidationException) { return HttpStatusCode.BadRequest; @@ -59,8 +65,8 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling { return HttpStatusCode.Forbidden; } - + return HttpStatusCode.InternalServerError; } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/ExceptionHandling/IHasHttpStatusCode.cs b/framework/src/Volo.Abp.Core/Volo/Abp/ExceptionHandling/IHasHttpStatusCode.cs new file mode 100644 index 0000000000..a32e3fd45f --- /dev/null +++ b/framework/src/Volo.Abp.Core/Volo/Abp/ExceptionHandling/IHasHttpStatusCode.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.ExceptionHandling +{ + public interface IHasHttpStatusCode + { + int HttpStatusCode { get; } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpRemoteCallException.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpRemoteCallException.cs index 19ecfba0ab..f9a6436715 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpRemoteCallException.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpRemoteCallException.cs @@ -5,8 +5,10 @@ using Volo.Abp.ExceptionHandling; namespace Volo.Abp.Http.Client { [Serializable] - public class AbpRemoteCallException : AbpException, IHasErrorCode, IHasErrorDetails + public class AbpRemoteCallException : AbpException, IHasErrorCode, IHasErrorDetails, IHasHttpStatusCode { + public int HttpStatusCode { get; set; } + public string Code => Error?.Code; public string Details => Error?.Details; diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs index 81c1d02d9b..59bb7d67b6 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs @@ -252,10 +252,22 @@ namespace Volo.Abp.Http.Client.DynamicProxying await response.Content.ReadAsStringAsync() ); - throw new AbpRemoteCallException(errorResponse.Error); + throw new AbpRemoteCallException(errorResponse.Error) + { + HttpStatusCode = (int) response.StatusCode + }; } - throw new AbpException($"Remote service returns error! HttpStatusCode: {response.StatusCode}, ReasonPhrase: {response.ReasonPhrase}"); + throw new AbpRemoteCallException( + new RemoteServiceErrorInfo + { + Message = response.ReasonPhrase, + Code = response.StatusCode.ToString() + } + ) + { + HttpStatusCode = (int) response.StatusCode + }; } protected virtual CancellationToken GetCancellationToken()