Skip to content
This repository was archived by the owner on Aug 6, 2024. It is now read-only.

Commit 64fa7c6

Browse files
committed
Added check to ensure that setErrorGroupCallback callback is sync
1 parent 42469d3 commit 64fa7c6

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

api.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1776,8 +1776,10 @@ API.prototype.setErrorGroupCallback = function setErrorGroupCallback(callback) {
17761776
)
17771777
metric.incrementCallCount()
17781778

1779-
if (!this.shim.isFunction(callback)) {
1780-
logger.warn('Error Group callback must be a function, Error Group attribute will not be added')
1779+
if (!this.shim.isFunction(callback) || this.shim.isPromise(callback)) {
1780+
logger.warn(
1781+
'Error Group callback must be a synchronous function, Error Group attribute will not be added'
1782+
)
17811783
return
17821784
}
17831785

test/unit/api/api-set-error-group-callback.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,28 @@ tap.test('Agent API = set Error Group callback', (t) => {
8282
)
8383
t.end()
8484
})
85+
86+
t.test('should not attach the callback when async function', (t) => {
87+
function callback() {
88+
return new Promise((resolve) => {
89+
setTimeout(() => {
90+
resolve()
91+
}, 200)
92+
}).then(() => 'error-group')
93+
}
94+
api.setErrorGroupCallback(callback())
95+
96+
t.equal(loggerMock.warn.callCount, 1, 'should log warning when failed')
97+
t.notOk(
98+
api.agent.errors.errorGroupCallback,
99+
'should not attach the callback on the error collector'
100+
)
101+
t.equal(
102+
api.agent.metrics.getOrCreateMetric(NAMES.SUPPORTABILITY.API + '/setErrorGroupCallback')
103+
.callCount,
104+
1,
105+
'should increment the API tracking metric'
106+
)
107+
t.end()
108+
})
85109
})

0 commit comments

Comments
 (0)