title: AASM state machine exception handling example?
body: <p>I'm currently working on a class, which is basically doing following:</p>
  
  <ul>
  <li>model gets created</li>
  <li>fetches data (event "get_things!")
  
  <ul>
  <li>if exception happens, state should become "failed"</li>
  <li>if success, state should be "finished"</li>
  </ul></li>
  </ul>
  
  <p>I try to implement it as following:</p>
  
  <pre><code>class Fetcher &lt; ActiveRecord::Base
    include AASM
  
    aasm do
      state :created, initial: true
      state :success, :failed
  
      event :succeed do
        transitions from: :created, to: :success
      end
  
      event :fail do
        transitions from: :created, to: :failed
      end
    end
  
    def read_things!(throw_exception = false)
      begin
        raise RuntimeError.new("RAISED EXCEPTION") if throw_exception
        self.content = open("https://example.com?asd=324").read
        self.succeed!
      rescue =&gt; e
        self.fail!
      end
    end
  end
  
  a = Fetcher.new
  a.read_things!(throw_exception = true)
  =&gt; state should be failed
  
  a = Fetcher.new
  a.read_things!(throw_exception = false)
  =&gt; state should be succeess
  </code></pre>
  
  <p>It works, but looks somehow not really good to do...</p>
  
  <p>I would prefer something like the error handling which is mentioned in the readme</p>
  
  <pre><code>event :read_things do
    before do
      self.content = open("https://example.com?asd=324").read
      self.succeed!
    end
    error do |e|
      self.fail!
    end
    transitions :from =&gt; :created, :to =&gt; :success
  end
  </code></pre>
  
  <p>but I dont know if this is really the best practice here?</p>
  
  <p>I also have many events, which all should behave like my mentioned error handling is showing above and I saw that I can somehow use error_on_all_events - but didnt find any documentation about it?</p>
  
  <p>Any thoughts? Thanks!</p>
  
  <p><em>edit:</em> Changed some small parts to remove confusion.</p>