Skip to content

Commit d8aab1c

Browse files
authored
Merge pull request bugsnag#779 from bugsnag/v7-notify-depth-facade
fix: Ensure depth is only incremented when calling via facade
2 parents 8c1acb3 + 4ea3b74 commit d8aab1c

File tree

8 files changed

+118
-6
lines changed

8 files changed

+118
-6
lines changed

packages/browser/src/notifier.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ const Bugsnag = {
7878
return Bugsnag._client
7979
}
8080
Bugsnag._client = Bugsnag.createClient(opts)
81-
Bugsnag._client._depth += 1
8281
return Bugsnag._client
8382
}
8483
}
@@ -87,7 +86,10 @@ map(['resetEventCount'].concat(keys(Client.prototype)), (m) => {
8786
if (/^_/.test(m)) return
8887
Bugsnag[m] = function () {
8988
if (!Bugsnag._client) return console.log(`Bugsnag.${m}() was called before Bugsnag.start()`)
90-
return Bugsnag._client[m].apply(Bugsnag._client, arguments)
89+
Bugsnag._client._depth += 1
90+
const ret = Bugsnag._client[m].apply(Bugsnag._client, arguments)
91+
Bugsnag._client._depth -= 1
92+
return ret
9193
}
9294
})
9395

packages/expo/src/notifier.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ const Bugsnag = {
7070
return Bugsnag._client
7171
}
7272
Bugsnag._client = Bugsnag.createClient(opts)
73-
Bugsnag._client._depth += 1
7473
return Bugsnag._client
7574
}
7675
}
@@ -81,7 +80,10 @@ Object.getOwnPropertyNames(Client.prototype).map((m) => {
8180
if (/^_/.test(m)) return
8281
Bugsnag[m] = function () {
8382
if (!Bugsnag._client) return console.warn(`Bugsnag.${m}() was called before Bugsnag.start()`)
84-
return Bugsnag._client[m].apply(Bugsnag._client, arguments)
83+
Bugsnag._client._depth += 1
84+
const ret = Bugsnag._client[m].apply(Bugsnag._client, arguments)
85+
Bugsnag._client._depth -= 1
86+
return ret
8587
}
8688
})
8789

packages/node/src/notifier.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ const Bugsnag = {
6262
return Bugsnag._client
6363
}
6464
Bugsnag._client = Bugsnag.createClient(opts)
65-
Bugsnag._client._depth += 1
6665
return Bugsnag._client
6766
}
6867
}
@@ -71,7 +70,10 @@ Object.keys(Client.prototype).forEach((m) => {
7170
if (/^_/.test(m)) return
7271
Bugsnag[m] = function () {
7372
if (!Bugsnag._client) return console.error(`Bugsnag.${m}() was called before Bugsnag.start()`)
74-
return Bugsnag._client[m].apply(Bugsnag._client, arguments)
73+
Bugsnag._client._depth += 1
74+
const ret = Bugsnag._client[m].apply(Bugsnag._client, arguments)
75+
Bugsnag._client._depth -= 1
76+
return ret
7577
}
7678
})
7779

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<script src="/node_modules/@bugsnag/browser/dist/bugsnag.min.js"></script>
6+
<script type="text/javascript">
7+
var ENDPOINT = decodeURIComponent(window.location.search.match(/ENDPOINT=([^&]+)/)[1])
8+
var API_KEY = decodeURIComponent(window.location.search.match(/API_KEY=([^&]+)/)[1])
9+
window.client = Bugsnag.start({
10+
apiKey: API_KEY,
11+
endpoints: { notify: ENDPOINT, sessions: '/noop' }
12+
})
13+
</script>
14+
</head>
15+
<body>
16+
<script>
17+
function a () {
18+
window.client.notify({ name: 'Error', message: 'make a stacktrace for me' })
19+
}
20+
function b () { a() }
21+
function c () { b() }
22+
c()
23+
</script>
24+
</body>
25+
</html>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<script src="/node_modules/@bugsnag/browser/dist/bugsnag.min.js"></script>
6+
<script type="text/javascript">
7+
var ENDPOINT = decodeURIComponent(window.location.search.match(/ENDPOINT=([^&]+)/)[1])
8+
var API_KEY = decodeURIComponent(window.location.search.match(/API_KEY=([^&]+)/)[1])
9+
window.client = Bugsnag.start({
10+
apiKey: API_KEY,
11+
endpoints: { notify: ENDPOINT, sessions: '/noop' }
12+
})
13+
</script>
14+
</head>
15+
<body>
16+
<script>
17+
function a () {
18+
window.client.notify('make a stacktrace for me')
19+
}
20+
function b () { a() }
21+
function c () { b() }
22+
c()
23+
</script>
24+
</body>
25+
</html>

