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>
<TargetFrameworks>netstandard2.0;netstandard2.1;net7.0</TargetFrameworks>
<Nullable>enable</Nullable>
<WarningsAsErrors>Nullable</WarningsAsErrors>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
<RootNamespace />
</PropertyGroup>

24
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<AbpExceptionHandlingOptions> options = null)
public RemoteServiceErrorInfo Convert(Exception exception, Action<AbpExceptionHandlingOptions>? 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())
{

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="options">Additional options.</param>
/// <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>
/// Type of the entity.
/// </summary>
public Type EntityType { get; set; }
public Type? EntityType { get; set; }
/// <summary>
/// Id of the Entity.
/// </summary>
public object Id { get; set; }
public object? Id { get; set; }
/// <summary>
/// Creates a new <see cref="EntityNotFoundException"/> object.
@ -37,7 +37,7 @@ public class EntityNotFoundException : AbpException
/// <summary>
/// Creates a new <see cref="EntityNotFoundException"/> object.
/// </summary>
public EntityNotFoundException(Type entityType, object id)
public EntityNotFoundException(Type entityType, object? id)
: this(entityType, id, null)
{
@ -46,7 +46,7 @@ public class EntityNotFoundException : AbpException
/// <summary>
/// Creates a new <see cref="EntityNotFoundException"/> object.
/// </summary>
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}"

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 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;

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

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

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

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

Loading…
Cancel
Save