Skip to content

Commit

Permalink
Adds additional logging and exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Kali-Sh committed Nov 6, 2024
1 parent 2acf097 commit 341629f
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/Elders.Cronus/Projections/LatestVersionProjectionFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Elders.Cronus.MessageProcessing;
using Elders.Cronus.Projections.Cassandra;
using Elders.Cronus.Projections.Versioning;
using Microsoft.Extensions.Logging;

namespace Elders.Cronus.Projections;

Expand All @@ -19,17 +20,21 @@ internal class LatestVersionProjectionFinder : IProjectionVersionFinder
private readonly ProjectionHasher _hasher;
private readonly IProjectionStore _projectionStore;
private readonly ICronusContextAccessor _cronusContextAccessor;
private readonly ILogger<LatestVersionProjectionFinder> _logger;

private const string MasterProjectionMissingMessage = "table f1469a8e9fc847f5b057d5394ed33b4c_1_ver does not exist";

private readonly ProjectionVersion persistentVersion;

public LatestVersionProjectionFinder(TypeContainer<IProjection> allProjections, ProjectionHasher hasher, IProjectionStore projectionStore, ICronusContextAccessor cronusContextAccessor)
public LatestVersionProjectionFinder(TypeContainer<IProjection> allProjections, ProjectionHasher hasher, IProjectionStore projectionStore, ICronusContextAccessor cronusContextAccessor, ILogger<LatestVersionProjectionFinder> logger)
{
_allProjections = allProjections;
_hasher = hasher;
_projectionStore = projectionStore;
_cronusContextAccessor = cronusContextAccessor;

persistentVersion = new ProjectionVersion(ProjectionVersionsHandler.ContractId, ProjectionStatus.Live, 1, hasher.CalculateHash(typeof(ProjectionVersionsHandler)));
_logger = logger;
}

public IEnumerable<ProjectionVersion> GetProjectionVersionsToBootstrap()
Expand Down Expand Up @@ -71,30 +76,25 @@ public async Task<ProjectionVersion> GetCurrentLiveVersionOrTheDefaultOne(Type p
return initialVersion;
}
}
catch (Exception ex)
catch (Exception ex) when (ex.Message.Contains(MasterProjectionMissingMessage)) // we might come here if we are in the initial state (we have no data and f1 projection is missing), because we are trying to load from tables that don't exist yet
{
// TODO: we might come here if we are in the initial state (we have no data), because we are trying to load from tables that don't exist yet (Check for specific exeption?)

return initialVersion;
}
catch (Exception ex) when (True(() => _logger.LogError(ex, "Something went wrong while getting the latest live version, because {message}", ex.Message)))
{
throw;
}
}

private async Task<ReadResult<ProjectionVersionsHandler>> GetProjectionVersionsFromStoreAsync(string projectionName, string tenant)
{
try
{
ProjectionVersionManagerId versionId = new ProjectionVersionManagerId(projectionName, tenant);
ProjectionStream stream = await LoadProjectionStreamAsync(versionId, persistentVersion).ConfigureAwait(false);
ProjectionVersionManagerId versionId = new ProjectionVersionManagerId(projectionName, tenant);
ProjectionStream stream = await LoadProjectionStreamAsync(versionId, persistentVersion).ConfigureAwait(false);

ProjectionVersionsHandler projectionInstance = new ProjectionVersionsHandler();
projectionInstance = await stream.RestoreFromHistoryAsync(projectionInstance).ConfigureAwait(false);
ProjectionVersionsHandler projectionInstance = new ProjectionVersionsHandler();
projectionInstance = await stream.RestoreFromHistoryAsync(projectionInstance).ConfigureAwait(false);

return new ReadResult<ProjectionVersionsHandler>(projectionInstance);
}
catch (Exception ex)
{
return ReadResult<ProjectionVersionsHandler>.WithError(ex.Message);
}
return new ReadResult<ProjectionVersionsHandler>(projectionInstance);
}


Expand Down

0 comments on commit 341629f

Please sign in to comment.