A platform for community discussion. Free, open, simple.
DEV: Allow to filter backtrace in exceptions caught in services (#37382)
Loïc Guitautaaadd986030e
diff --git a/lib/service/base.rb b/lib/service/base.rbindex 48bfc0a4799..3dd98571393 100644--- a/lib/service/base.rb+++ b/lib/service/base.rb@@ -331,6 +331,18 @@ module Service# @!visibility privateclass TryStep < Step+ module FilteredBacktrace+ def filtered_backtrace+ Array+ .wrap(backtrace)+ .chunk { _1.match?(%r{/(gems|lib/service|ruby)/}) }+ .flat_map do |excluded, lines|+ next "(#{lines.size} framework line(s) excluded)" if excluded+ lines+ end+ end+ end+include StepsHelpersattr_reader :steps, :exceptions@@ -349,6 +361,7 @@ module Serviceendrescue *exceptions => eraise e if e.is_a?(Failure)+ e.singleton_class.prepend(FilteredBacktrace)context[@current_step.result_key].fail(raised_exception?: true, exception: e)context[result_key][:exception] = econtext.fail!diff --git a/spec/lib/service/runner_spec.rb b/spec/lib/service/runner_spec.rbindex bf1a54b654d..4aa0c9d4103 100644--- a/spec/lib/service/runner_spec.rb+++ b/spec/lib/service/runner_spec.rb@@ -466,7 +466,7 @@ RSpec.describe Service::Runner docontext "when using the on_exceptions action" dolet(:actions) { <<-BLOCK }proc do |result|- on_exceptions { |exception| exception.message == "BOOM" }+ on_exceptions { |exception| exception }endBLOCK@@ -474,7 +474,13 @@ RSpec.describe Service::Runner dolet(:service) { FailureTryService }it "runs the provided block" do- expect(runner).to be true+ expect(runner).to be_a RuntimeError+ end++ context "when accessing the exception object" do+ it "has access to a filtered backtrace" do+ expect(runner.filtered_backtrace.size).to be < runner.backtrace.size+ endendend@@ -482,7 +488,7 @@ RSpec.describe Service::Runner dolet(:service) { SuccessTryService }it "does not run the provided block" do- expect(runner).not_to eq true+ expect(runner).not_to be_a RuntimeErrorendendend