From 87a3e835dc471c9c8ceb15ac48b6a8d907232256 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Wed, 7 Jun 2023 17:47:14 +0800 Subject: [PATCH] Enable nullable annotations for Volo.Abp.ExceptionHanding --- .../Volo.Abp.ExceptionHandling.csproj | 2 ++ .../DefaultExceptionToErrorInfoConverter.cs | 24 ++++++++----------- .../IExceptionToErrorInfoConverter.cs | 2 +- .../Entities/EntityNotFoundException.cs | 8 +++---- .../Abp/Http/Client/AbpRemoteCallException.cs | 10 ++++---- .../Volo/Abp/Http/RemoteServiceErrorInfo.cs | 12 +++++----- .../Http/RemoteServiceValidationErrorInfo.cs | 4 ++-- 7 files changed, 30 insertions(+), 32 deletions(-) diff --git a/framework/src/Volo.Abp.ExceptionHandling/Volo.Abp.ExceptionHandling.csproj b/framework/src/Volo.Abp.ExceptionHandling/Volo.Abp.ExceptionHandling.csproj index 96c708d6c6..142b143632 100644 --- a/framework/src/Volo.Abp.ExceptionHandling/Volo.Abp.ExceptionHandling.csproj +++ b/framework/src/Volo.Abp.ExceptionHandling/Volo.Abp.ExceptionHandling.csproj @@ -5,6 +5,8 @@ netstandard2.0;netstandard2.1;net7.0 + enable + Nullable true diff --git a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs index 1b8742af81..b068890b97 100644 --- a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs +++ b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs @@ -54,7 +54,7 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert return errorInfo; } - public RemoteServiceErrorInfo Convert(Exception exception, Action options = null) + public RemoteServiceErrorInfo Convert(Exception exception, Action? options = null) { var exceptionHandlingOptions = CreateDefaultOptions(); options?.Invoke(exceptionHandlingOptions); @@ -90,7 +90,7 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert if (exception is EntityNotFoundException) { - return CreateEntityNotFoundError(exception as EntityNotFoundException); + return CreateEntityNotFoundError((exception as EntityNotFoundException)!); } var errorInfo = new RemoteServiceErrorInfo(); @@ -110,10 +110,10 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert if (errorInfo.Details.IsNullOrEmpty()) { - errorInfo.Details = GetValidationErrorNarrative(exception as IHasValidationErrors); + errorInfo.Details = GetValidationErrorNarrative((exception as IHasValidationErrors)!); } - errorInfo.ValidationErrors = GetValidationErrorInfos(exception as IHasValidationErrors); + errorInfo.ValidationErrors = GetValidationErrorInfos((exception as IHasValidationErrors)!); } TryToLocalizeExceptionMessage(exception, errorInfo); @@ -146,7 +146,7 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert } if (exceptionWithErrorCode.Code.IsNullOrWhiteSpace() || - !exceptionWithErrorCode.Code.Contains(":")) + !exceptionWithErrorCode.Code!.Contains(":")) { return; } @@ -197,10 +197,8 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert protected virtual Exception TryToGetActualException(Exception exception) { - if (exception is AggregateException && exception.InnerException != null) + if (exception is AggregateException aggException && aggException.InnerException != null) { - var aggException = exception as AggregateException; - if (aggException.InnerException is AbpValidationException || aggException.InnerException is AbpAuthorizationException || aggException.InnerException is EntityNotFoundException || @@ -223,7 +221,7 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert if (exception is AbpValidationException) { - errorInfo.ValidationErrors = GetValidationErrorInfos(exception as AbpValidationException); + errorInfo.ValidationErrors = GetValidationErrorInfos((exception as AbpValidationException)!); } return errorInfo; @@ -246,9 +244,8 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert } //Additional info for AbpValidationException - if (exception is AbpValidationException) + if (exception is AbpValidationException validationException) { - var validationException = exception as AbpValidationException; if (validationException.ValidationErrors.Count > 0) { detailBuilder.AppendLine(GetValidationErrorNarrative(validationException)); @@ -268,9 +265,8 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert } //Inner exceptions for AggregateException - if (exception is AggregateException) + if (exception is AggregateException aggException) { - var aggException = exception as AggregateException; if (aggException.InnerExceptions.IsNullOrEmpty()) { return; @@ -289,7 +285,7 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert foreach (var validationResult in validationException.ValidationErrors) { - var validationError = new RemoteServiceValidationErrorInfo(validationResult.ErrorMessage); + var validationError = new RemoteServiceValidationErrorInfo(validationResult.ErrorMessage!); if (validationResult.MemberNames != null && validationResult.MemberNames.Any()) { diff --git a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/IExceptionToErrorInfoConverter.cs b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/IExceptionToErrorInfoConverter.cs index ebf8993bd6..bd14010db0 100644 --- a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/IExceptionToErrorInfoConverter.cs +++ b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/IExceptionToErrorInfoConverter.cs @@ -24,5 +24,5 @@ public interface IExceptionToErrorInfoConverter /// The exception. /// Additional options. /// Error info or null - RemoteServiceErrorInfo Convert(Exception exception, Action options = null); + RemoteServiceErrorInfo Convert(Exception exception, Action? options = null); } diff --git a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Domain/Entities/EntityNotFoundException.cs b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Domain/Entities/EntityNotFoundException.cs index 1feb8c4cbb..5f524f9578 100644 --- a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Domain/Entities/EntityNotFoundException.cs +++ b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Domain/Entities/EntityNotFoundException.cs @@ -10,12 +10,12 @@ public class EntityNotFoundException : AbpException /// /// Type of the entity. /// - public Type EntityType { get; set; } + public Type? EntityType { get; set; } /// /// Id of the Entity. /// - public object Id { get; set; } + public object? Id { get; set; } /// /// Creates a new object. @@ -37,7 +37,7 @@ public class EntityNotFoundException : AbpException /// /// Creates a new object. /// - public EntityNotFoundException(Type entityType, object id) + public EntityNotFoundException(Type entityType, object? id) : this(entityType, id, null) { @@ -46,7 +46,7 @@ public class EntityNotFoundException : AbpException /// /// Creates a new object. /// - public EntityNotFoundException(Type entityType, object id, Exception innerException) + public EntityNotFoundException(Type entityType, object? id, Exception? innerException) : base( id == null ? $"There is no such an entity given id. Entity type: {entityType.FullName}" diff --git a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/Client/AbpRemoteCallException.cs b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/Client/AbpRemoteCallException.cs index 78c5bff6f9..887c8fb081 100644 --- a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/Client/AbpRemoteCallException.cs +++ b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/Client/AbpRemoteCallException.cs @@ -9,18 +9,18 @@ public class AbpRemoteCallException : AbpException, IHasErrorCode, IHasErrorDeta { public int HttpStatusCode { get; set; } - public string Code => Error?.Code; + public string? Code => Error?.Code; - public string Details => Error?.Details; + public string? Details => Error?.Details; - public RemoteServiceErrorInfo Error { get; set; } + public RemoteServiceErrorInfo? Error { get; set; } public AbpRemoteCallException() { } - public AbpRemoteCallException(string message, Exception innerException = null) + public AbpRemoteCallException(string message, Exception? innerException = null) : base(message, innerException) { @@ -32,7 +32,7 @@ public class AbpRemoteCallException : AbpException, IHasErrorCode, IHasErrorDeta } - public AbpRemoteCallException(RemoteServiceErrorInfo error, Exception innerException = null) + public AbpRemoteCallException(RemoteServiceErrorInfo error, Exception? innerException = null) : base(error.Message, innerException) { Error = error; diff --git a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/RemoteServiceErrorInfo.cs b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/RemoteServiceErrorInfo.cs index 89ed87454a..e61d55e2d3 100644 --- a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/RemoteServiceErrorInfo.cs +++ b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/RemoteServiceErrorInfo.cs @@ -12,27 +12,27 @@ public class RemoteServiceErrorInfo /// /// Error code. /// - public string Code { get; set; } + public string? Code { get; set; } /// /// Error message. /// - public string Message { get; set; } + public string? Message { get; set; } /// /// Error details. /// - public string Details { get; set; } + public string? Details { get; set; } /// /// Error data. /// - public IDictionary Data { get; set; } + public IDictionary? Data { get; set; } /// /// Validation errors if exists. /// - public RemoteServiceValidationErrorInfo[] ValidationErrors { get; set; } + public RemoteServiceValidationErrorInfo[]? ValidationErrors { get; set; } /// /// Creates a new instance of . @@ -49,7 +49,7 @@ public class RemoteServiceErrorInfo /// Error details /// Error message /// Error data - public RemoteServiceErrorInfo(string message, string details = null, string code = null, IDictionary data = null) + public RemoteServiceErrorInfo(string message, string? details = null, string? code = null, IDictionary? data = null) { Message = message; Details = details; diff --git a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/RemoteServiceValidationErrorInfo.cs b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/RemoteServiceValidationErrorInfo.cs index 4b86bbf4e3..64a31d7041 100644 --- a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/RemoteServiceValidationErrorInfo.cs +++ b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/RemoteServiceValidationErrorInfo.cs @@ -11,12 +11,12 @@ public class RemoteServiceValidationErrorInfo /// /// Validation error message. /// - public string Message { get; set; } + public string Message { get; set; } = default!; /// /// Relate invalid members (fields/properties). /// - public string[] Members { get; set; } + public string[] Members { get; set; } = default!; /// /// Creates a new instance of .