Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: reduce duplication in the error-collector #2323

Merged
merged 8 commits into from
Jul 2, 2024
91 changes: 36 additions & 55 deletions lib/errors/error-collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,69 +89,47 @@ class ErrorCollector {
}

/**
* Helper method for processing errors that are created with .noticeError()
* Gets the iterable property from the transaction based on the error type
* @param {Transaction} transaction the collected exception's transaction
* @param {string} errorType the type of error to be processed: "user", "transactionException", "transaction"
* @returns
amychisholm03 marked this conversation as resolved.
Show resolved Hide resolved
*/
amychisholm03 marked this conversation as resolved.
Show resolved Hide resolved
_getIterableProperty(transaction, errorType) {
let iterableProperty = null
if (errorType === 'user' && transaction.userErrors.length) {
iterableProperty = transaction.userErrors
}
if (errorType === 'transactionException') {
iterableProperty = transaction.exceptions
}
return iterableProperty
}

/**
* Helper method for processing errors that are created with .noticeError(), exceptions
* on the transaction (transaction.exceptions array), and inferred errors based on Transaction metadata.
*
* @param {Transaction} transaction the collected exception's transaction
* @param {number} collectedErrors the number of errors we've successfully .collect()-ed
* @param {number} expectedErrors the number of errors marked as expected in noticeError
* @returns {Array.<number>} the updated [collectedErrors, expectedErrors] numbers post-processing
* @param {string} errorType the type of error to be processed; "user", "transactionException", "transaction"
amychisholm03 marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Array.<number>} the updated [collectedErrors, expectedErrors] numbers post processing
*/
_processUserErrors(transaction, collectedErrors, expectedErrors) {
if (transaction.userErrors.length) {
for (let i = 0; i < transaction.userErrors.length; i++) {
const exception = transaction.userErrors[i]
_processErrors(transaction, collectedErrors, expectedErrors, errorType) {
amychisholm03 marked this conversation as resolved.
Show resolved Hide resolved
const iterableProperty = this._getIterableProperty(transaction, errorType)
if (iterableProperty) {
iterableProperty.forEach((exception) => {
amychisholm03 marked this conversation as resolved.
Show resolved Hide resolved
if (this.collect(transaction, exception)) {
++collectedErrors
// if we could collect it, then check if expected
if (
urltils.isExpectedError(this.config, transaction.statusCode) ||
errorHelper.isExpectedException(transaction, exception, this.config, urltils)
) {
++expectedErrors
}
}
}
}

return [collectedErrors, expectedErrors]
}
amychisholm03 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Helper method for processing exceptions on the transaction (transaction.exceptions array)
*
* @param {Transaction} transaction the transaction being processed
* @param {number} collectedErrors the number of errors successfully .collect()-ed
* @param {number} expectedErrors the number of collected errors that were expected
* @returns {Array.<number>} the updated [collectedErrors, expectedErrors] numbers post-processing
*/
_processTransactionExceptions(transaction, collectedErrors, expectedErrors) {
for (let i = 0; i < transaction.exceptions.length; i++) {
const exception = transaction.exceptions[i]
if (this.collect(transaction, exception)) {
++collectedErrors
// if we could collect it, then check if expected
if (
urltils.isExpectedError(this.config, transaction.statusCode) ||
errorHelper.isExpectedException(transaction, exception, this.config, urltils)
) {
++expectedErrors
}
}
}

return [collectedErrors, expectedErrors]
}

/**
* Helper method for processing an inferred error based on Transaction metadata
*
* @param {Transaction} transaction the transaction being processed
* @param {number} collectedErrors the number of errors successfully .collect()-ed
* @param {number} expectedErrors the number of collected errors that were expected
* @returns {Array.<number>} the updated [collectedErrors, expectedErrors] numbers post-processing
*/
_processTransactionErrors(transaction, collectedErrors, expectedErrors) {
if (this.collect(transaction)) {
})
} else if (errorType === 'transaction' && this.collect(transaction)) {
++collectedErrors
if (urltils.isExpectedError(this.config, transaction.statusCode)) {
++expectedErrors
Expand Down Expand Up @@ -183,27 +161,30 @@ class ErrorCollector {

// errors from noticeError are currently exempt from
// ignore and exclude rules
;[collectedErrors, expectedErrors] = this._processUserErrors(
;[collectedErrors, expectedErrors] = this._processErrors(
transaction,
collectedErrors,
expectedErrors
expectedErrors,
'user'
)

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

// collect other exceptions only if status code is not ignored
if (transaction.exceptions.length && !isIgnoredErrorStatusCode) {
;[collectedErrors, expectedErrors] = this._processTransactionExceptions(
;[collectedErrors, expectedErrors] = this._processErrors(
transaction,
collectedErrors,
expectedErrors
expectedErrors,
'transactionException'
)
} else if (isErroredTransaction) {
;[collectedErrors, expectedErrors] = this._processTransactionErrors(
;[collectedErrors, expectedErrors] = this._processErrors(
transaction,
collectedErrors,
expectedErrors
expectedErrors,
'transaction'
)
}

Expand Down
Loading