Skip to content

Commit df0fee8

Browse files
authored
fix: #35 - Limits the backtrace to 4 lines in our exceptions (#36)
* fix: #35 - Limits the backtrace to 4 lines in our exceptions * fix: #35 - Adds test for backtrace truncation * fix: #35 - Adds nil check in case backtrace is empty * chore: Bumps leopard version in bundle * fix: #35 - Do not add a truncate message if we did not truncate it
1 parent e1e5c8c commit df0fee8

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
leopard (0.1.7)
4+
leopard (0.2.2)
55
concurrent-ruby (~> 1.1)
66
dry-configurable (~> 1.3)
77
dry-monads (~> 1.9)

lib/leopard/errors.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,24 @@
22

33
module Rubyists
44
module Leopard
5-
class Error < StandardError; end
5+
class LeopardError < StandardError
6+
def initialize(...)
7+
super
8+
set_backtrace(caller)
9+
end
10+
11+
def backtrace
12+
# If the backtrace is nil, return an empty array
13+
orig = (super || [])[0..3]
14+
# If the backtrace is less than 4 lines, return it as is
15+
return orig if orig.size < 4
16+
17+
# Otherwise, add a note indicating truncation
18+
orig + ['... (truncated by Leopard)']
19+
end
20+
end
21+
22+
class Error < LeopardError; end
623
class ConfigurationError < Error; end
724
class ResultError < Error; end
825
end

test/lib/errors.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require 'helper'
4+
require 'leopard/errors'
5+
6+
describe 'Rubyists::Leopard::Errors' do
7+
def level1
8+
level2
9+
end
10+
11+
def level2
12+
level3
13+
end
14+
15+
def level3
16+
raise Rubyists::Leopard::Error, 'Error boom'
17+
end
18+
19+
it 'truncates backtrace to 5 items' do
20+
err = assert_raises(Rubyists::Leopard::Error) { level1 }
21+
22+
bt = err.backtrace
23+
24+
assert_equal 5, bt.count
25+
assert_equal '... (truncated by Leopard)', bt.last
26+
refute(bt.any? { |line| line.include?("in 'level1'") })
27+
end
28+
end

0 commit comments

Comments
 (0)