Skip to content

Commit

Permalink
Merge pull request #341 from pirj/fix-lazy-exception
Browse files Browse the repository at this point in the history
Fix lazy error message messing up originating state
  • Loading branch information
alto committed Mar 19, 2016
2 parents fc9584e + b891895 commit bca2d19
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/aasm/errors.rb
Expand Up @@ -3,18 +3,18 @@ module AASM
class UnknownStateMachineError < RuntimeError; end

class InvalidTransition < RuntimeError
attr_reader :object, :event_name, :state_machine_name, :failures
attr_reader :object, :event_name, :originating_state, :failures

def initialize(object, event_name, state_machine_name, failures = [])
@object, @event_name, @state_machine_name, @failures = object, event_name, state_machine_name, failures
@object, @event_name, @originating_state, @failures = object, event_name, object.aasm(state_machine_name).current_state, failures
end

def message
"Event '#{event_name}' cannot transition from '#{object.aasm(state_machine_name).current_state}'. #{reasoning}"
"Event '#{event_name}' cannot transition from '#{originating_state}'. #{reasoning}"
end

def reasoning
"Failed callback(s): #{@failures}." unless failures.empty?
"Failed callback(s): #{failures}." unless failures.empty?
end
end

Expand Down
11 changes: 11 additions & 0 deletions spec/unit/exception_spec.rb
@@ -0,0 +1,11 @@
require 'spec_helper'

describe AASM::InvalidTransition do
it 'should not be lazy detecting originating state' do
process = ProcessWithNewDsl.new
expect { process.stop! }.to raise_error do |err|
process.start
expect(err.message).to eql("Event 'stop' cannot transition from 'sleeping'. ")
end
end
end

0 comments on commit bca2d19

Please sign in to comment.