Skip to content
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System;
using System.Net.Http;
using System.Threading.Tasks;
Expand All @@ -7,14 +6,14 @@ namespace Activout.RestClient.DomainExceptions
{
public abstract class AbstractDomainExceptionMapper : IDomainExceptionMapper
{
public virtual Task<Exception> CreateExceptionAsync(HttpResponseMessage httpResponseMessage, object data,
Exception innerException = null)
public virtual Task<Exception> 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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -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)
Expand All @@ -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)
{
Expand All @@ -61,7 +60,7 @@ where a.ApiValue.Equals(value)
select a.DomainValue).FirstOrDefault();
}

private object MapHttpStatusCodeByEnumName(HttpResponseMessage httpResponseMessage)
private object? MapHttpStatusCodeByEnumName(HttpResponseMessage httpResponseMessage)
{
try
{
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -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,
Expand All @@ -26,11 +27,17 @@ public virtual IDomainExceptionMapper CreateDomainExceptionMapper(
return new DefaultDomainExceptionMapper(exceptionType, errorType, httpErrorAttributes);
}

private static IEnumerable<DomainHttpErrorAttribute> GetAllDomainHttpErrorAttributes(MemberInfo method,
private static List<DomainHttpErrorAttribute> 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;
}
Expand Down
19 changes: 13 additions & 6 deletions Activout.RestClient/DomainExceptions/DomainHttpErrorAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System;
using System.Net.Http;
using System.Threading.Tasks;
Expand All @@ -7,8 +6,8 @@ namespace Activout.RestClient.DomainExceptions
{
public interface IDomainExceptionMapper
{
Task<Exception> CreateExceptionAsync(HttpResponseMessage httpResponseMessage, object data,
Exception innerException = null);
Task<Exception> CreateExceptionAsync(HttpResponseMessage httpResponseMessage, object? data,
Exception? innerException = null);

}
}