Skip to content

Commit 10581bf

Browse files
authoredJul 2, 2024··
refactor: reduce duplication in the error-collector (#2323)
* Refactored _processUserErrors, _processTransactionExceptions, and _processTransactionErrors into a single function: _processErrors * Created _processErrrors test
1 parent 3a910ef commit 10581bf

File tree

2 files changed

+125
-59
lines changed

2 files changed

+125
-59
lines changed
 

‎lib/errors/error-collector.js

+49-59
Original file line numberDiff line numberDiff line change
@@ -89,75 +89,62 @@ class ErrorCollector {
8989
}
9090

9191
/**
92-
* Helper method for processing errors that are created with .noticeError()
92+
* Gets the iterable property from the transaction based on the error type
9393
*
9494
* @param {Transaction} transaction the collected exception's transaction
95-
* @param {number} collectedErrors the number of errors we've successfully .collect()-ed
96-
* @param {number} expectedErrors the number of errors marked as expected in noticeError
97-
* @returns {Array.<number>} the updated [collectedErrors, expectedErrors] numbers post-processing
95+
* @param {string} errorType the type of error: "user", "transactionException", "transaction"
96+
* @returns {object[]} the iterable property from the transaction based on the error type
9897
*/
99-
_processUserErrors(transaction, collectedErrors, expectedErrors) {
100-
if (transaction.userErrors.length) {
101-
for (let i = 0; i < transaction.userErrors.length; i++) {
102-
const exception = transaction.userErrors[i]
103-
if (this.collect(transaction, exception)) {
104-
++collectedErrors
105-
// if we could collect it, then check if expected
106-
if (
107-
urltils.isExpectedError(this.config, transaction.statusCode) ||
108-
errorHelper.isExpectedException(transaction, exception, this.config, urltils)
109-
) {
110-
++expectedErrors
111-
}
112-
}
113-
}
98+
_getIterableProperty(transaction, errorType) {
99+
let iterableProperty = null
100+
if (errorType === 'user') {
101+
iterableProperty = transaction.userErrors
114102
}
115-
116-
return [collectedErrors, expectedErrors]
103+
if (errorType === 'transactionException') {
104+
iterableProperty = transaction.exceptions
105+
}
106+
return iterableProperty
117107
}
118108

119109
/**
120-
* Helper method for processing exceptions on the transaction (transaction.exceptions array)
110+
* Helper method for processing errors that are created with .noticeError(), exceptions
111+
* on the transaction (transaction.exceptions array), and inferred errors based on Transaction metadata.
121112
*
122-
* @param {Transaction} transaction the transaction being processed
123-
* @param {number} collectedErrors the number of errors successfully .collect()-ed
124-
* @param {number} expectedErrors the number of collected errors that were expected
125-
* @returns {Array.<number>} the updated [collectedErrors, expectedErrors] numbers post-processing
113+
* @param {Transaction} transaction the collected exception's transaction
114+
* @param {number} collectedErrors the number of errors we've successfully .collect()-ed
115+
* @param {number} expectedErrors the number of errors marked as expected in noticeError
116+
* @param {string} errorType the type of error to be processed; "user", "transactionException", "transaction"
117+
* @returns {Array.<number>} the updated [collectedErrors, expectedErrors] numbers post processing
126118
*/
127-
_processTransactionExceptions(transaction, collectedErrors, expectedErrors) {
128-
for (let i = 0; i < transaction.exceptions.length; i++) {
129-
const exception = transaction.exceptions[i]
130-
if (this.collect(transaction, exception)) {
131-
++collectedErrors
132-
// if we could collect it, then check if expected
133-
if (
134-
urltils.isExpectedError(this.config, transaction.statusCode) ||
135-
errorHelper.isExpectedException(transaction, exception, this.config, urltils)
136-
) {
137-
++expectedErrors
119+
_processErrors(transaction, collectedErrors, expectedErrors, errorType) {
120+
const iterableProperty = this._getIterableProperty(transaction, errorType)
121+
if (iterableProperty === null && errorType === 'transaction') {
122+
if (this.collect(transaction)) {
123+
collectedErrors++
124+
if (urltils.isExpectedError(this.config, transaction.statusCode)) {
125+
expectedErrors++
138126
}
139127
}
128+
return [collectedErrors, expectedErrors]
140129
}
141130

142-
return [collectedErrors, expectedErrors]
143-
}
131+
if (iterableProperty === null) {
132+
return [collectedErrors, expectedErrors]
133+
}
144134

145-
/**
146-
* Helper method for processing an inferred error based on Transaction metadata
147-
*
148-
* @param {Transaction} transaction the transaction being processed
149-
* @param {number} collectedErrors the number of errors successfully .collect()-ed
150-
* @param {number} expectedErrors the number of collected errors that were expected
151-
* @returns {Array.<number>} the updated [collectedErrors, expectedErrors] numbers post-processing
152-
*/
153-
_processTransactionErrors(transaction, collectedErrors, expectedErrors) {
154-
if (this.collect(transaction)) {
155-
++collectedErrors
156-
if (urltils.isExpectedError(this.config, transaction.statusCode)) {
157-
++expectedErrors
135+
for (let i = 0; i < iterableProperty.length; i++) {
136+
const exception = iterableProperty[i]
137+
if (!this.collect(transaction, exception)) {
138+
continue
139+
}
140+
collectedErrors++
141+
if (
142+
urltils.isExpectedError(this.config, transaction.statusCode) ||
143+
errorHelper.isExpectedException(transaction, exception, this.config, urltils)
144+
) {
145+
expectedErrors++
158146
}
159147
}
160-
161148
return [collectedErrors, expectedErrors]
162149
}
163150

@@ -183,27 +170,30 @@ class ErrorCollector {
183170

184171
// errors from noticeError are currently exempt from
185172
// ignore and exclude rules
186-
;[collectedErrors, expectedErrors] = this._processUserErrors(
173+
;[collectedErrors, expectedErrors] = this._processErrors(
187174
transaction,
188175
collectedErrors,
189-
expectedErrors
176+
expectedErrors,
177+
'user'
190178
)
191179

192180
const isErroredTransaction = urltils.isError(this.config, transaction.statusCode)
193181
const isIgnoredErrorStatusCode = urltils.isIgnoredError(this.config, transaction.statusCode)
194182

195183
// collect other exceptions only if status code is not ignored
196184
if (transaction.exceptions.length && !isIgnoredErrorStatusCode) {
197-
;[collectedErrors, expectedErrors] = this._processTransactionExceptions(
185+
;[collectedErrors, expectedErrors] = this._processErrors(
198186
transaction,
199187
collectedErrors,
200-
expectedErrors
188+
expectedErrors,
189+
'transactionException'
201190
)
202191
} else if (isErroredTransaction) {
203-
;[collectedErrors, expectedErrors] = this._processTransactionErrors(
192+
;[collectedErrors, expectedErrors] = this._processErrors(
204193
transaction,
205194
collectedErrors,
206-
expectedErrors
195+
expectedErrors,
196+
'transaction'
207197
)
208198
}
209199

‎test/unit/errors/error-collector.test.js

+76
Original file line numberDiff line numberDiff line change
@@ -2370,3 +2370,79 @@ test('When using the async listener', (t) => {
23702370
})
23712371
}
23722372
})
2373+
2374+
tap.test('_processErrors', (t) => {
2375+
t.beforeEach((t) => {
2376+
t.context.agent = helper.loadMockedAgent({
2377+
attributes: {
2378+
enabled: true
2379+
}
2380+
})
2381+
2382+
const transaction = new Transaction(t.context.agent)
2383+
transaction.url = '/'
2384+
t.context.transaction = transaction
2385+
t.context.errorCollector = t.context.agent.errors
2386+
})
2387+
2388+
t.afterEach((t) => {
2389+
helper.unloadAgent(t.context.agent)
2390+
})
2391+
2392+
t.test('invalid errorType should return no iterableProperty', (t) => {
2393+
const { errorCollector, transaction } = t.context
2394+
const errorType = 'invalid'
2395+
const result = errorCollector._getIterableProperty(transaction, errorType)
2396+
2397+
t.equal(result, null)
2398+
t.end()
2399+
})
2400+
2401+
t.test('if errorType is transaction, should return no iterableProperty', (t) => {
2402+
const { errorCollector, transaction } = t.context
2403+
const errorType = 'transaction'
2404+
const result = errorCollector._getIterableProperty(transaction, errorType)
2405+
2406+
t.equal(result, null)
2407+
t.end()
2408+
})
2409+
2410+
t.test('if type is user, return an array of objects', (t) => {
2411+
const { errorCollector, transaction } = t.context
2412+
const errorType = 'user'
2413+
const result = errorCollector._getIterableProperty(transaction, errorType)
2414+
2415+
t.same(result, [])
2416+
t.end()
2417+
})
2418+
2419+
t.test('if type is transactionException, return an array of objects', (t) => {
2420+
const { errorCollector, transaction } = t.context
2421+
const errorType = 'transactionException'
2422+
const result = errorCollector._getIterableProperty(transaction, errorType)
2423+
2424+
t.same(result, [])
2425+
t.end()
2426+
})
2427+
2428+
t.test(
2429+
'if iterableProperty is null and errorType is not transaction, do not modify collectedErrors or expectedErrors',
2430+
(t) => {
2431+
const { errorCollector, transaction } = t.context
2432+
const errorType = 'error'
2433+
const collectedErrors = 0
2434+
const expectedErrors = 0
2435+
const result = errorCollector._processErrors(
2436+
transaction,
2437+
collectedErrors,
2438+
expectedErrors,
2439+
errorType
2440+
)
2441+
2442+
t.same(result, [collectedErrors, expectedErrors])
2443+
t.end()
2444+
}
2445+
)
2446+
2447+
t.end()
2448+
})

0 commit comments

Comments
 (0)
Please sign in to comment.