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

fix: await transaction commit and rollback and avoid unhandledPromise error #354

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
"audit-ci": "^7.1.0",
"eslint": "8.16.0",
"eslint-config-standard": "17.1.0",
"eslint-plugin-jest": "28.8.0",
"eslint-plugin-jest": "28.8.1",
"jest": "29.7.0",
"jest-junit": "16.0.0",
"npm-check-updates": "17.1.0",
Expand Down
8 changes: 4 additions & 4 deletions src/model/fxQuotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class FxQuotesModel {
histTimer({ success: false, queryName: 'handleFxQuoteRequest' })
this.log.error('error in handleFxQuoteRequest', err)
if (txn) {
txn.rollback(err)
await txn.rollback().catch(() => {})
}
await this.handleException(fspiopSource, fxQuoteRequest.conversionRequestId, err, headers, childSpan)
} finally {
Expand Down Expand Up @@ -380,7 +380,7 @@ class FxQuotesModel {
this.log.error('error in handleFxQuoteUpdate', err)
const fspiopSource = headers[ENUM.Http.Headers.FSPIOP.SOURCE]
if (txn) {
txn.rollback(err)
await txn.rollback().catch(() => {})
}
await this.handleException(fspiopSource, conversionRequestId, err, headers, childSpan)
} finally {
Expand Down Expand Up @@ -534,7 +534,7 @@ class FxQuotesModel {
})

// commit the txn to the db
txn.commit()
await txn.commit()
}

await childSpan.audit({ headers, params: { conversionRequestId } }, EventSdk.AuditEventAction.start)
Expand All @@ -545,7 +545,7 @@ class FxQuotesModel {
histTimer({ success: false, queryName: 'handleFxQuoteError' })
this.log.error('error in handleFxQuoteError', err)
if (txn) {
txn.rollback(err)
await txn.rollback().catch(() => {})
}
await this.handleException(fspiopSource, conversionRequestId, err, headers, childSpan)
} finally {
Expand Down
11 changes: 6 additions & 5 deletions src/model/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ class QuotesModel {
} catch (err) {
log.error('error in handleQuoteRequest:', err)
if (txn) {
txn.rollback(err)
await txn.rollback().catch(() => {})
}

const fspiopError = ErrorHandler.ReformatFSPIOPError(err)
Expand Down Expand Up @@ -711,7 +711,7 @@ class QuotesModel {
// internal-error
this.writeLog(`Error in handleQuoteUpdate: ${getStackOrInspect(err)}`)
if (txn) {
txn.rollback(err)
await txn.rollback().catch(() => {})
}
const fspiopError = ErrorHandler.ReformatFSPIOPError(err)
const state = new EventSdk.EventStateMetadata(EventSdk.EventStatusType.failed, fspiopError.apiErrorCode.code, fspiopError.apiErrorCode.message)
Expand Down Expand Up @@ -857,7 +857,7 @@ class QuotesModel {
})

// commit the txn to the db
txn.commit()
await txn.commit()
}
// create a new object to represent the error
const fspiopError = ErrorHandler.CreateFSPIOPErrorFromErrorInformation(error)
Expand All @@ -871,7 +871,7 @@ class QuotesModel {
// internal-error
this.writeLog(`Error in handleQuoteError: ${getStackOrInspect(err)}`)
if (txn) {
txn.rollback(err)
await txn.rollback().catch(() => {})
}
const fspiopError = ErrorHandler.ReformatFSPIOPError(err)
const state = new EventSdk.EventStateMetadata(EventSdk.EventStatusType.failed, fspiopError.apiErrorCode.code, fspiopError.apiErrorCode.message)
Expand Down Expand Up @@ -998,9 +998,10 @@ class QuotesModel {
const childSpan = span.getChild('qs_quote_sendErrorCallback')
try {
await childSpan.audit({ headers, params: { quoteId } }, EventSdk.AuditEventAction.start)
await this.sendErrorCallback(fspiopSource, fspiopError, quoteId, headers, childSpan, true)
const result = await this.sendErrorCallback(fspiopSource, fspiopError, quoteId, headers, childSpan, true)
histTimer({ success: true, queryName: 'quote_handleException' })
log.info('handleException is done')
return result
} catch (err) {
// any-error
// not much we can do other than log the error
Expand Down
2 changes: 1 addition & 1 deletion test/unit/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const fxQuoteMocks = {
}),
db: ({
commit = jest.fn().mockResolvedValue({}),
rollback = jest.fn(),
rollback = jest.fn(() => Promise.reject(new Error('DB error'))),
getParticipant = jest.fn().mockResolvedValue({}),
getParticipantEndpoint = jest.fn().mockResolvedValue(undefined),
createFxQuoteResponse = jest.fn().mockResolvedValue({}),
Expand Down
2 changes: 1 addition & 1 deletion test/unit/model/quotes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('QuotesModel', () => {
})
mockTransaction = {
commit: jest.fn(),
rollback: jest.fn()
rollback: jest.fn(() => Promise.reject(new Error('DB error')))
}
mockChildSpan = {
injectContextToHttpRequest: jest.fn(opts => opts),
Expand Down