DEV: Show parameters on a service contract failure

Now, when calling `StepsInspector#error` on a contract failure, the
output will contain the parameters provided to the contract.
This commit is contained in:
Loïc Guitaut 2024-06-13 11:55:57 +02:00 committed by Loïc Guitaut
parent 47540fb4e0
commit c790c57415
3 changed files with 10 additions and 2 deletions

View File

@ -177,7 +177,7 @@ module Service
context[contract_name] = contract context[contract_name] = contract
context[result_key] = Context.build context[result_key] = Context.build
if contract.invalid? if contract.invalid?
context[result_key].fail(errors: contract.errors) context[result_key].fail(errors: contract.errors, parameters: contract.raw_attributes)
context.fail! context.fail!
end end
end end
@ -218,6 +218,10 @@ module Service
include ActiveModel::Attributes include ActiveModel::Attributes
include ActiveModel::AttributeMethods include ActiveModel::AttributeMethods
include ActiveModel::Validations::Callbacks include ActiveModel::Validations::Callbacks
def raw_attributes
@attributes.values_before_type_cast
end
end end
end end

View File

@ -73,7 +73,7 @@ class StepsInspector
# @!visibility private # @!visibility private
class Contract < Step class Contract < Step
def error def error
step_result.errors.inspect "#{step_result.errors.inspect}\n\nProvided parameters: #{step_result.parameters.pretty_inspect}"
end end
end end

View File

@ -219,6 +219,10 @@ RSpec.describe StepsInspector do
it "returns an error related to the contract" do it "returns an error related to the contract" do
expect(error).to match(/ActiveModel::Error attribute=parameter, type=blank, options={}/) expect(error).to match(/ActiveModel::Error attribute=parameter, type=blank, options={}/)
end end
it "returns the provided paramaters" do
expect(error).to match(/{"parameter"=>nil}/)
end
end end
context "when the policy step is failing" do context "when the policy step is failing" do