Browse Source

Enable nullable annotations for Volo.Abp.ExceptionHanding

pull/17020/head
liangshiwei 3 years ago
parent
commit
87a3e835dc
  1. 2
      framework/src/Volo.Abp.ExceptionHandling/Volo.Abp.ExceptionHandling.csproj
  2. 24
      framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs
  3. 2
      framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/IExceptionToErrorInfoConverter.cs
  4. 8
      framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Domain/Entities/EntityNotFoundException.cs
  5. 10
      framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/Client/AbpRemoteCallException.cs
  6. 12
      framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/RemoteServiceErrorInfo.cs
  7. 4
      framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/RemoteServiceValidationErrorInfo.cs

2
framework/src/Volo.Abp.ExceptionHandling/Volo.Abp.ExceptionHandling.csproj

@ -5,6 +5,8 @@
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net7.0</TargetFrameworks> <TargetFrameworks>netstandard2.0;netstandard2.1;net7.0</TargetFrameworks>
<Nullable>enable</Nullable>
<WarningsAsErrors>Nullable</WarningsAsErrors>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest> <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
<RootNamespace /> <RootNamespace />
</PropertyGroup> </PropertyGroup>

24
framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs

@ -54,7 +54,7 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert
return errorInfo; return errorInfo;
} }
public RemoteServiceErrorInfo Convert(Exception exception, Action<AbpExceptionHandlingOptions> options = null) public RemoteServiceErrorInfo Convert(Exception exception, Action<AbpExceptionHandlingOptions>? options = null)
{ {
var exceptionHandlingOptions = CreateDefaultOptions(); var exceptionHandlingOptions = CreateDefaultOptions();
options?.Invoke(exceptionHandlingOptions); options?.Invoke(exceptionHandlingOptions);
@ -90,7 +90,7 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert
if (exception is EntityNotFoundException) if (exception is EntityNotFoundException)
{ {
return CreateEntityNotFoundError(exception as EntityNotFoundException); return CreateEntityNotFoundError((exception as EntityNotFoundException)!);
} }
var errorInfo = new RemoteServiceErrorInfo(); var errorInfo = new RemoteServiceErrorInfo();
@ -110,10 +110,10 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert
if (errorInfo.Details.IsNullOrEmpty()) 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); TryToLocalizeExceptionMessage(exception, errorInfo);
@ -146,7 +146,7 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert
} }
if (exceptionWithErrorCode.Code.IsNullOrWhiteSpace() || if (exceptionWithErrorCode.Code.IsNullOrWhiteSpace() ||
!exceptionWithErrorCode.Code.Contains(":")) !exceptionWithErrorCode.Code!.Contains(":"))
{ {
return; return;
} }
@ -197,10 +197,8 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert
protected virtual Exception TryToGetActualException(Exception exception) 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 || if (aggException.InnerException is AbpValidationException ||
aggException.InnerException is AbpAuthorizationException || aggException.InnerException is AbpAuthorizationException ||
aggException.InnerException is EntityNotFoundException || aggException.InnerException is EntityNotFoundException ||
@ -223,7 +221,7 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert
if (exception is AbpValidationException) if (exception is AbpValidationException)
{ {
errorInfo.ValidationErrors = GetValidationErrorInfos(exception as AbpValidationException); errorInfo.ValidationErrors = GetValidationErrorInfos((exception as AbpValidationException)!);
} }
return errorInfo; return errorInfo;
@ -246,9 +244,8 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert
} }
//Additional info for AbpValidationException //Additional info for AbpValidationException
if (exception is AbpValidationException) if (exception is AbpValidationException validationException)
{ {
var validationException = exception as AbpValidationException;
if (validationException.ValidationErrors.Count > 0) if (validationException.ValidationErrors.Count > 0)
{ {
detailBuilder.AppendLine(GetValidationErrorNarrative(validationException)); detailBuilder.AppendLine(GetValidationErrorNarrative(validationException));
@ -268,9 +265,8 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert
} }
//Inner exceptions for AggregateException //Inner exceptions for AggregateException
if (exception is AggregateException) if (exception is AggregateException aggException)
{ {
var aggException = exception as AggregateException;
if (aggException.InnerExceptions.IsNullOrEmpty()) if (aggException.InnerExceptions.IsNullOrEmpty())
{ {
return; return;
@ -289,7 +285,7 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert
foreach (var validationResult in validationException.ValidationErrors) 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()) if (validationResult.MemberNames != null && validationResult.MemberNames.Any())
{ {

2
framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/IExceptionToErrorInfoConverter.cs

@ -24,5 +24,5 @@ public interface IExceptionToErrorInfoConverter
/// <param name="exception">The exception.</param> /// <param name="exception">The exception.</param>
/// <param name="options">Additional options.</param> /// <param name="options">Additional options.</param>
/// <returns>Error info or null</returns> /// <returns>Error info or null</returns>
RemoteServiceErrorInfo Convert(Exception exception, Action<AbpExceptionHandlingOptions> options = null); RemoteServiceErrorInfo Convert(Exception exception, Action<AbpExceptionHandlingOptions>? options = null);
} }

8
framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Domain/Entities/EntityNotFoundException.cs

@ -10,12 +10,12 @@ public class EntityNotFoundException : AbpException
/// <summary> /// <summary>
/// Type of the entity. /// Type of the entity.
/// </summary> /// </summary>
public Type EntityType { get; set; } public Type? EntityType { get; set; }
/// <summary> /// <summary>
/// Id of the Entity. /// Id of the Entity.
/// </summary> /// </summary>
public object Id { get; set; } public object? Id { get; set; }
/// <summary> /// <summary>
/// Creates a new <see cref="EntityNotFoundException"/> object. /// Creates a new <see cref="EntityNotFoundException"/> object.
@ -37,7 +37,7 @@ public class EntityNotFoundException : AbpException
/// <summary> /// <summary>
/// Creates a new <see cref="EntityNotFoundException"/> object. /// Creates a new <see cref="EntityNotFoundException"/> object.
/// </summary> /// </summary>
public EntityNotFoundException(Type entityType, object id) public EntityNotFoundException(Type entityType, object? id)
: this(entityType, id, null) : this(entityType, id, null)
{ {
@ -46,7 +46,7 @@ public class EntityNotFoundException : AbpException
/// <summary> /// <summary>
/// Creates a new <see cref="EntityNotFoundException"/> object. /// Creates a new <see cref="EntityNotFoundException"/> object.
/// </summary> /// </summary>
public EntityNotFoundException(Type entityType, object id, Exception innerException) public EntityNotFoundException(Type entityType, object? id, Exception? innerException)
: base( : base(
id == null id == null
? $"There is no such an entity given id. Entity type: {entityType.FullName}" ? $"There is no such an entity given id. Entity type: {entityType.FullName}"

10
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 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()
{ {
} }
public AbpRemoteCallException(string message, Exception innerException = null) public AbpRemoteCallException(string message, Exception? innerException = null)
: base(message, innerException) : 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) : base(error.Message, innerException)
{ {
Error = error; Error = error;

12
framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/RemoteServiceErrorInfo.cs

@ -12,27 +12,27 @@ public class RemoteServiceErrorInfo
/// <summary> /// <summary>
/// Error code. /// Error code.
/// </summary> /// </summary>
public string Code { get; set; } public string? Code { get; set; }
/// <summary> /// <summary>
/// Error message. /// Error message.
/// </summary> /// </summary>
public string Message { get; set; } public string? Message { get; set; }
/// <summary> /// <summary>
/// Error details. /// Error details.
/// </summary> /// </summary>
public string Details { get; set; } public string? Details { get; set; }
/// <summary> /// <summary>
/// Error data. /// Error data.
/// </summary> /// </summary>
public IDictionary Data { get; set; } public IDictionary? Data { get; set; }
/// <summary> /// <summary>
/// Validation errors if exists. /// Validation errors if exists.
/// </summary> /// </summary>
public RemoteServiceValidationErrorInfo[] ValidationErrors { get; set; } public RemoteServiceValidationErrorInfo[]? ValidationErrors { get; set; }
/// <summary> /// <summary>
/// Creates a new instance of <see cref="RemoteServiceErrorInfo"/>. /// Creates a new instance of <see cref="RemoteServiceErrorInfo"/>.
@ -49,7 +49,7 @@ public class RemoteServiceErrorInfo
/// <param name="details">Error details</param> /// <param name="details">Error details</param>
/// <param name="message">Error message</param> /// <param name="message">Error message</param>
/// <param name="data">Error data</param> /// <param name="data">Error data</param>
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; Message = message;
Details = details; Details = details;

4
framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/Http/RemoteServiceValidationErrorInfo.cs

@ -11,12 +11,12 @@ public class RemoteServiceValidationErrorInfo
/// <summary> /// <summary>
/// Validation error message. /// Validation error message.
/// </summary> /// </summary>
public string Message { get; set; } public string Message { get; set; } = default!;
/// <summary> /// <summary>
/// Relate invalid members (fields/properties). /// Relate invalid members (fields/properties).
/// </summary> /// </summary>
public string[] Members { get; set; } public string[] Members { get; set; } = default!;
/// <summary> /// <summary>
/// Creates a new instance of <see cref="RemoteServiceValidationErrorInfo"/>. /// Creates a new instance of <see cref="RemoteServiceValidationErrorInfo"/>.

Loading…
Cancel
Save