test/browser/features/handled_errors.feature

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,29 @@ Scenario: calling notify() with a string, getting a generated stacktrace
7373

7474
# this ensures the first generated stackframe doesn't come from bugsnag's source
7575
And the payload field "events.0.exceptions.0.stacktrace.0.method" equals "a"
76+
77+
Scenario: calling window.client.notify() with an object, getting a generated stacktrace
78+
When I navigate to the URL "/handled/script/f.html"
79+
Then I wait to receive a request
80+
And the request is a valid browser payload for the error reporting API
81+
And the exception "errorClass" equals "Error"
82+
And the exception "message" equals "make a stacktrace for me"
83+
And the exception "type" equals "browserjs"
84+
85+
# this ensure the stacktrace features all of the nested stackframes
86+
And the payload field "events.0.exceptions.0.stacktrace.0.method" equals "a"
87+
And the payload field "events.0.exceptions.0.stacktrace.1.method" equals "b"
88+
And the payload field "events.0.exceptions.0.stacktrace.2.method" equals "c"
89+
90+
Scenario: calling window.client.notify() with a string, getting a generated stacktrace
91+
When I navigate to the URL "/handled/script/g.html"
92+
Then I wait to receive a request
93+
And the request is a valid browser payload for the error reporting API
94+
And the exception "errorClass" equals "Error"
95+
And the exception "message" equals "make a stacktrace for me"
96+
And the exception "type" equals "browserjs"
97+
98+
# this ensure the stacktrace features all of the nested stackframes
99+
And the payload field "events.0.exceptions.0.stacktrace.0.method" equals "a"
100+
And the payload field "events.0.exceptions.0.stacktrace.1.method" equals "b"
101+
And the payload field "events.0.exceptions.0.stacktrace.2.method" equals "c"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var Bugsnag = require('@bugsnag/node')
2+
var client = Bugsnag.start({
3+
apiKey: process.env.BUGSNAG_API_KEY,
4+
endpoints: {
5+
notify: process.env.BUGSNAG_NOTIFY_ENDPOINT,
6+
sessions: process.env.BUGSNAG_SESSIONS_ENDPOINT
7+
}
8+
})
9+
10+
function a () {
11+
client.notify({ name: 'Error', message: 'make a stacktrace for me' })
12+
}
13+
function b () { a() }
14+
function c () { b() }
15+
c()

test/node/features/handled_errors.feature

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,18 @@ Scenario: calling notify with a string
8080
And the exception "type" equals "nodejs"
8181
And the "file" of stack frame 0 equals "scenarios/notify-string.js"
8282
And the "lineNumber" of stack frame 0 equals 10
83+
84+
Scenario: calling an assigned client.notify with an object
85+
And I run the service "handled" with the command "node scenarios/global-notify-string"
86+
And I wait to receive a request
87+
Then the request is valid for the error reporting API version "4" for the "Bugsnag Node" notifier
88+
And the event "unhandled" is false
89+
And the event "severity" equals "warning"
90+
And the event "severityReason.type" equals "handledException"
91+
And the exception "errorClass" equals "Error"
92+
And the exception "message" equals "make a stacktrace for me"
93+
And the exception "type" equals "nodejs"
94+
And the "file" of stack frame 0 equals "scenarios/global-notify-string.js"
95+
And the "method" of stack frame 0 equals "a"
96+
And the "method" of stack frame 1 equals "b"
97+
And the "method" of stack frame 2 equals "c"

0 commit comments

Comments
 (0)