-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Click to Pay - Adding Visa timeout logging (#2797)
* reworked timeout. added visa logging * cleanup * passin srciDpaId to buildClientProfile * changeset * adding tests * fix lint issue * ignoring coverage for test files * fixing coverage
- Loading branch information
1 parent
13d3cba
commit c265abc
Showing
9 changed files
with
286 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@adyen/adyen-web": patch | ||
--- | ||
|
||
Reporting custom Click to Pay Visa timeouts to Visa SDK |
22 changes: 22 additions & 0 deletions
22
packages/lib/src/components/internal/ClickToPay/errors/TimeoutError.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import TimeoutError from './TimeoutError'; | ||
|
||
describe('Click to Pay: TimeoutError', () => { | ||
test('should return proper error message', () => { | ||
const error = new TimeoutError({ source: 'identityLookup', scheme: 'visa', isTimeoutTriggeredBySchemeSdk: true }); | ||
|
||
expect(error.message).toBe("ClickToPayService - Timeout during identityLookup() of the scheme 'visa'"); | ||
expect(error.isTimeoutTriggeredBySchemeSdk).toBeTruthy(); | ||
expect(error.correlationId).toBeUndefined(); | ||
expect(error.source).toBe('identityLookup'); | ||
}); | ||
|
||
test('should be able to set the correlationId as part of the error', () => { | ||
const error = new TimeoutError({ source: 'init', scheme: 'mc', isTimeoutTriggeredBySchemeSdk: false }); | ||
error.setCorrelationId('xxx-yyy'); | ||
|
||
expect(error.message).toBe("ClickToPayService - Timeout during init() of the scheme 'mc'"); | ||
expect(error.isTimeoutTriggeredBySchemeSdk).toBeFalsy(); | ||
expect(error.source).toBe('init'); | ||
expect(error.correlationId).toBe('xxx-yyy'); | ||
}); | ||
}); |
25 changes: 23 additions & 2 deletions
25
packages/lib/src/components/internal/ClickToPay/errors/TimeoutError.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/lib/src/components/internal/ClickToPay/services/execute-with-timeout.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { executeWithTimeout } from './execute-with-timeout'; | ||
import TimeoutError from '../errors/TimeoutError'; | ||
|
||
const error = new TimeoutError({ | ||
source: 'init', | ||
isTimeoutTriggeredBySchemeSdk: true, | ||
scheme: 'mc' | ||
}); | ||
|
||
describe('executeWithTimeout', () => { | ||
it('should return the result of asyncFn if it resolves before timeout', async () => { | ||
const asyncFn = jest.fn().mockResolvedValue('success'); | ||
const timer = 1000; // 1 second timeout | ||
|
||
const result = await executeWithTimeout(asyncFn, timer, error); | ||
|
||
expect(result).toBe('success'); | ||
expect(asyncFn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should throw TimeoutError if asyncFn does not resolve before timeout', async () => { | ||
const asyncFn = jest.fn(() => new Promise(resolve => setTimeout(resolve, 2000))); // Resolves in 2 seconds | ||
const timer = 1000; // 1 second timeout | ||
|
||
await expect(executeWithTimeout(asyncFn, timer, error)).rejects.toThrow(TimeoutError); | ||
expect(asyncFn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should throw the original error if asyncFn rejects', async () => { | ||
const asyncFn = jest.fn(() => Promise.reject(new Error('async error'))); | ||
const timer = 1000; // 1 second timeout | ||
|
||
await expect(executeWithTimeout(asyncFn, timer, error)).rejects.toThrow('async error'); | ||
expect(asyncFn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should clear the timeout if asyncFn resolves before timeout', async () => { | ||
jest.useFakeTimers(); | ||
const asyncFn = jest.fn().mockResolvedValue('success'); | ||
const timer = 1000; // 1 second timeout | ||
|
||
const promise = executeWithTimeout(asyncFn, timer, error); | ||
|
||
jest.runAllTimers(); // Fast-forward all timers | ||
|
||
const result = await promise; | ||
expect(result).toBe('success'); | ||
expect(asyncFn).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
packages/lib/src/components/internal/ClickToPay/services/execute-with-timeout.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import TimeoutError from '../errors/TimeoutError'; | ||
|
||
function executeWithTimeout<T>(asyncFn: () => Promise<T>, timer: number, error: TimeoutError): Promise<T> { | ||
let timeout = null; | ||
|
||
const wait = (seconds: number) => | ||
new Promise<T>((_, reject) => { | ||
timeout = setTimeout(() => reject(error), seconds); | ||
}); | ||
|
||
return Promise.race<T>([asyncFn(), wait(timer)]) | ||
.then(value => { | ||
clearTimeout(timeout); | ||
return value; | ||
}) | ||
.catch(error => { | ||
clearTimeout(timeout); | ||
throw error; | ||
}); | ||
} | ||
|
||
export { executeWithTimeout }; |
Oops, something went wrong.