-
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.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
8 changed files
with
269 additions
and
28 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,4 @@ | ||
--- | ||
"@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'); | ||
}); | ||
}); |
29 changes: 25 additions & 4 deletions
29
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
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
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); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
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,19 @@ | ||
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 }; |
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