Skip to content

Commit 7610878

Browse files
committed
chore: add retry for 429
1 parent 913cd4a commit 7610878

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

packages/backend/src/apps/aisay/__tests__/common/interceptors.error-handlers.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,24 @@ describe('AISAY request error handlers', () => {
9797
vi.restoreAllMocks()
9898
})
9999

100+
it('logs an error and jams the entire queue on 429', async () => {
101+
mockAxiosAdapterToThrowOnce(429, { 'retry-after': 123 })
102+
await http
103+
.get('/test-url')
104+
.then(() => {
105+
expect.unreachable()
106+
})
107+
.catch((error): void => {
108+
expect(error).toBeInstanceOf(RetriableError)
109+
expect(error.delayType).toEqual('queue')
110+
expect(error.message).toEqual('Rate limited by AISAY.')
111+
})
112+
expect(mocks.logError).toHaveBeenCalledWith(
113+
expect.stringContaining('HTTP 429'),
114+
expect.objectContaining({ event: 'aisay-http-429' }),
115+
)
116+
})
117+
100118
it('logs a warning and throws a RetriableError with default step delay on 503', async () => {
101119
mockAxiosAdapterToThrowOnce(503)
102120
await http

packages/backend/src/apps/aisay/common/interceptors/request-error-handler.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,29 @@ type ThrowingHandler = (
88
...args: Parameters<IApp['requestErrorHandler']>
99
) => never
1010

11+
// handle rate limiting
12+
const handle429: ThrowingHandler = function ($, error) {
13+
const retryAfterMs =
14+
parseRetryAfterToMs(error.response?.headers?.['retry-after']) ?? 'default'
15+
16+
logger.error('Received HTTP 429 from AISAY', {
17+
event: 'aisay-http-429',
18+
clientId: $.auth.data.clientId,
19+
baseUrl: error.response.config.baseURL,
20+
url: error.response.config.url,
21+
flowId: $.flow?.id,
22+
stepId: $.step?.id,
23+
executionId: $.execution?.id,
24+
retryAfterMs: error.response?.headers?.['retry-after'],
25+
})
26+
27+
throw new RetriableError({
28+
error: 'Rate limited by AISAY.',
29+
delayInMs: retryAfterMs,
30+
delayType: 'queue',
31+
})
32+
}
33+
1134
// Retry failures
1235
const handle503: ThrowingHandler = function ($, error) {
1336
const status = error.response.status
@@ -34,6 +57,8 @@ const handle503: ThrowingHandler = function ($, error) {
3457

3558
const errorHandler: IApp['requestErrorHandler'] = async function ($, error) {
3659
switch (error.response.status) {
60+
case 429:
61+
return handle429($, error)
3762
case 503:
3863
return handle503($, error)
3964
}

0 commit comments

Comments
 (0)