-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ def reset | |
) | ||
|
||
@middleware = MiddlewareStack.new | ||
@middleware.use Middleware::GemRootFilter.new | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module Telebugs | ||
class Middleware | ||
# GemRootFilter is a middleware that filters out the root path of the gems. | ||
# It replaces the root path with the gem name and version. | ||
class GemRootFilter < Telebugs::Middleware | ||
def initialize | ||
@gem_paths = Gem.path.map { |path| /\A#{Regexp.escape(path)}\/gems\// } | ||
end | ||
|
||
def call(report) | ||
report.data[:errors].each do |error| | ||
process_backtrace(error[:backtrace]) | ||
end | ||
end | ||
|
||
def weight | ||
-999 | ||
end | ||
|
||
private | ||
|
||
def process_backtrace(backtrace) | ||
backtrace.each do |frame| | ||
next unless (file = frame[:file]) | ||
|
||
process_frame(frame, file) | ||
end | ||
end | ||
|
||
def process_frame(frame, file) | ||
@gem_paths.each do |gem_path_regex| | ||
next unless file&.match?(gem_path_regex) | ||
|
||
frame[:file] = format_file(file.sub(gem_path_regex, "")) | ||
break | ||
end | ||
end | ||
|
||
def format_file(file) | ||
parts = file.split("/") | ||
|
||
gem_info = parts[0].split("-") | ||
gem_name = gem_info[0] | ||
gem_version = gem_info[1] | ||
|
||
file_path = parts[1..].join("/") | ||
|
||
"#{gem_name} (#{gem_version}) #{file_path}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class TestGemRootFilter < Minitest::Test | ||
def test_gem_root_filter | ||
begin | ||
raise StandardError.new("test error") | ||
rescue StandardError => e | ||
report = Telebugs::Report.new(e) | ||
end | ||
|
||
Telebugs::Middleware::GemRootFilter.new.call(report) | ||
|
||
assert_match( | ||
%r{/test/middleware/test_gem_root_filter.rb}, | ||
report.data[:errors][0][:backtrace][0][:file] | ||
) | ||
assert_match( | ||
%r{\Aminitest \(.+\..+\..+\) lib/minitest/test.rb\z}, | ||
report.data[:errors][0][:backtrace][1][:file] | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters