diff --git a/Activout.RestClient/DomainExceptions/AbstractDomainExceptionMapper.cs b/Activout.RestClient/DomainExceptions/AbstractDomainExceptionMapper.cs index 8bc7482..ddb4da6 100644 --- a/Activout.RestClient/DomainExceptions/AbstractDomainExceptionMapper.cs +++ b/Activout.RestClient/DomainExceptions/AbstractDomainExceptionMapper.cs @@ -1,4 +1,3 @@ -#nullable disable using System; using System.Net.Http; using System.Threading.Tasks; @@ -7,14 +6,14 @@ namespace Activout.RestClient.DomainExceptions { public abstract class AbstractDomainExceptionMapper : IDomainExceptionMapper { - public virtual Task CreateExceptionAsync(HttpResponseMessage httpResponseMessage, object data, - Exception innerException = null) + public virtual Task CreateExceptionAsync(HttpResponseMessage httpResponseMessage, object? data, + Exception? innerException = null) { return Task.FromResult(CreateException(httpResponseMessage, data, innerException)); } - protected virtual Exception CreateException(HttpResponseMessage httpResponseMessage, object data, - Exception innerException) + protected virtual Exception CreateException(HttpResponseMessage httpResponseMessage, object? data, + Exception? innerException) { throw new NotImplementedException(); } diff --git a/Activout.RestClient/DomainExceptions/DefaultDomainExceptionMapper.cs b/Activout.RestClient/DomainExceptions/DefaultDomainExceptionMapper.cs index 03370a5..e988b9d 100644 --- a/Activout.RestClient/DomainExceptions/DefaultDomainExceptionMapper.cs +++ b/Activout.RestClient/DomainExceptions/DefaultDomainExceptionMapper.cs @@ -1,4 +1,3 @@ -#nullable disable using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -25,8 +24,8 @@ public DefaultDomainExceptionMapper( _httpErrorAttributes = httpErrorAttributes.ToList(); } - protected override Exception CreateException(HttpResponseMessage httpResponseMessage, object data, - Exception innerException) + protected override Exception CreateException(HttpResponseMessage httpResponseMessage, object? data, + Exception? innerException) { var domainError = MapByDomainErrorAttribute(data) ?? MapHttpStatusCodeByAttribute(httpResponseMessage) @@ -35,15 +34,15 @@ protected override Exception CreateException(HttpResponseMessage httpResponseMes try { - return (Exception)Activator.CreateInstance(_domainExceptionType, domainError, innerException); + return (Exception)Activator.CreateInstance(_domainExceptionType, domainError, innerException)!; } catch (MissingMethodException) { - return (Exception)Activator.CreateInstance(_domainExceptionType, domainError); + return (Exception)Activator.CreateInstance(_domainExceptionType, domainError)!; } } - private static object MapByDomainErrorAttribute(object data) + private static object? MapByDomainErrorAttribute(object? data) { if (data == null) { @@ -61,7 +60,7 @@ where a.ApiValue.Equals(value) select a.DomainValue).FirstOrDefault(); } - private object MapHttpStatusCodeByEnumName(HttpResponseMessage httpResponseMessage) + private object? MapHttpStatusCodeByEnumName(HttpResponseMessage httpResponseMessage) { try { @@ -75,19 +74,19 @@ private object MapHttpStatusCodeByEnumName(HttpResponseMessage httpResponseMessa } } - private object MapHttpStatusCodeByAttribute(HttpResponseMessage httpResponseMessage) + private object? MapHttpStatusCodeByAttribute(HttpResponseMessage httpResponseMessage) { return GetDomainErrorValue(httpResponseMessage.StatusCode); } - private object GetDomainErrorValue(HttpStatusCode httpStatusCode) + private object? GetDomainErrorValue(HttpStatusCode httpStatusCode) { return _httpErrorAttributes .FirstOrDefault(x => x.HttpStatusCode == httpStatusCode) ?.DomainErrorValue; } - private object MapGenericClientOrServerError(HttpResponseMessage httpResponseMessage) + private object? MapGenericClientOrServerError(HttpResponseMessage httpResponseMessage) { var httpStatusCode = httpResponseMessage.StatusCode; return ((int)httpStatusCode > 500 ? GetDomainErrorValue((HttpStatusCode)500) : null) diff --git a/Activout.RestClient/DomainExceptions/DefaultDomainExceptionMapperFactory.cs b/Activout.RestClient/DomainExceptions/DefaultDomainExceptionMapperFactory.cs index 1ee547d..f845053 100644 --- a/Activout.RestClient/DomainExceptions/DefaultDomainExceptionMapperFactory.cs +++ b/Activout.RestClient/DomainExceptions/DefaultDomainExceptionMapperFactory.cs @@ -1,4 +1,3 @@ -#nullable disable using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -9,6 +8,8 @@ namespace Activout.RestClient.DomainExceptions { public class DefaultDomainExceptionMapperFactory : IDomainExceptionMapperFactory { + public static DefaultDomainExceptionMapperFactory Instance { get; } = new(); + public virtual IDomainExceptionMapper CreateDomainExceptionMapper( MethodInfo method, Type errorResponseType, @@ -26,11 +27,17 @@ public virtual IDomainExceptionMapper CreateDomainExceptionMapper( return new DefaultDomainExceptionMapper(exceptionType, errorType, httpErrorAttributes); } - private static IEnumerable GetAllDomainHttpErrorAttributes(MemberInfo method, + private static List GetAllDomainHttpErrorAttributes(MemberInfo method, Type errorType) { var attributes = GetDomainHttpErrorAttributes(method).ToList(); - attributes.AddRange(GetDomainHttpErrorAttributes(method.DeclaringType)); + + var declaringType = method.DeclaringType; + if (declaringType != null) + { + attributes.AddRange(GetDomainHttpErrorAttributes(declaringType)); + } + CheckDomainHttpErrorAttributes(errorType, attributes); return attributes; } diff --git a/Activout.RestClient/DomainExceptions/DomainHttpErrorAttribute.cs b/Activout.RestClient/DomainExceptions/DomainHttpErrorAttribute.cs index 34bf3ae..81b548f 100644 --- a/Activout.RestClient/DomainExceptions/DomainHttpErrorAttribute.cs +++ b/Activout.RestClient/DomainExceptions/DomainHttpErrorAttribute.cs @@ -1,11 +1,18 @@ using System; using System.Net; -namespace Activout.RestClient.DomainExceptions; - -[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method, AllowMultiple = true)] -public class DomainHttpErrorAttribute(HttpStatusCode httpStatusCode, object domainErrorValue) : Attribute +namespace Activout.RestClient.DomainExceptions { - public HttpStatusCode HttpStatusCode { get; } = httpStatusCode; - public object DomainErrorValue { get; } = domainErrorValue; + [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method, AllowMultiple = true)] + public class DomainHttpErrorAttribute : Attribute + { + public HttpStatusCode HttpStatusCode { get; } + public object DomainErrorValue { get; } + + public DomainHttpErrorAttribute(HttpStatusCode httpStatusCode, object domainErrorValue) + { + HttpStatusCode = httpStatusCode; + DomainErrorValue = domainErrorValue; + } + } } \ No newline at end of file diff --git a/Activout.RestClient/DomainExceptions/IDomainExceptionMapper.cs b/Activout.RestClient/DomainExceptions/IDomainExceptionMapper.cs index 1062cb8..e210afc 100644 --- a/Activout.RestClient/DomainExceptions/IDomainExceptionMapper.cs +++ b/Activout.RestClient/DomainExceptions/IDomainExceptionMapper.cs @@ -1,4 +1,3 @@ -#nullable disable using System; using System.Net.Http; using System.Threading.Tasks; @@ -7,8 +6,8 @@ namespace Activout.RestClient.DomainExceptions { public interface IDomainExceptionMapper { - Task CreateExceptionAsync(HttpResponseMessage httpResponseMessage, object data, - Exception innerException = null); + Task CreateExceptionAsync(HttpResponseMessage httpResponseMessage, object? data, + Exception? innerException = null); } } \ No newline at end of file