reference state machine object and event in invalid transition error so end user can intelligently handle

This commit is contained in:
Luke Roberts 2015-03-01 20:08:45 -08:00
parent 123700ce4e
commit 5a8454221f
3 changed files with 19 additions and 3 deletions

View File

@ -129,7 +129,7 @@ private
end
if AASM::StateMachine[self.class].config.whiny_transitions
raise AASM::InvalidTransition, "Event '#{event_name}' cannot transition from '#{aasm.current_state}'"
raise AASM::InvalidTransition.new(self, event_name)
else
false
end

View File

@ -1,5 +1,16 @@
module AASM
class InvalidTransition < RuntimeError; end
class InvalidTransition < RuntimeError
attr_reader :object, :event_name
def initialize(object, event_name)
@object, @event_name = object, event_name
end
def message
"Event '#{event_name}' cannot transition from '#{object.aasm.current_state}'"
end
end
class UndefinedState < RuntimeError; end
class NoDirectAssignmentError < RuntimeError; end
end

View File

@ -4,7 +4,12 @@ describe 'transitions' do
it 'should raise an exception when whiny' do
process = ProcessWithNewDsl.new
expect { process.stop! }.to raise_error(AASM::InvalidTransition)
expect { process.stop! }.to raise_error do |err|
expect(err.class).to eql(AASM::InvalidTransition)
expect(err.message).to eql("Event 'stop' cannot transition from 'sleeping'")
expect(err.object).to eql(process)
expect(err.event_name).to eql(:stop)
end
expect(process).to be_sleeping
end