* A CodeceptJS utility function to retry a step or callback multiple times with a specified polling interval. * * @async * @function retryTo * @param {Function} callback - The function to execute, which will be retried upon failure. * Receives the current retry cou
(callback, maxTries, pollInterval = 200)
| 202 | * @throws Will reject with the last error encountered if the maximum retries are exceeded. |
| 203 | */ |
| 204 | async function retryTo(callback, maxTries, pollInterval = 200) { |
| 205 | const sessionName = 'retryTo' |
| 206 | |
| 207 | return new Promise((done, reject) => { |
| 208 | let tries = 0 |
| 209 | |
| 210 | function handleRetryException(err) { |
| 211 | recorder.throw(err) |
| 212 | reject(err) |
| 213 | } |
| 214 | |
| 215 | const tryBlock = async () => { |
| 216 | tries++ |
| 217 | recorder.session.start(`${sessionName} ${tries}`) |
| 218 | try { |
| 219 | await callback(tries) |
| 220 | } catch (err) { |
| 221 | recorder.throw(err) |
| 222 | } |
| 223 | |
| 224 | // Call done if no errors |
| 225 | recorder.add(() => { |
| 226 | recorder.session.restore(`${sessionName} ${tries}`) |
| 227 | done(null) |
| 228 | }) |
| 229 | |
| 230 | // Catch errors and retry |
| 231 | recorder.session.catch(err => { |
| 232 | recorder.session.restore(`${sessionName} ${tries}`) |
| 233 | if (tries < maxTries) { |
| 234 | output.debug(`Error ${err}... Retrying`) |
| 235 | recorder.add(`${sessionName} ${tries}`, () => setTimeout(tryBlock, pollInterval)) |
| 236 | } else { |
| 237 | // if maxTries reached |
| 238 | handleRetryException(err) |
| 239 | } |
| 240 | }) |
| 241 | } |
| 242 | |
| 243 | recorder.add(sessionName, tryBlock).catch(err => { |
| 244 | console.error('An error occurred:', err) |
| 245 | done(null) |
| 246 | }) |
| 247 | }) |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * A CodeceptJS utility function to attempt a step or callback without failing the test. |
no test coverage detected