using System.Collections.Generic;
using System.Threading.Tasks;
using Serilog;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Errors;
namespace Wino.Core.Services;
///
/// Factory for handling synchronizer errors
///
public class SynchronizerErrorHandlingFactory
{
private readonly ILogger _logger = Log.ForContext();
private readonly List _handlers = new();
///
/// Registers an error handler
///
/// The handler to register
public void RegisterHandler(ISynchronizerErrorHandler handler)
{
_handlers.Add(handler);
}
///
/// Handles an error using the registered handlers
///
/// The error to handle
/// True if the error was handled, false otherwise
public async Task HandleErrorAsync(SynchronizerErrorContext error)
{
foreach (var handler in _handlers)
{
if (handler.CanHandle(error))
{
_logger.Debug("Found handler {HandlerType} for error code {ErrorCode} message {ErrorMessage}",
handler.GetType().Name, error.ErrorCode, error.ErrorMessage);
return await handler.HandleAsync(error);
}
}
_logger.Debug("No handler found for error code {ErrorCode} message {ErrorMessage}",
error.ErrorCode, error.ErrorMessage);
return false;
}
}