From fb96b9230da7be5c24210e76df01c8c468124dff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Pacana?= Date: Wed, 22 Nov 2023 14:15:11 +0100 Subject: [PATCH] Communicate with exceptions Co-authored-by: Szymon Fiedler Co-authored-by: Piotr Jurewicz --- .../lib/project_management/command_handler.rb | 17 +++---- .../decider/lib/project_management/issue.rb | 49 +++++++------------ 2 files changed, 24 insertions(+), 42 deletions(-) diff --git a/examples/decider/lib/project_management/command_handler.rb b/examples/decider/lib/project_management/command_handler.rb index d539213..936a229 100644 --- a/examples/decider/lib/project_management/command_handler.rb +++ b/examples/decider/lib/project_management/command_handler.rb @@ -17,16 +17,13 @@ def handle(cmd) version = idx end - case result = decider.decide(cmd) - when StandardError - raise Error - else - @event_store.publish( - result, - stream_name: stream_name(cmd.id), - expected_version: version - ) - end + @event_store.publish( + decider.decide(cmd), + stream_name: stream_name(cmd.id), + expected_version: version + ) + rescue Issue::InvalidTransition + raise Error end private diff --git a/examples/decider/lib/project_management/issue.rb b/examples/decider/lib/project_management/issue.rb index 93154ed..39734ba 100644 --- a/examples/decider/lib/project_management/issue.rb +++ b/examples/decider/lib/project_management/issue.rb @@ -1,6 +1,7 @@ module ProjectManagement class Issue InvalidTransition = Class.new(StandardError) + State = Data.define(:id, :status) private_constant :State @@ -46,53 +47,37 @@ def evolve(event) private def open - if @state.status - InvalidTransition.new - else - IssueOpened.new(data: { issue_id: @state.id }) - end + fail if @state.status + IssueOpened.new(data: { issue_id: @state.id }) end def resolve - if %i[open in_progress reopened].include? @state.status - IssueResolved.new(data: { issue_id: @state.id }) - else - InvalidTransition.new - end + fail unless %i[open in_progress reopened].include? @state.status + IssueResolved.new(data: { issue_id: @state.id }) end def close - if %i[open in_progress resolved reopened].include? @state.status - IssueClosed.new(data: { issue_id: @state.id }) - else - InvalidTransition.new - end + fail unless %i[open in_progress resolved reopened].include? @state.status + IssueClosed.new(data: { issue_id: @state.id }) end def reopen - if %i[resolved closed].include? @state.status - IssueReopened.new(data: { issue_id: @state.id }) - else - InvalidTransition.new - end + fail unless %i[resolved closed].include? @state.status + IssueReopened.new(data: { issue_id: @state.id }) end - def stop - if %i[in_progress].include? @state.status - IssueProgressStopped.new(data: { issue_id: @state.id }) - else - InvalidTransition.new - end + def start + fail unless %i[open reopened].include? @state.status + IssueProgressStarted.new(data: { issue_id: @state.id }) end - def start - if %i[open reopened].include? @state.status - IssueProgressStarted.new(data: { issue_id: @state.id }) - else - InvalidTransition.new - end + def stop + fail unless %i[in_progress].include? @state.status + IssueProgressStopped.new(data: { issue_id: @state.id }) end + def fail = raise InvalidTransition + def initial_state(id) = State.new(id: id, status: nil) end end