Skip to content

Commit

Permalink
Add logging to AmazonShipping providers and cleanup
Browse files Browse the repository at this point in the history
Introduce logging capabilities in `AmazonShippingRateProvider` and `AmazonShippingShipmentProvider` for improved error handling. Log detailed error messages from the Amazon Shipping API. Remove redundant `services.AddLogging()` calls in `AddRestApiAmazonRateProvider` and `AddRestApiAmazonShipmentProvider`.
  • Loading branch information
Brandon Moffett committed Dec 16, 2024
1 parent be68694 commit c000fbc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
using EasyKeys.Shipping.Amazon.Abstractions.Options;
using EasyKeys.Shipping.Amazon.Abstractions.Services;

using Microsoft.Extensions.Logging;

namespace EasyKeys.Shipping.Amazon.Rates;

public class AmazonShippingRateProvider : IAmazonShippingRateProvider
{
private readonly IAmazonApiAuthenticatorService _authenticatorService;
private readonly AmazonShippingApiOptions _options;
private readonly AmazonShippingApi _shippingApi;
private readonly ILogger<AmazonShippingRateProvider> _logger;

public AmazonShippingRateProvider(
ILogger<AmazonShippingRateProvider> logger,
AmazonShippingApiOptions options,
IAmazonApiAuthenticatorService authenticatorService,
AmazonShippingApi shippingApi)
{
_logger = logger;
_options = options;
_authenticatorService = authenticatorService;
_shippingApi = shippingApi;
Expand Down Expand Up @@ -121,14 +126,18 @@ public async Task<Shipment> GetRatesAsync(Shipment shipment, CancellationToken c
{
shipment.InternalErrors.Add(error.Message);
}

_logger.LogError(ex, $"Error getting rates from Amazon Shipping API: {string.Join(",",ex.Result.Errors)}");

Check warning on line 130 in src/EasyKeys.Shipping.Amazon.Rates/AmazonShippingRateProvider.cs

View workflow job for this annotation

GitHub Actions / build

}
catch (ApiException ex)
{
shipment.InternalErrors.Add(ex.Message);
_logger.LogError(ex, $"Error getting rates from Amazon Shipping API: {ex.Message}");
}
catch (Exception ex)
{
shipment.InternalErrors.Add(ex.Message);
_logger.LogError(ex, $"Error getting rates from Amazon Shipping API: {ex.Message}");
}

return shipment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public static IServiceCollection AddRestApiAmazonRateProvider(
string sectionName = nameof(AmazonShippingApiOptions),
Action<AmazonShippingApiOptions, IServiceProvider>? configOptions = null)
{
services.AddLogging();
services.AddTransient<IAmazonShippingRateProvider, AmazonShippingRateProvider>();

return services;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Collections.ObjectModel;
using System.Text;

using EasyKeys.Shipping.Abstractions.Models;
using EasyKeys.Shipping.Amazon.Abstractions.OpenApis.V2.Shipping;
using EasyKeys.Shipping.Amazon.Abstractions.Options;
using EasyKeys.Shipping.Amazon.Abstractions.Services;
using EasyKeys.Shipping.Amazon.Shipment.Models;
using Microsoft.Extensions.Logging;

namespace EasyKeys.Shipping.Amazon.Shipment;

Expand All @@ -14,12 +14,15 @@ public class AmazonShippingShipmentProvider : IAmazonShippingShipmentProvider
private readonly AmazonShippingApi _shippingApi;
private readonly AmazonShippingApiOptions _options;
private readonly IAmazonApiAuthenticatorService _authenticatorService;
private readonly ILogger<AmazonShippingShipmentProvider> _logger;

public AmazonShippingShipmentProvider(
ILogger<AmazonShippingShipmentProvider> logger,
AmazonShippingApiOptions options,
AmazonShippingApi amazonShippingApi,
IAmazonApiAuthenticatorService amazonApiAuthenticator)
{
_logger = logger;
_options = options;
_shippingApi = amazonShippingApi;
_authenticatorService = amazonApiAuthenticator;
Expand Down Expand Up @@ -179,14 +182,18 @@ public async Task<ShipmentLabel> CreateSmartShipmentAsync(
{
label.InternalErrors.Add(error.Message);
}

_logger.LogError(ex, $"Error creating shipment from Amazon Shipping API: {string.Join(",", ex.Result.Errors)}");
}
catch (ApiException ex)
{
label.InternalErrors.Add(ex.Message);
_logger.LogError(ex, $"Error creating shipment from Amazon Shipping API: {ex.Message}");
}
catch (Exception ex)
{
label.InternalErrors.Add(ex.Message);
_logger.LogError(ex, $"Error creating shipment from Amazon Shipping API: {ex.Message}");
}

return label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public static IServiceCollection AddRestApiAmazonShipmentProvider(
string sectionName = nameof(AmazonShippingApiOptions),
Action<AmazonShippingApiOptions, IServiceProvider>? configOptions = null)
{
services.AddLogging();
services.AddTransient<IAmazonShippingShipmentProvider, AmazonShippingShipmentProvider>();

return services;
Expand Down

0 comments on commit c000fbc

Please sign in to comment.