Skip to content

Commit 5175335

Browse files
authored
Release v6.27.0
2 parents 522d1b9 + 39401d3 commit 5175335

File tree

8 files changed

+159
-12
lines changed

8 files changed

+159
-12
lines changed

.github/workflows/maze-runner.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ jobs:
127127
strategy:
128128
fail-fast: false
129129
matrix:
130-
ruby-version: ['2.7', '3.3']
130+
ruby-version: ['2.7', '3.3.0'] # TODO: change back to '3.3' after https://github.com/ruby/ruby/pull/10619 is released
131131
rails-version: ['6', '7', '_integrations']
132132
include:
133133
- ruby-version: '2.5'
134134
rails-version: '6'
135135
exclude:
136136
- ruby-version: '2.7'
137137
rails-version: '6'
138-
- ruby-version: '3.3'
138+
- ruby-version: '3.3.0' # TODO: change back to '3.3' after https://github.com/ruby/ruby/pull/10619 is released
139139
rails-version: '_integrations'
140140

141141
uses: ./.github/workflows/run-maze-runner.yml

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
Changelog
22
=========
33

4+
## v6.27.0 (23 May 2024)
5+
6+
### Enhancements
7+
8+
* Include the Warden scope in user metadata
9+
| [#821](https://github.com/bugsnag/bugsnag-ruby/pull/821)
10+
| [javierjulio](https://github.com/javierjulio)
11+
* Add a block variant of `add_on_error`
12+
| [#824](https://github.com/bugsnag/bugsnag-ruby/pull/824)
13+
414
## v6.26.4 (25 March 2024)
515

616
### Fixes

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.26.4
1+
6.27.0

lib/bugsnag.rb

+14
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,20 @@ def leave_breadcrumb(name, meta_data={}, type=Bugsnag::Breadcrumbs::MANUAL_BREAD
309309
configuration.breadcrumbs << breadcrumb unless breadcrumb.ignore?
310310
end
311311

312+
##
313+
# Add the given block to the list of on_error callbacks
314+
#
315+
# The on_error callbacks will be called when an error is captured or reported
316+
# and are passed a {Bugsnag::Report} object
317+
#
318+
# Returning false from an on_error callback will cause the error to be ignored
319+
# and will prevent any remaining callbacks from being called
320+
#
321+
# @return [void]
322+
def on_error(&block)
323+
configuration.on_error(&block)
324+
end
325+
312326
##
313327
# Add the given callback to the list of on_error callbacks
314328
#

lib/bugsnag/configuration.rb

+14
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,20 @@ def disable_sessions
569569
@enable_sessions = false
570570
end
571571

572+
##
573+
# Add the given block to the list of on_error callbacks
574+
#
575+
# The on_error callbacks will be called when an error is captured or reported
576+
# and are passed a {Bugsnag::Report} object
577+
#
578+
# Returning false from an on_error callback will cause the error to be ignored
579+
# and will prevent any remaining callbacks from being called
580+
#
581+
# @return [void]
582+
def on_error(&block)
583+
middleware.use(block)
584+
end
585+
572586
##
573587
# Add the given callback to the list of on_error callbacks
574588
#

lib/bugsnag/middleware/warden_user.rb

+3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ def call(report)
2323
# Extract useful user information
2424
user = {}
2525
user_object = env["warden"].user({:scope => best_scope, :run_callbacks => false}) rescue nil
26+
2627
if user_object
28+
user[:warden_scope] = best_scope
29+
2730
# Build the user info for this scope
2831
COMMON_USER_FIELDS.each do |field|
2932
user[field] = user_object.send(field) if user_object.respond_to?(field)

spec/integrations/warden_user_spec.rb

+76-9
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,100 @@
66

77
user = double
88
allow(user).to receive_messages(
9-
:email => "TEST_EMAIL",
10-
:name => "TEST_NAME",
11-
:created_at => "TEST_NOW"
9+
email: "TEST_EMAIL",
10+
name: "TEST_NAME",
11+
created_at: "TEST_NOW",
1212
)
1313

1414
warden = double
1515
allow(warden).to receive(:user).with({
16-
:scope => "user",
17-
:run_callbacks => false
16+
scope: "user",
17+
run_callbacks: false,
1818
}).and_return(user)
1919

2020
report = double("Bugsnag::Report")
21+
expect(report).to receive(:request_data).exactly(3).times.and_return({
22+
rack_env: {
23+
"warden" => warden,
24+
"rack.session" => {
25+
"warden.user.user.key" => "TEST_USER",
26+
}
27+
}
28+
})
29+
30+
expect(report).to receive(:user=).with({
31+
email: "TEST_EMAIL",
32+
name: "TEST_NAME",
33+
created_at: "TEST_NOW",
34+
warden_scope: "user",
35+
})
36+
37+
expect(callback).to receive(:call).with(report)
38+
39+
middleware = Bugsnag::Middleware::WardenUser.new(callback)
40+
middleware.call(report)
41+
end
42+
43+
it "sets the scope to the 'best scope'" do
44+
callback = double
45+
46+
user = double
47+
allow(user).to receive_messages(
48+
email: "TEST_EMAIL",
49+
name: "TEST_NAME",
50+
created_at: "TEST_NOW",
51+
)
52+
53+
warden = double
54+
allow(warden).to receive(:user).with({
55+
scope: "admin",
56+
run_callbacks: false,
57+
}).and_return(user)
58+
59+
report = double("Bugsnag::Report")
2160
expect(report).to receive(:request_data).exactly(3).times.and_return({
2261
:rack_env => {
2362
"warden" => warden,
2463
"rack.session" => {
25-
"warden.user.user.key" => "TEST_USER"
64+
"warden.user.admin.key" => "TEST_USER"
2665
}
2766
}
2867
})
2968

3069
expect(report).to receive(:user=).with({
31-
:email => "TEST_EMAIL",
32-
:name => "TEST_NAME",
33-
:created_at => "TEST_NOW"
70+
email: "TEST_EMAIL",
71+
name: "TEST_NAME",
72+
created_at: "TEST_NOW",
73+
warden_scope: "admin",
74+
})
75+
76+
expect(callback).to receive(:call).with(report)
77+
78+
middleware = Bugsnag::Middleware::WardenUser.new(callback)
79+
middleware.call(report)
80+
end
81+
82+
it "doesn't set the user if the user object is empty" do
83+
callback = double
84+
85+
warden = double
86+
allow(warden).to receive(:user).with({
87+
scope: "user",
88+
run_callbacks: false,
89+
}).and_return(nil)
90+
91+
report = double("Bugsnag::Report")
92+
expect(report).to receive(:request_data).exactly(3).times.and_return({
93+
:rack_env => {
94+
"warden" => warden,
95+
"rack.session" => {
96+
"warden.user.user.key" => "TEST_USER"
97+
}
98+
}
3499
})
35100

101+
expect(report).not_to receive(:user=)
102+
36103
expect(callback).to receive(:call).with(report)
37104

38105
middleware = Bugsnag::Middleware::WardenUser.new(callback)

spec/on_error_spec.rb

+39
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,30 @@
1818
end)
1919
end
2020

21+
it "accepts a block" do
22+
Bugsnag.on_error {|report| report.add_tab(:important, { hello: "world" }) }
23+
Bugsnag.on_error {|report| report.add_tab(:significant, { hey: "earth" }) }
24+
25+
Bugsnag.notify(RuntimeError.new("Oh no!"))
26+
27+
expect(Bugsnag).to(have_sent_notification do |payload, _headers|
28+
event = get_event_from_payload(payload)
29+
30+
expect(event["metaData"]["important"]).to eq({ "hello" => "world" })
31+
expect(event["metaData"]["significant"]).to eq({ "hey" => "earth" })
32+
end)
33+
end
34+
2135
it "can add callbacks in a configure block" do
2236
callback1 = proc {|report| report.add_tab(:important, { hello: "world" }) }
2337
callback2 = proc {|report| report.add_tab(:significant, { hey: "earth" }) }
2438

2539
Bugsnag.configure do |config|
2640
config.add_on_error(callback1)
2741
config.add_on_error(callback2)
42+
config.on_error do |report|
43+
report.add_tab(:critical, { hi: "planet" })
44+
end
2845
end
2946

3047
Bugsnag.notify(RuntimeError.new("Oh no!"))
@@ -34,6 +51,7 @@
3451

3552
expect(event["metaData"]["important"]).to eq({ "hello" => "world" })
3653
expect(event["metaData"]["significant"]).to eq({ "hey" => "earth" })
54+
expect(event["metaData"]["critical"]).to eq({ "hi" => "planet" })
3755
end)
3856
end
3957

@@ -56,6 +74,27 @@
5674
end)
5775
end
5876

77+
it "can remove an already registered block" do
78+
callback1 = proc {|report| report.add_tab(:important, { hello: "world" }) }
79+
callback2 = proc {|report| report.add_tab(:significant, { hey: "earth" }) }
80+
81+
Bugsnag.add_on_error(callback1)
82+
83+
# pass callback2 as a block so that it can be removed
84+
Bugsnag.on_error(&callback2)
85+
86+
Bugsnag.remove_on_error(callback2)
87+
88+
Bugsnag.notify(RuntimeError.new("Oh no!"))
89+
90+
expect(Bugsnag).to(have_sent_notification do |payload, _headers|
91+
event = get_event_from_payload(payload)
92+
93+
expect(event["metaData"]["important"]).to eq({ "hello" => "world" })
94+
expect(event["metaData"]["significant"]).to be_nil
95+
end)
96+
end
97+
5998
it "can remove all registered callbacks" do
6099
callback1 = proc {|report| report.add_tab(:important, { hello: "world" }) }
61100
callback2 = proc {|report| report.add_tab(:significant, { hey: "earth" }) }

0 commit comments

Comments
 (0)