/// <inheritdoc /> public CommittedEventStream Commit(UncommittedEventStream uncommittedEvents) { var commit = uncommittedEvents.AsBsonCommit(); return(Do(() => { try { var result = ExecuteCommit(commit); if (result.IsSuccessfulCommit()) { var sequence_number = result[Constants.ID].ToUlong(); return uncommittedEvents.ToCommitted(sequence_number); } else if (result.IsKnownError()) { if (result.IsPreviousVersion()) { throw new EventSourceConcurrencyConflict(result.ToEventSourceVersion(), uncommittedEvents.Source.Version); } else if (IsDuplicateCommit(uncommittedEvents.Id)) { throw new CommitIsADuplicate(); } else { throw new EventSourceConcurrencyConflict(result[Constants.ERROR].AsBsonDocument.ToEventSourceVersion(), uncommittedEvents.Source.Version); } } else { throw new UnknownCommitError(result?.ToString() ?? "[NULL]"); } } catch (Exception ex) { _logger.Error(ex, "Exception committing event stream"); throw; } })); }
/// <inheritdoc /> public CommittedEventStream Commit(UncommittedEventStream uncommittedEvents) { var commit = uncommittedEvents.AsBsonCommit(); return(Do <CommittedEventStream>(() => { try { var result = ExecuteCommit(commit); if (result.IsSuccessfulCommit()) { var sequence_number = result[Constants.ID].ToUlong(); var committed = uncommittedEvents.ToCommitted(sequence_number); return committed; } else if (result.IsKnownError()) { if (result.IsPreviousVersion()) { throw new EventSourceConcurrencyConflict($"Current Version is {result["version"]}, tried to commit {uncommittedEvents.Source.Version.Commit}"); } else if (IsDuplicateCommit(uncommittedEvents.Id)) { throw new CommitIsADuplicate(); } else { throw new EventSourceConcurrencyConflict(); } } else { throw new Exception("Unknown error type"); } } catch (Exception ex) { _logger.Error(ex, "Exception committing event stream"); throw; } })); }