Skip to content

Commit

Permalink
fix: return error in projector for removed models
Browse files Browse the repository at this point in the history
  • Loading branch information
maxekman committed Nov 22, 2021
1 parent 005a02f commit f6db37c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions eventhandler/projector/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func (t Type) String() string {
var (
// ErrModelNotSet is when a model factory is not set on the EventHandler.
ErrModelNotSet = errors.New("model not set")
// ErrModelRemoved is when a model has been removed.
ErrModelRemoved = errors.New("model removed")
// Returned if the model has not incremented its version as predicted.
ErrIncorrectProjectedEntityVersion = errors.New("incorrect projected entity version")
)
Expand Down Expand Up @@ -251,6 +253,16 @@ retryOnce:
goto retryOnce
}

if entityVersion == 0 && event.Version() > 1 {
return &Error{
Err: ErrModelRemoved,
Projector: h.projector.ProjectorType().String(),
Event: event,
EntityID: id,
EntityVersion: entityVersion,
}
}

return &Error{
Err: eh.ErrIncorrectEntityVersion,
Projector: h.projector.ProjectorType().String(),
Expand Down
27 changes: 27 additions & 0 deletions eventhandler/projector/eventhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,33 @@ func TestEventHandler_DeleteModel(t *testing.T) {
}
}

func TestEventHandler_UpdateModelAfterDelete(t *testing.T) {
repo := &mocks.Repo{}
projector := &TestProjector{}
handler := NewEventHandler(projector, repo)
handler.SetEntityFactory(func() eh.Entity {
return &mocks.Model{}
})

// Entity must be versionable, but empty.
repo.Entity = &mocks.Model{}

ctx := context.Background()

id := uuid.New()
eventData := &mocks.EventData{Content: "event1"}
timestamp := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
updateEvent := eh.NewEvent(mocks.EventType, eventData, timestamp,
eh.ForAggregate(mocks.AggregateType, id, 3))

err := handler.HandleEvent(ctx, updateEvent)

errType := &Error{}
if !errors.As(err, &errType) || !errors.Is(err, ErrModelRemoved) {
t.Error("there should be an error:", err)
}
}

func TestEventHandler_LoadError(t *testing.T) {
repo := &mocks.Repo{}
projector := &TestProjector{}
Expand Down

0 comments on commit f6db37c

Please sign in to comment.