From c790c574155265121c12d79d1b63c116a652be85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guitaut?= Date: Thu, 13 Jun 2024 11:55:57 +0200 Subject: [PATCH] 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. --- app/services/service/base.rb | 6 +++++- lib/steps_inspector.rb | 2 +- spec/lib/steps_inspector_spec.rb | 4 ++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/services/service/base.rb b/app/services/service/base.rb index 6c8f608b7fe..531169f44cd 100644 --- a/app/services/service/base.rb +++ b/app/services/service/base.rb @@ -177,7 +177,7 @@ module Service context[contract_name] = contract context[result_key] = Context.build if contract.invalid? - context[result_key].fail(errors: contract.errors) + context[result_key].fail(errors: contract.errors, parameters: contract.raw_attributes) context.fail! end end @@ -218,6 +218,10 @@ module Service include ActiveModel::Attributes include ActiveModel::AttributeMethods include ActiveModel::Validations::Callbacks + + def raw_attributes + @attributes.values_before_type_cast + end end end diff --git a/lib/steps_inspector.rb b/lib/steps_inspector.rb index 80b5438563d..61f71a830d5 100644 --- a/lib/steps_inspector.rb +++ b/lib/steps_inspector.rb @@ -73,7 +73,7 @@ class StepsInspector # @!visibility private class Contract < Step def error - step_result.errors.inspect + "#{step_result.errors.inspect}\n\nProvided parameters: #{step_result.parameters.pretty_inspect}" end end diff --git a/spec/lib/steps_inspector_spec.rb b/spec/lib/steps_inspector_spec.rb index 534d0356140..38227239a7d 100644 --- a/spec/lib/steps_inspector_spec.rb +++ b/spec/lib/steps_inspector_spec.rb @@ -219,6 +219,10 @@ RSpec.describe StepsInspector do it "returns an error related to the contract" do expect(error).to match(/ActiveModel::Error attribute=parameter, type=blank, options={}/) end + + it "returns the provided paramaters" do + expect(error).to match(/{"parameter"=>nil}/) + end end context "when the policy step is failing" do