From c8b88a451332c76b851cc95976b751b4bf93ae40 Mon Sep 17 00:00:00 2001 From: "Tj (bougyman) Vanderpoel" Date: Sat, 16 Aug 2025 10:46:26 -0500 Subject: [PATCH 1/5] fix: #35 - Limits the backtrace to 4 lines in our exceptions --- lib/leopard/errors.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/leopard/errors.rb b/lib/leopard/errors.rb index a66b24c..15f1ac9 100644 --- a/lib/leopard/errors.rb +++ b/lib/leopard/errors.rb @@ -2,7 +2,13 @@ module Rubyists module Leopard - class Error < StandardError; end + class LeopardError < StandardError + def backtrace + super[0..3] + ['... (truncated by Leopard)'] + end + end + + class Error < LeopardError; end class ConfigurationError < Error; end class ResultError < Error; end end From 1d8f611a23484481e5419d215fd9b9552478243f Mon Sep 17 00:00:00 2001 From: "Tj (bougyman) Vanderpoel" Date: Sat, 16 Aug 2025 11:12:50 -0500 Subject: [PATCH 2/5] fix: #35 - Adds test for backtrace truncation --- Gemfile.lock | 2 +- lib/leopard/errors.rb | 5 +++++ test/lib/errors.rb | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/lib/errors.rb diff --git a/Gemfile.lock b/Gemfile.lock index 96ee7e6..b9e73f1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - leopard (0.1.7) + leopard (0.2.1) concurrent-ruby (~> 1.1) dry-configurable (~> 1.3) dry-monads (~> 1.9) diff --git a/lib/leopard/errors.rb b/lib/leopard/errors.rb index 15f1ac9..d6cef38 100644 --- a/lib/leopard/errors.rb +++ b/lib/leopard/errors.rb @@ -3,6 +3,11 @@ module Rubyists module Leopard class LeopardError < StandardError + def initialize(...) + super + set_backtrace(caller) + end + def backtrace super[0..3] + ['... (truncated by Leopard)'] end diff --git a/test/lib/errors.rb b/test/lib/errors.rb new file mode 100644 index 0000000..f772f7b --- /dev/null +++ b/test/lib/errors.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'helper' +require 'leopard/errors' + +describe 'Rubyists::Leopard::Errors' do + def level1 + level2 + end + + def level2 + level3 + end + + def level3 + raise Rubyists::Leopard::Error, 'Error boom' + end + + it 'truncates backtrace to 5 items' do + err = assert_raises(Rubyists::Leopard::Error) { level1 } + + bt = err.backtrace + + assert_equal 5, bt.count + assert_equal '... (truncated by Leopard)', bt.last + refute(bt.any? { |line| line.include?("in 'level1'") }) + end +end From e20d7f2d5f975c129d9f3631dbacc63daf011607 Mon Sep 17 00:00:00 2001 From: "Tj (bougyman) Vanderpoel" Date: Sat, 16 Aug 2025 11:16:24 -0500 Subject: [PATCH 3/5] fix: #35 - Adds nil check in case backtrace is empty --- lib/leopard/errors.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/leopard/errors.rb b/lib/leopard/errors.rb index d6cef38..19f76cd 100644 --- a/lib/leopard/errors.rb +++ b/lib/leopard/errors.rb @@ -9,7 +9,7 @@ def initialize(...) end def backtrace - super[0..3] + ['... (truncated by Leopard)'] + (super || [])[0..3] + ['... (truncated by Leopard)'] end end From 06c7e6dbb0c6acc77258abfec9afc004d755dec4 Mon Sep 17 00:00:00 2001 From: "Tj (bougyman) Vanderpoel" Date: Sat, 16 Aug 2025 11:17:18 -0500 Subject: [PATCH 4/5] chore: Bumps leopard version in bundle --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index b9e73f1..d9d88fd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - leopard (0.2.1) + leopard (0.2.2) concurrent-ruby (~> 1.1) dry-configurable (~> 1.3) dry-monads (~> 1.9) From 027fcd07241641d207d0e0a612ea2f185c88d0d2 Mon Sep 17 00:00:00 2001 From: "Tj (bougyman) Vanderpoel" Date: Sat, 16 Aug 2025 11:21:42 -0500 Subject: [PATCH 5/5] fix: #35 - Do not add a truncate message if we did not truncate it --- lib/leopard/errors.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/leopard/errors.rb b/lib/leopard/errors.rb index 19f76cd..985a373 100644 --- a/lib/leopard/errors.rb +++ b/lib/leopard/errors.rb @@ -9,7 +9,13 @@ def initialize(...) end def backtrace - (super || [])[0..3] + ['... (truncated by Leopard)'] + # If the backtrace is nil, return an empty array + orig = (super || [])[0..3] + # If the backtrace is less than 4 lines, return it as is + return orig if orig.size < 4 + + # Otherwise, add a note indicating truncation + orig + ['... (truncated by Leopard)'] end end