Skip to content

Commit

Permalink
fix: add proper exception when there is no aggregate root id in state
Browse files Browse the repository at this point in the history
  • Loading branch information
spardadanthe committed Aug 30, 2022
1 parent e1d3fae commit 41333d4
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Elders.Cronus/EventStore/LoggingAggregateRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

namespace Elders.Cronus.EventStore
Expand Down Expand Up @@ -29,11 +30,26 @@ public LoggingAggregateRepository(IAggregateRepository realDeal, ILogger<Logging
{
using (logger.BeginScope(s => s
.AddScope(Log.AggregateName, typeof(AR).Name)
.AddScope(Log.AggregateId, aggregateRoot.State.Id.Value)))
.AddScope(Log.AggregateId, GetAggregateRootId(aggregateRoot))))
{
await realDeal.SaveAsync<AR>(aggregateRoot).ConfigureAwait(false);
logger.Debug(() => "Aggregate has been saved.");
}
}

#region Welcome
private string GetAggregateRootId<AR>(AR aggregateRoot) where AR : IAggregateRoot
{
string aggregateId = string.Empty;
try { aggregateId = aggregateRoot.State.Id.Value; }
catch (Exception ex)
{
logger.ErrorException(ex, () => $"Unable to save aggregate. There is a problem with the ID for {typeof(AR).Name}");
throw new Exception($"Unable to save aggregate. There is a problem with the ID for {typeof(AR).Name}. Check the inner exception if you want to be confused even more.", ex);
}

return aggregateId;
}
#endregion
}
}

0 comments on commit 41333d4

Please sign in to comment.