-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement code hunks support #258
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,48 @@ | ||
module Airbrake | ||
## | ||
# Represents a small hunk of code consisting of a base line and a couple lines | ||
# around it | ||
# @api private | ||
class CodeHunk | ||
## | ||
# @return [Integer] the maximum length of a line | ||
MAX_LINE_LEN = 200 | ||
|
||
## | ||
# @return [Integer] how many lines should be read around the base line | ||
NLINES = 2 | ||
|
||
def initialize(config) | ||
@config = config | ||
end | ||
|
||
## | ||
# @param [String] file The file to read | ||
# @param [Integer] line The base line in the file | ||
# @return [Hash{Integer=>String}, nil] lines of code around the base line | ||
def get(file, line) | ||
return unless File.exist?(file) | ||
|
||
start_line = [line - NLINES, 1].max | ||
end_line = line + NLINES | ||
lines = {} | ||
|
||
begin | ||
File.foreach(file).with_index(1) do |l, i| | ||
next if i < start_line | ||
break if i > end_line | ||
|
||
lines[i] = l[0...MAX_LINE_LEN].rstrip | ||
end | ||
rescue StandardError => ex | ||
@config.logger.error( | ||
"#{self.class.name}##{__method__}: can't read code hunk for " \ | ||
"#{file}:#{line}: #{ex}" | ||
) | ||
end | ||
|
||
return { 1 => '' } if lines.empty? | ||
lines | ||
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
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
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 |
---|---|---|
@@ -1,6 +1,10 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Airbrake::Backtrace do | ||
let(:config) do | ||
Airbrake::Config.new.tap { |c| c.logger = Logger.new('/dev/null') } | ||
end | ||
|
||
describe ".parse" do | ||
context "UNIX backtrace" do | ||
let(:parsed_backtrace) do | ||
|
@@ -23,7 +27,7 @@ | |
|
||
it "returns a properly formatted array of hashes" do | ||
expect( | ||
described_class.parse(AirbrakeTestError.new, Logger.new('/dev/null')) | ||
described_class.parse(config, AirbrakeTestError.new) | ||
).to eq(parsed_backtrace) | ||
end | ||
end | ||
|
@@ -44,9 +48,7 @@ | |
end | ||
|
||
it "returns a properly formatted array of hashes" do | ||
expect( | ||
described_class.parse(ex, Logger.new('/dev/null')) | ||
).to eq(parsed_backtrace) | ||
expect(described_class.parse(config, ex)).to eq(parsed_backtrace) | ||
end | ||
end | ||
|
||
|
@@ -71,7 +73,7 @@ | |
allow(described_class).to receive(:java_exception?).and_return(true) | ||
|
||
expect( | ||
described_class.parse(JavaAirbrakeTestError.new, Logger.new('/dev/null')) | ||
described_class.parse(config, JavaAirbrakeTestError.new) | ||
).to eq(backtrace_array) | ||
end | ||
end | ||
|
@@ -99,10 +101,7 @@ | |
|
||
it "returns a properly formatted array of hashes" do | ||
allow(described_class).to receive(:java_exception?).and_return(true) | ||
|
||
expect( | ||
described_class.parse(ex, Logger.new('/dev/null')) | ||
).to eq(parsed_backtrace) | ||
expect(described_class.parse(config, ex)).to eq(parsed_backtrace) | ||
end | ||
end | ||
|
||
|
@@ -126,9 +125,7 @@ | |
let(:ex) { AirbrakeTestError.new.tap { |e| e.set_backtrace(backtrace) } } | ||
|
||
it "returns a properly formatted array of hashes" do | ||
expect( | ||
described_class.parse(ex, Logger.new('/dev/null')) | ||
).to eq(parsed_backtrace) | ||
expect(described_class.parse(config, ex)).to eq(parsed_backtrace) | ||
end | ||
end | ||
|
||
|
@@ -153,9 +150,7 @@ | |
end | ||
|
||
it "returns a properly formatted array of hashes" do | ||
expect( | ||
described_class.parse(ex, Logger.new('/dev/null')) | ||
).to eq(parsed_backtrace) | ||
expect(described_class.parse(config, ex)).to eq(parsed_backtrace) | ||
end | ||
end | ||
|
||
|
@@ -173,9 +168,7 @@ | |
end | ||
|
||
it "returns a properly formatted array of hashes" do | ||
expect( | ||
described_class.parse(ex, Logger.new('/dev/null')) | ||
).to eq(parsed_backtrace) | ||
expect(described_class.parse(config, ex)).to eq(parsed_backtrace) | ||
end | ||
end | ||
end | ||
|
@@ -187,15 +180,15 @@ | |
|
||
it "returns array of hashes where each unknown frame is marked as 'function'" do | ||
expect( | ||
described_class.parse(ex, Logger.new('/dev/null')) | ||
described_class.parse(config, ex) | ||
).to eq([file: nil, line: nil, function: 'a b c 1 23 321 .rb']) | ||
end | ||
|
||
it "logs unknown frames as errors" do | ||
out = StringIO.new | ||
logger = Logger.new(out) | ||
config.logger = Logger.new(out) | ||
|
||
expect { described_class.parse(ex, logger) }. | ||
expect { described_class.parse(config, ex) }. | ||
to change { out.string }. | ||
from(''). | ||
to(/ERROR -- : can't parse 'a b c 1 23 321 .rb'/) | ||
|
@@ -216,9 +209,7 @@ | |
end | ||
|
||
it "returns a properly formatted array of hashes" do | ||
expect( | ||
described_class.parse(ex, Logger.new('/dev/null')) | ||
).to eq(parsed_backtrace) | ||
expect(described_class.parse(config, ex)).to eq(parsed_backtrace) | ||
end | ||
end | ||
|
||
|
@@ -241,9 +232,7 @@ | |
|
||
it "returns a properly formatted array of hashes" do | ||
stub_const('OCIError', AirbrakeTestError) | ||
expect( | ||
described_class.parse(ex, Logger.new('/dev/null')) | ||
).to eq(parsed_backtrace) | ||
expect(described_class.parse(config, ex)).to eq(parsed_backtrace) | ||
end | ||
end | ||
|
||
|
@@ -279,9 +268,7 @@ | |
stub_const('ExecJS::RuntimeError', AirbrakeTestError) | ||
stub_const('Airbrake::RUBY_20', false) | ||
|
||
expect( | ||
described_class.parse(ex, Logger.new('/dev/null')) | ||
).to eq(parsed_backtrace) | ||
expect(described_class.parse(config, ex)).to eq(parsed_backtrace) | ||
end | ||
end | ||
|
||
|
@@ -296,12 +283,44 @@ | |
stub_const('ExecJS::RuntimeError', NameError) | ||
stub_const('Airbrake::RUBY_20', true) | ||
|
||
expect( | ||
described_class.parse(ex, Logger.new('/dev/null')) | ||
).to eq(parsed_backtrace) | ||
expect(described_class.parse(config, ex)).to eq(parsed_backtrace) | ||
end | ||
end | ||
end | ||
end | ||
|
||
context "when code hunks are enabled" do | ||
let(:config) do | ||
config = Airbrake::Config.new | ||
config.logger = Logger.new('/dev/null') | ||
config.code_hunks = true | ||
config | ||
end | ||
|
||
let(:parsed_backtrace) do | ||
[ | ||
{ | ||
file: File.join(fixture_path('code.rb')), | ||
line: 94, | ||
function: 'to_json', | ||
code: { | ||
92 => ' loop do', | ||
93 => ' begin', | ||
94 => ' json = @payload.to_json', | ||
95 => ' rescue *JSON_EXCEPTIONS => ex', | ||
# rubocop:disable Metrics/LineLength,Lint/InterpolationCheck | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lint/UnneededDisable: Unnecessary disabling of Lint/InterpolationCheck (did you mean Lint/LiteralInCondition?), Metrics/LineLength. |
||
96 => ' @config.logger.debug("#{LOG_LABEL} `notice.to_json` failed: #{ex.class}: #{ex}")', | ||
# rubocop:enable Metrics/LineLength,Lint/InterpolationCheck | ||
} | ||
} | ||
] | ||
end | ||
|
||
it "attaches code to each frame" do | ||
ex = RuntimeError.new | ||
ex.set_backtrace([File.join(fixture_path('code.rb') + ":94:in `to_json'")]) | ||
expect(described_class.parse(config, ex)).to eq(parsed_backtrace) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lint/UnneededDisable: Unnecessary disabling of Lint/InterpolationCheck (did you mean Lint/LiteralInCondition?).