|
8 | 8 | import * as raw from './raw'
|
9 | 9 | import { RestEndpoint } from '../types'
|
10 | 10 | import { GetAppActionCallDetailsParams, GetAppActionCallParams } from '../../../common-types'
|
| 11 | +import { isSuccessful, shouldRePoll, waitFor } from '../../../common-utils' |
11 | 12 |
|
12 | 13 | export const create: RestEndpoint<'AppActionCall', 'create'> = (
|
13 | 14 | http: AxiosInstance,
|
@@ -50,28 +51,46 @@ async function callAppActionResult(
|
50 | 51 | callId: string
|
51 | 52 | }
|
52 | 53 | ) {
|
53 |
| - const appActionResponse = await getCallDetails(http, { ...params, callId }) |
| 54 | + const result = await getCallDetails(http, { ...params, callId }) |
54 | 55 |
|
55 |
| - if (appActionResponse && appActionResponse.sys && appActionResponse.sys.id) { |
56 |
| - resolve(appActionResponse) |
| 56 | + // The lambda failed or returned a 404, so we shouldn't re-poll anymore |
| 57 | + if (result?.response?.statusCode && !isSuccessful(result?.response?.statusCode)) { |
| 58 | + const error = new Error(result.response.body) |
| 59 | + reject(error) |
| 60 | + } |
| 61 | + |
| 62 | + if (isSuccessful(result.statusCode)) { |
| 63 | + resolve(result) |
| 64 | + } |
| 65 | + |
| 66 | + // The logs are not ready yet. Continue waiting for them |
| 67 | + if (shouldRePoll(result.statusCode) && checkCount !== retries) { |
| 68 | + waitFor(retryInterval) |
| 69 | + |
| 70 | + await callAppActionResult(http, params, { |
| 71 | + resolve, |
| 72 | + reject, |
| 73 | + retryInterval, |
| 74 | + retries, |
| 75 | + checkCount, |
| 76 | + callId, |
| 77 | + }) |
57 | 78 | } else if (checkCount === retries) {
|
58 | 79 | const error = new Error()
|
59 | 80 | error.message = 'The app action response is taking longer than expected to process.'
|
60 | 81 | reject(error)
|
61 | 82 | } else {
|
62 | 83 | checkCount++
|
63 |
| - setTimeout( |
64 |
| - () => |
65 |
| - callAppActionResult(http, params, { |
66 |
| - resolve, |
67 |
| - reject, |
68 |
| - retryInterval, |
69 |
| - retries, |
70 |
| - checkCount, |
71 |
| - callId, |
72 |
| - }), |
73 |
| - retryInterval |
74 |
| - ) |
| 84 | + waitFor(retryInterval) |
| 85 | + |
| 86 | + await callAppActionResult(http, params, { |
| 87 | + resolve, |
| 88 | + reject, |
| 89 | + retryInterval, |
| 90 | + retries, |
| 91 | + checkCount, |
| 92 | + callId, |
| 93 | + }) |
75 | 94 | }
|
76 | 95 | }
|
77 | 96 |
|
|
0 commit comments