Skip to content

Commit

Permalink
Merge pull request #499 from gocardless/joesouthan/guard-location
Browse files Browse the repository at this point in the history
Add the source location of the Guard callback to GuardFailedError
  • Loading branch information
JoeSouthan authored Mar 10, 2023
2 parents c45b6ca + dc1619b commit 58a02d1
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ jobs:
MYSQL_DATABASE: statesman_test
ports:
- "3306:3306"
options: >-
--health-cmd "mysqladmin ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
DATABASE_URL: mysql2://foobar:password@127.0.0.1/statesman_test
DATABASE_DEPENDENCY_PORT: "3306"
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v10.1.0 10th March 2023

### CHanged
- Add the source location of the guard callback to `Statesman::GuardFailedError`

## v10.0.0 17th May 2022

### Changed
Expand Down
6 changes: 4 additions & 2 deletions lib/statesman/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ def _message
end

class GuardFailedError < StandardError
def initialize(from, to)
def initialize(from, to, callback)
@from = from
@to = to
@callback = callback
super(_message)
set_backtrace(callback.source_location.join(":")) if callback&.source_location
end

attr_reader :from, :to
attr_reader :from, :to, :callback

private

Expand Down
2 changes: 1 addition & 1 deletion lib/statesman/guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
module Statesman
class Guard < Callback
def call(*args)
raise GuardFailedError.new(from, to) unless super(*args)
raise GuardFailedError.new(from, to, callback) unless super(*args)
end
end
end
2 changes: 1 addition & 1 deletion lib/statesman/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Statesman
VERSION = "10.0.0"
VERSION = "10.1.0"
end
8 changes: 4 additions & 4 deletions spec/statesman/adapters/active_record_queries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,17 @@ def configure_new(klass, transition_class)
subject(:not_in_state) { MyActiveRecordModel.not_in_state(:succeeded, :failed) }

it do
expect(not_in_state).to match_array([initial_state_model,
returned_to_initial_model])
expect(not_in_state).to contain_exactly(initial_state_model,
returned_to_initial_model)
end
end

context "given an array of states" do
subject(:not_in_state) { MyActiveRecordModel.not_in_state(%i[succeeded failed]) }

it do
expect(not_in_state).to match_array([initial_state_model,
returned_to_initial_model])
expect(not_in_state).to contain_exactly(initial_state_model,
returned_to_initial_model)
end
end
end
Expand Down
8 changes: 7 additions & 1 deletion spec/statesman/exceptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,18 @@
end

describe "GuardFailedError" do
subject(:error) { Statesman::GuardFailedError.new("from", "to") }
subject(:error) { Statesman::GuardFailedError.new("from", "to", callback) }

let(:callback) { -> { "hello" } }

its(:message) do
is_expected.to eq("Guard on transition from: 'from' to 'to' returned false")
end

its(:backtrace) do
is_expected.to eq([callback.source_location.join(":")])
end

its "string matches its message" do
expect(error.to_s).to eq(error.message)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/statesman/machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -935,10 +935,10 @@ def after_initialize; end
it { is_expected.to be(:some_state) }
end

context "when it is unsuccesful" do
context "when it is unsuccessful" do
before do
allow(instance).to receive(:transition_to!).
and_raise(Statesman::GuardFailedError.new(:x, :some_state))
and_raise(Statesman::GuardFailedError.new(:x, :some_state, nil))
end

it { is_expected.to be_falsey }
Expand Down

0 comments on commit 58a02d1

Please sign in to comment.