Skip to content

Commit

Permalink
Extracted error handling on reading snapshot
Browse files Browse the repository at this point in the history
This allows different implementations to react differently on a failure
while reading a snapshot.

Related to #607
  • Loading branch information
abuijze committed May 30, 2018
1 parent 167c270 commit ecf0ebf
Showing 1 changed file with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public DomainEventStream readEvents(String aggregateIdentifier) {
try {
optionalSnapshot = storageEngine.readSnapshot(aggregateIdentifier);
} catch (Exception | LinkageError e) {
logger.warn("Error reading snapshot. Reconstructing aggregate from entire event stream.", e);
optionalSnapshot = Optional.empty();
optionalSnapshot = handleSnapshotReadingError(aggregateIdentifier, e);
}
DomainEventStream eventStream;
if (optionalSnapshot.isPresent()) {
Expand All @@ -95,6 +94,29 @@ public DomainEventStream readEvents(String aggregateIdentifier) {
return DomainEventStream.concat(eventStream, DomainEventStream.of(domainEventMessages));
}

/**
* Invoked when an error ({@link Exception} or {@link LinkageError}) occurs while attempting to read a snapshot
* event. This method can be overridden to change the default behavior, which is to log the exception (warn level)
* and ignore the snapshot.
* <p>
* Overriding implementations may choose to return normally, or raise an exception. Exceptions raised from this
* method are propagated to the caller of the {@link #readEvents(String)} or {@link #readEvents(String, long)}
* methods.
* <p>
* Returning an empty Optional will force the initialization of the aggregate to happen based on the entire event
* stream of that aggregate.
*
* @param aggregateIdentifier The identifier of the aggregate for which an snapshot failed to load
* @param e The exception or error that occurred while loading or deserializing the snapshot
* @return An optional DomainEventMessage to use as the snapshot for this aggregate
* @throws RuntimeException any runtimeException to fail loading the
*/
protected Optional<DomainEventMessage<?>> handleSnapshotReadingError(String aggregateIdentifier, Throwable e) {
logger.warn("Error reading snapshot for aggregate [{}]. Reconstructing from entire event stream.",
aggregateIdentifier, e);
return Optional.empty();
}

/**
* Returns a Stream of all DomainEventMessages that have been staged for publication by an Aggregate with given
* {@code aggregateIdentifier}.
Expand Down

0 comments on commit ecf0ebf

Please sign in to comment.