Skip to content

Commit

Permalink
Added logic to catch and log any exception that may occur during data…
Browse files Browse the repository at this point in the history
… serialization.
  • Loading branch information
gmcelhanon committed Nov 7, 2024
1 parent 39c9daa commit 3c42a50
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,29 @@ public bool OnPreInsert(PreInsertEvent @event)

if (_serializationEnabled)
{
var lastModifiedDate = persister.Get<DateTime>(@event.State, ColumnNames.LastModifiedDate);
aggregateRoot.LastModifiedDate = lastModifiedDate;
aggregateRoot.CreateDate = createDate;
try
{
var lastModifiedDate = persister.Get<DateTime>(@event.State, ColumnNames.LastModifiedDate);
aggregateRoot.LastModifiedDate = lastModifiedDate;
aggregateRoot.CreateDate = createDate;

// Produce the serialized data
var resourceData = MessagePackHelper.SerializeAndCompressAggregateData(aggregateRoot);
aggregateRoot.AggregateData = resourceData;
// Produce the serialized data
var resourceData = MessagePackHelper.SerializeAndCompressAggregateData(aggregateRoot);
aggregateRoot.AggregateData = resourceData;

// Update the state
persister.Set(@event.State, ColumnNames.AggregateData, aggregateRoot.AggregateData);
// Update the state
persister.Set(@event.State, ColumnNames.AggregateData, aggregateRoot.AggregateData);

if (_logger.IsDebugEnabled)
if (_logger.IsDebugEnabled)
{
_logger.Debug($"MessagePack bytes for updated entity: {resourceData.Length:N0}");
}
}
catch (Exception ex)
{
_logger.Debug($"MessagePack bytes for updated entity: {resourceData.Length:N0}");
_logger.Error($"An unexpected error occurred while serializing entity data on entity '{@event.Entity.GetType().Name}'...", ex);

throw;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,31 @@ public bool OnPreUpdate(PreUpdateEvent @event)
{
if (@event.Entity is AggregateRootWithCompositeKey aggregateRoot)
{
var persister = @event.Persister;
try
{
var persister = @event.Persister;

// Update the entity with the last modified date before serializing
var lastModifiedDate = persister.Get<DateTime>(@event.State, ColumnNames.LastModifiedDate);
aggregateRoot.LastModifiedDate = lastModifiedDate;
// Update the entity with the last modified date before serializing
var lastModifiedDate = persister.Get<DateTime>(@event.State, ColumnNames.LastModifiedDate);
aggregateRoot.LastModifiedDate = lastModifiedDate;

// Produce the serialized data
var aggregateData = MessagePackHelper.SerializeAndCompressAggregateData(aggregateRoot);
aggregateRoot.AggregateData = aggregateData;
// Produce the serialized data
var aggregateData = MessagePackHelper.SerializeAndCompressAggregateData(aggregateRoot);
aggregateRoot.AggregateData = aggregateData;

// Update the state
persister.Set(@event.State, ColumnNames.AggregateData, aggregateRoot.AggregateData);
// Update the state
persister.Set(@event.State, ColumnNames.AggregateData, aggregateRoot.AggregateData);

if (_logger.IsDebugEnabled)
if (_logger.IsDebugEnabled)
{
_logger.Debug($"MessagePack bytes for updated entity: {aggregateData.Length:N0}");
}
}
catch (Exception ex)
{
_logger.Debug($"MessagePack bytes for updated entity: {aggregateData.Length:N0}");
_logger.Error($"An unexpected error occurred while serializing entity data on entity '{@event.Entity.GetType().Name}'...", ex);

throw;
}
}
}
Expand Down

0 comments on commit 3c42a50

Please sign in to comment.