-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathbacktrace_spec.rb
430 lines (374 loc) · 19.5 KB
/
backtrace_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
RSpec.describe Airbrake::Backtrace do
describe ".parse" do
context "UNIX backtrace" do
let(:parsed_backtrace) do
# rubocop:disable Layout/LineLength, Style/HashSyntax, Layout/SpaceAroundOperators, Layout/SpaceInsideHashLiteralBraces
[{:file=>"/home/kyrylo/code/airbrake/ruby/spec/spec_helper.rb", :line=>23, :function=>"<top (required)>"},
{:file=>"/opt/rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb", :line=>54, :function=>"require"},
{:file=>"/opt/rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb", :line=>54, :function=>"require"},
{:file=>"/home/kyrylo/code/airbrake/ruby/spec/airbrake_spec.rb", :line=>1, :function=>"<top (required)>"},
{:file=>"/home/kyrylo/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb", :line=>1327, :function=>"load"},
{:file=>"/home/kyrylo/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb", :line=>1327, :function=>"block in load_spec_files"},
{:file=>"/home/kyrylo/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb", :line=>1325, :function=>"each"},
{:file=>"/home/kyrylo/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb", :line=>1325, :function=>"load_spec_files"},
{:file=>"/home/kyrylo/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb", :line=>102, :function=>"setup"},
{:file=>"/home/kyrylo/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb", :line=>88, :function=>"run"},
{:file=>"/home/kyrylo/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb", :line=>73, :function=>"run"},
{:file=>"/home/kyrylo/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb", :line=>41, :function=>"invoke"},
{:file=>"/home/kyrylo/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/exe/rspec", :line=>4, :function=>"<main>"}]
# rubocop:enable Layout/LineLength, Style/HashSyntax, Layout/SpaceAroundOperators, Layout/SpaceInsideHashLiteralBraces
end
it "returns a properly formatted array of hashes" do
expect(described_class.parse(AirbrakeTestError.new))
.to eq(parsed_backtrace)
end
end
context "Windows backtrace" do
let(:windows_bt) do
["C:/Program Files/Server/app/models/user.rb:13:in `magic'",
"C:/Program Files/Server/app/controllers/users_controller.rb:8:in `index'"]
end
let(:ex) { AirbrakeTestError.new.tap { |e| e.set_backtrace(windows_bt) } }
let(:parsed_backtrace) do
# rubocop:disable Layout/LineLength, Style/HashSyntax, Layout/SpaceInsideHashLiteralBraces, Layout/SpaceAroundOperators
[{:file=>"C:/Program Files/Server/app/models/user.rb", :line=>13, :function=>"magic"},
{:file=>"C:/Program Files/Server/app/controllers/users_controller.rb", :line=>8, :function=>"index"}]
# rubocop:enable Layout/LineLength, Style/HashSyntax, Layout/SpaceInsideHashLiteralBraces, Layout/SpaceAroundOperators
end
it "returns a properly formatted array of hashes" do
expect(described_class.parse(ex)).to eq(parsed_backtrace)
end
end
context "JRuby Java exceptions" do
let(:backtrace_array) do
# rubocop:disable Layout/LineLength, Style/HashSyntax, Layout/SpaceInsideHashLiteralBraces, Layout/SpaceAroundOperators
[{:file=>"InstanceMethodInvoker.java", :line=>26, :function=>"org.jruby.java.invokers.InstanceMethodInvoker.call"},
{:file=>"Interpreter.java", :line=>126, :function=>"org.jruby.ir.interpreter.Interpreter.INTERPRET_EVAL"},
{:file=>"RubyKernel$INVOKER$s$0$3$eval19.gen", :line=>nil, :function=>"org.jruby.RubyKernel$INVOKER$s$0$3$eval19.call"},
{:file=>"RubyKernel$INVOKER$s$0$0$loop.gen", :line=>nil, :function=>"org.jruby.RubyKernel$INVOKER$s$0$0$loop.call"},
{:file=>"IRBlockBody.java", :line=>139, :function=>"org.jruby.runtime.IRBlockBody.doYield"},
{:file=>"RubyKernel$INVOKER$s$rbCatch19.gen", :line=>nil, :function=>"org.jruby.RubyKernel$INVOKER$s$rbCatch19.call"},
{:file=>"/opt/rubies/jruby-9.0.0.0/bin/irb", :line=>nil, :function=>"opt.rubies.jruby_minus_9_dot_0_dot_0_dot_0.bin.irb.invokeOther4:start"},
{:file=>"/opt/rubies/jruby-9.0.0.0/bin/irb", :line=>13, :function=>"opt.rubies.jruby_minus_9_dot_0_dot_0_dot_0.bin.irb.RUBY$script"},
{:file=>"Compiler.java", :line=>111, :function=>"org.jruby.ir.Compiler$1.load"},
{:file=>"Main.java", :line=>225, :function=>"org.jruby.Main.run"},
{:file=>"Main.java", :line=>197, :function=>"org.jruby.Main.main"}]
# rubocop:enable Layout/LineLength, Style/HashSyntax, Layout/SpaceInsideHashLiteralBraces, Layout/SpaceAroundOperators
end
it "returns a properly formatted array of hashes" do
allow(described_class).to receive(:java_exception?).and_return(true)
expect(described_class.parse(JavaAirbrakeTestError.new))
.to eq(backtrace_array)
end
end
context "JRuby classloader exceptions" do
let(:backtrace) do
# rubocop:disable Layout/LineLength
['uri_3a_classloader_3a_.META_minus_INF.jruby_dot_home.lib.ruby.stdlib.net.protocol.rbuf_fill(uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib/net/protocol.rb:158)',
'bin.processors.image_uploader.block in make_streams(bin/processors/image_uploader.rb:21)',
'uri_3a_classloader_3a_.gems.faye_minus_websocket_minus_0_dot_10_dot_5.lib.faye.websocket.api.invokeOther13:dispatch_event(uri_3a_classloader_3a_/gems/faye_minus_websocket_minus_0_dot_10_dot_5/lib/faye/websocket/uri:classloader:/gems/faye-websocket-0.10.5/lib/faye/websocket/api.rb:109)',
'tmp.jruby9022301782566983632extract.$dot.META_minus_INF.rails.file(/tmp/jruby9022301782566983632extract/./META-INF/rails.rb:13)']
# rubocop:enable Layout/LineLength
end
let(:parsed_backtrace) do
# rubocop:disable Layout/LineLength
[{ file: 'uri:classloader:/META-INF/jruby.home/lib/ruby/stdlib/net/protocol.rb', line: 158, function: 'uri_3a_classloader_3a_.META_minus_INF.jruby_dot_home.lib.ruby.stdlib.net.protocol.rbuf_fill' },
{ file: 'bin/processors/image_uploader.rb', line: 21, function: 'bin.processors.image_uploader.block in make_streams' },
{ file: 'uri_3a_classloader_3a_/gems/faye_minus_websocket_minus_0_dot_10_dot_5/lib/faye/websocket/uri:classloader:/gems/faye-websocket-0.10.5/lib/faye/websocket/api.rb', line: 109, function: 'uri_3a_classloader_3a_.gems.faye_minus_websocket_minus_0_dot_10_dot_5.lib.faye.websocket.api.invokeOther13:dispatch_event' },
{ file: '/tmp/jruby9022301782566983632extract/./META-INF/rails.rb', line: 13, function: 'tmp.jruby9022301782566983632extract.$dot.META_minus_INF.rails.file' }]
# rubocop:enable Layout/LineLength
end
let(:ex) { JavaAirbrakeTestError.new.tap { |e| e.set_backtrace(backtrace) } }
it "returns a properly formatted array of hashes" do
allow(described_class).to receive(:java_exception?).and_return(true)
expect(described_class.parse(ex)).to eq(parsed_backtrace)
end
end
context "JRuby non-throwable exceptions" do
let(:backtrace) do
# rubocop:disable Layout/LineLength
['org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(org/postgresql/core/v3/ConnectionFactoryImpl.java:257)',
'org.postgresql.core.ConnectionFactory.openConnection(org/postgresql/core/ConnectionFactory.java:65)',
'org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(org/postgresql/jdbc2/AbstractJdbc2Connection.java:149)']
# rubocop:enable Layout/LineLength
end
let(:parsed_backtrace) do
# rubocop:disable Layout/LineLength
[{ file: 'org/postgresql/core/v3/ConnectionFactoryImpl.java', line: 257, function: 'org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl' },
{ file: 'org/postgresql/core/ConnectionFactory.java', line: 65, function: 'org.postgresql.core.ConnectionFactory.openConnection' },
{ file: 'org/postgresql/jdbc2/AbstractJdbc2Connection.java', line: 149, function: 'org.postgresql.jdbc2.AbstractJdbc2Connection.<init>' }]
# rubocop:enable Layout/LineLength
end
let(:ex) { AirbrakeTestError.new.tap { |e| e.set_backtrace(backtrace) } }
it "returns a properly formatted array of hashes" do
expect(described_class.parse(ex)).to eq(parsed_backtrace)
end
end
context "generic backtrace" do
context "when function is absent" do
# rubocop:disable Layout/LineLength
let(:generic_bt) do
["/home/bingo/bango/assets/stylesheets/error_pages.scss:139:in `animation'",
"/home/bingo/bango/assets/stylesheets/error_pages.scss:139",
"/home/bingo/.gem/ruby/2.2.2/gems/sass-3.4.20/lib/sass/tree/visitors/perform.rb:349:in `block in visit_mixin'"]
end
# rubocop:enable Layout/LineLength
let(:ex) { AirbrakeTestError.new.tap { |e| e.set_backtrace(generic_bt) } }
let(:parsed_backtrace) do
# rubocop:disable Layout/LineLength, Style/HashSyntax, Layout/SpaceInsideHashLiteralBraces, Layout/SpaceAroundOperators
[{:file=>"/home/bingo/bango/assets/stylesheets/error_pages.scss", :line=>139, :function=>"animation"},
{:file=>"/home/bingo/bango/assets/stylesheets/error_pages.scss", :line=>139, :function=>nil},
{:file=>"/home/bingo/.gem/ruby/2.2.2/gems/sass-3.4.20/lib/sass/tree/visitors/perform.rb", :line=>349, :function=>"block in visit_mixin"}]
# rubocop:enable Layout/LineLength, Style/HashSyntax, Layout/SpaceInsideHashLiteralBraces, Layout/SpaceAroundOperators
end
it "returns a properly formatted array of hashes" do
expect(described_class.parse(ex)).to eq(parsed_backtrace)
end
end
context "when line is absent" do
let(:generic_bt) do
["/Users/grammakov/repositories/weintervene/config.ru:in `new'"]
end
let(:ex) { AirbrakeTestError.new.tap { |e| e.set_backtrace(generic_bt) } }
let(:parsed_backtrace) do
[{ file: '/Users/grammakov/repositories/weintervene/config.ru',
line: nil,
function: 'new' }]
end
it "returns a properly formatted array of hashes" do
expect(described_class.parse(ex)).to eq(parsed_backtrace)
end
end
end
context "unknown backtrace" do
let(:unknown_bt) { ['a b c 1 23 321 .rb'] }
let(:ex) { AirbrakeTestError.new.tap { |e| e.set_backtrace(unknown_bt) } }
it "returns array of hashes where each unknown frame is marked as 'function'" do
expect(
described_class.parse(ex),
).to eq([file: nil, line: nil, function: 'a b c 1 23 321 .rb'])
end
it "logs frames that cannot be parsed" do
allow(Airbrake::Loggable.instance).to receive(:error)
described_class.parse(ex)
expect(Airbrake::Loggable.instance).to have_received(:error).with(
/can't parse 'a b c 1 23 321 .rb'/,
)
end
end
context "given a backtrace with an empty function" do
let(:bt) do
["/airbrake-ruby/vendor/jruby/1.9/gems/rspec-core-3.4.1/exe/rspec:3:in `'"]
end
let(:ex) { AirbrakeTestError.new.tap { |e| e.set_backtrace(bt) } }
let(:parsed_backtrace) do
[{ file: '/airbrake-ruby/vendor/jruby/1.9/gems/rspec-core-3.4.1/exe/rspec',
line: 3,
function: '' }]
end
it "returns a properly formatted array of hashes" do
expect(described_class.parse(ex)).to eq(parsed_backtrace)
end
end
context "given an Oracle backtrace" do
let(:bt) do
['ORA-06512: at "STORE.LI_LICENSES_PACK", line 1945',
'ORA-06512: at "ACTIVATION.LI_ACT_LICENSES_PACK", line 101',
'ORA-06512: at line 2',
'from stmt.c:243:in oci8lib_220.bundle']
end
let(:ex) { OCIError.new.tap { |e| e.set_backtrace(bt) } }
let(:parsed_backtrace) do
[{ file: nil, line: 1945, function: 'STORE.LI_LICENSES_PACK' },
{ file: nil, line: 101, function: 'ACTIVATION.LI_ACT_LICENSES_PACK' },
{ file: nil, line: 2, function: nil },
{ file: 'stmt.c', line: 243, function: 'oci8lib_220.bundle' }]
end
it "returns a properly formatted array of hashes" do
stub_const('OCIError', AirbrakeTestError)
expect(described_class.parse(ex)).to eq(parsed_backtrace)
end
end
context "given an ExecJS exception" do
let(:bt) do
['compile ((execjs):6692:19)',
'eval (<anonymous>:1:10)',
'(execjs):6703:8',
'require../helpers.exports ((execjs):1:102)',
'Object.<anonymous> ((execjs):1:120)',
'Object.Module._extensions..js (module.js:550:10)',
'bootstrap_node.js:467:3',
"/opt/rubies/ruby-2.3.1/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'"]
end
let(:ex) { ExecJS::RuntimeError.new.tap { |e| e.set_backtrace(bt) } }
let(:parsed_backtrace) do
[{ file: '(execjs)', line: 6692, function: 'compile' },
{ file: '<anonymous>', line: 1, function: 'eval' },
{ file: '(execjs)', line: 6703, function: '' },
{ file: '(execjs)', line: 1, function: 'require../helpers.exports' },
{ file: '(execjs)', line: 1, function: 'Object.<anonymous>' },
{ file: 'module.js', line: 550, function: 'Object.Module._extensions..js' },
{ file: 'bootstrap_node.js', line: 467, function: '' },
{ file: '/opt/rubies/ruby-2.3.1/lib/ruby/2.3.0/benchmark.rb',
line: 308,
function: 'realtime' }]
end
it "returns a properly formatted array of hashes" do
stub_const('ExecJS::RuntimeError', AirbrakeTestError)
expect(described_class.parse(ex)).to eq(parsed_backtrace)
end
end
context "when code hunks are enabled" do
before { Airbrake::Config.instance.merge(code_hunks: true) }
context "and when root_directory is configured" do
before do
Airbrake::Config.instance.merge(root_directory: project_root_path(''))
end
let(:parsed_backtrace) do
[
{
file: project_root_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 Layout/LineLength,Lint/InterpolationCheck
96 => ' @config.logger.debug("#{LOG_LABEL} `notice.to_json` failed: #{ex.class}: #{ex}")',
# rubocop:enable Layout/LineLength,Lint/InterpolationCheck
},
},
{
file: fixture_path('notroot.txt'),
line: 3,
function: 'pineapple',
},
{
file: project_root_path('vendor/bundle/ignored_file.rb'),
line: 2,
function: 'ignore_me',
},
]
end
it "attaches code to those frames files of which match root_directory" do
ex = RuntimeError.new
backtrace = [
"#{project_root_path('code.rb')}:94:in `to_json'",
fixture_path("notroot.txt:3:in `pineapple'"),
"#{project_root_path('vendor/bundle/ignored_file.rb')}:2:in `ignore_me'",
]
ex.set_backtrace(backtrace)
expect(described_class.parse(ex)).to eq(parsed_backtrace)
end
end
context "and when root_directory is a Pathname" do
before do
Airbrake::Config.instance.merge(
root_directory: Pathname.new(project_root_path('')),
)
end
let(:parsed_backtrace) do
[
{
file: project_root_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 Layout/LineLength,Lint/InterpolationCheck
96 => ' @config.logger.debug("#{LOG_LABEL} `notice.to_json` failed: #{ex.class}: #{ex}")',
# rubocop:enable Layout/LineLength,Lint/InterpolationCheck
},
},
]
end
it "attaches code to those frames files of which match root_directory" do
ex = RuntimeError.new
ex.set_backtrace(["#{project_root_path('code.rb')}:94:in `to_json'"])
expect(described_class.parse(ex)).to eq(parsed_backtrace)
end
end
context "and when root_directory isn't configured" do
before do
Airbrake::Config.instance.merge(root_directory: nil)
stub_const('Airbrake::Backtrace::CODE_FRAME_LIMIT', 2)
end
let(:parsed_backtrace) do
[
{
file: project_root_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 Layout/LineLength,Lint/InterpolationCheck
96 => ' @config.logger.debug("#{LOG_LABEL} `notice.to_json` failed: #{ex.class}: #{ex}")',
# rubocop:enable Layout/LineLength,Lint/InterpolationCheck
},
},
{
file: project_root_path('code.rb'),
line: 95,
function: 'to_json',
code: {
93 => ' begin',
94 => ' json = @payload.to_json',
95 => ' rescue *JSON_EXCEPTIONS => ex',
# rubocop:disable Layout/LineLength,Lint/InterpolationCheck
96 => ' @config.logger.debug("#{LOG_LABEL} `notice.to_json` failed: #{ex.class}: #{ex}")',
# rubocop:enable Layout/LineLength,Lint/InterpolationCheck
97 => ' else',
},
},
{
file: project_root_path('code.rb'),
line: 96,
function: 'to_json',
},
]
end
it "attaches code to first N frames" do
ex = RuntimeError.new
backtrace = [
"#{project_root_path('code.rb')}:94:in `to_json'",
"#{project_root_path('code.rb')}:95:in `to_json'",
"#{project_root_path('code.rb')}:96:in `to_json'",
]
ex.set_backtrace(backtrace)
expect(described_class.parse(ex)).to eq(parsed_backtrace)
end
end
end
context "when code hunks are disabled" do
before { Airbrake::Config.instance.merge(code_hunks: false) }
context "and when root_directory is configured" do
before do
Airbrake::Config.instance.merge(root_directory: project_root_path(''))
end
let(:parsed_backtrace) do
[
{
file: project_root_path('code.rb'),
line: 94,
function: 'to_json',
},
]
end
it "doesn't attach code to frames" do
ex = RuntimeError.new
backtrace = ["#{project_root_path('code.rb')}:94:in `to_json'"]
ex.set_backtrace(backtrace)
expect(described_class.parse(ex)).to eq(parsed_backtrace)
end
end
end
end
end