Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/Lykke.HttpClientGenerator/ApiExceptionTextExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Common;
using Refit;

namespace Lykke.HttpClientGenerator
{
/// <summary>
/// Text extensions for <see cref="ApiException"/>
/// </summary>
public static class ApiExceptionTextExtensions
{
/// <summary>
/// Get http request caused the exception as text
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public static string GetRequestPhrase(this ApiException exception)
{
return exception.RequestMessage == null ? string.Empty : $"Request: {exception.RequestMessage.ToJson()}.";
}

/// <summary>
/// Get response content as text
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public static string GetContentPhrase(this ApiException exception)
{
return exception.HasContent ? $"Content: {exception.Content.ToJson()}." : string.Empty;
}

/// <summary>
/// Get exception data as text
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public static string GetDataPhrase(this ApiException exception)
{
if (exception.Data.Count == 0)
return string.Empty;

return $"Data: {exception.Data.ToJson()}.";
}

/// <summary>
/// Get exception description including all the details about request and error, as text
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public static string GetDescription(this ApiException exception)
{
return $"Couldn't execute http request. Reason: {exception.ReasonPhrase}. {exception.GetContentPhrase()} {exception.GetDataPhrase()} {exception.GetRequestPhrase()}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Refit;

namespace Lykke.HttpClientGenerator.Exceptions
{
/// <summary>
/// Refit specific exceptions handling middleware
/// </summary>
public class RefitExceptionHandlerMiddleware
{
private readonly RequestDelegate _next;
private readonly RefitExceptionHandlingOptions _options;
private readonly ILogger<RefitExceptionHandlerMiddleware> _logger;

/// <summary>
/// Creates middleware
/// </summary>
/// <param name="next"></param>
/// <param name="logger"></param>
/// <param name="options"></param>
public RefitExceptionHandlerMiddleware(RequestDelegate next,
ILogger<RefitExceptionHandlerMiddleware> logger,
RefitExceptionHandlingOptions options)
{
_next = next;
_logger = logger;
_options = options;
}

/// <summary>
/// Executes upon middleware invocation
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public async Task Invoke(HttpContext context)
{
try
{
await _next(context);
}
catch (ValidationApiException e)
{
_logger.LogError(e, e.GetDescription());
if (_options.ReThrow) throw;
}
catch (ApiException e)
{
_logger.LogError(e, e.GetDescription());
if (_options.ReThrow) throw;
}
catch (HttpClientApiException e)
{
_logger.LogError(e, e.GetDescription());
if (_options.ReThrow) throw;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Lykke.HttpClientGenerator.Exceptions
{
/// <summary>
/// Options for Refit exceptions handling middleware
/// </summary>
public class RefitExceptionHandlingOptions
{
/// <summary>
/// If exception has to be thrown after processing
/// </summary>
public bool ReThrow { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Common;
using Lykke.HttpClientGenerator.Exceptions;

namespace Lykke.HttpClientGenerator
{
/// <summary>
/// Text extensions for <see cref="HttpClientApiException"/>
/// </summary>
public static class HttpClientApiExceptionTextExtensions
{
/// <summary>
/// Get error response as text
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public static string GetErrorResponsePhrase(this HttpClientApiException exception)
{
if (exception.ErrorResponse == null)
return string.Empty;

return $"Error response: {exception.ErrorResponse.ToJson()}.";
}

/// <summary>
/// Get status code as text
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public static string GetStatusCodePhrase(this HttpClientApiException exception)
{
return $"Status code: {exception.HttpStatusCode.ToString()}.";
}

/// <summary>
/// Get exception description including all the details about error, as text
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public static string GetDescription(this HttpClientApiException exception)
{
return $"Couldn't execute http request. {GetErrorResponsePhrase(exception)} {GetStatusCodePhrase(exception)}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageReference Include="Nito.AsyncEx" Version="5.0.0" />
<PackageReference Include="Polly" Version="6.0.1" />
<PackageReference Include="Polly.Caching.Memory" Version="2.0.0" />
<PackageReference Include="Refit" Version="4.5.6" />
<PackageReference Include="Refit" Version="5.2.1" />
<PackageReference Include="System.Reflection.DispatchProxy" Version="4.5.1" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Common;
using Lykke.HttpClientGenerator.Retries;
using Refit;

namespace Lykke.HttpClientGenerator
{
/// <summary>
/// Text extensions for <see cref="ValidationApiException"/>
/// </summary>
public static class ValidationApiExceptionTextExtensions
{
/// <summary>
/// Get http request caused the exception problem details as text
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public static string GetProblemDetailsPhrase(this ValidationApiException exception)
{
return exception.HasContent ? $"Problem details: {exception.Content.ToJson()}." : string.Empty;
}

/// <summary>
/// Get exception description including all the details about request and error, as text
/// </summary>
/// <param name="exception"></param>
/// <returns></returns>
public static string GetDescription(this ValidationApiException exception)
{
return $"Couldn't execute http request. {exception.GetProblemDetailsPhrase()} {exception.GetRequestPhrase()}";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
Expand Down Expand Up @@ -87,7 +88,7 @@ public SuffixUrlParameterFormatter(string suffix)
_suffix = suffix ?? string.Empty;
}

public string Format(object value, ParameterInfo parameterInfo)
public string Format(object value, ICustomAttributeProvider attributeProvider, Type type)
{
return string.Concat(value, '_', _suffix);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="Moq" Version="4.14.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Refit" Version="4.5.6" />
<PackageReference Include="Refit" Version="5.2.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Lykke.HttpClientGenerator\Lykke.HttpClientGenerator.csproj">
Expand Down