Skip to content

Commit

Permalink
feat: [RELEASE] improved InvalidTransition error to include aggregate_id
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-M-Judd committed Mar 27, 2024
1 parent c21786d commit 7262e41
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## 1.4.0 - 2024-03-01
### Changed
- Improved InvalidTransition error to include the aggreate_id of the transition that failed

## 1.3.2 - 2024-03-01
### Changed
- Updated dependencies to fix a security vulnerability
Expand Down
2 changes: 1 addition & 1 deletion lib/eventsimple/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def perform_transition_checks
return if can_apply?(aggregate)

_on_invalid_transition.call(
Eventsimple::InvalidTransition.new(self.class),
Eventsimple::InvalidTransition.new(self.class, aggregate_id),
)

raise ActiveRecord::Rollback
Expand Down
9 changes: 5 additions & 4 deletions lib/eventsimple/invalid_transition.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module Eventsimple
class InvalidTransition < StandardError
attr_reader :klass
attr_reader :klass, :aggregate_id

def initialize(klass = nil)
def initialize(klass = nil, aggregate_id = nil)
@klass = klass
super
@aggregate_id = aggregate_id
super(klass)
end

def to_s
"Invalid State Transition for #{klass}"
"Invalid State Transition for #{klass} on aggregate_id: #{aggregate_id}"
end
end
end
10 changes: 10 additions & 0 deletions lib/eventsimple/support/spec_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
event.class, :count
)
end

it 'raises an event with the correct error message' do
allow(Eventsimple::InvalidTransition).to receive(:new).and_call_original

expect { event.save }.to raise_error do |e|
# make a string assertion with match instead of anything
# use regex pattern
expect(e.message).to match(/Invalid State Transition for .* on aggregate_id: #{event.aggregate_id}/)
end
end
end

RSpec.shared_examples 'an event in invalid state that is rescued' do
Expand Down
2 changes: 1 addition & 1 deletion lib/eventsimple/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Eventsimple
VERSION = '1.3.2'
VERSION = '1.4.0'
end

0 comments on commit 7262e41

Please sign in to comment.