Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions io/notion/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* The shared pause in rateLimit.ts decides every wait after a Notion 429 or 529, so the SDK must
* hand the first one straight to withRetry rather than retrying on its own. retry: false turns off
* the SDK's retry for both statuses; this checks it with a 429.
*/

describe('notion client', () => {
afterEach(() => {
vi.unstubAllGlobals()
vi.resetModules()
})

it('rejects a rate-limited request after one attempt, leaving retries to withRetry', async () => {
const fetchStub = vi.fn(
async () =>
new Response(JSON.stringify({ object: 'error', status: 429, code: 'rate_limited', message: 'slow down' }), {
status: 429,
headers: { 'content-type': 'application/json', 'retry-after': '1' },
}),
)
// The SDK binds fetch when the client is created, so stub it before the module loads
vi.stubGlobal('fetch', fetchStub)
vi.resetModules()
const { default: notion } = await vi.importActual<typeof import('./client')>('./client')

await expect(notion.dataSources.query({ data_source_id: 'test-data-source' })).rejects.toMatchObject({
status: 429,
})
expect(fetchStub).toHaveBeenCalledTimes(1)
})
})
2 changes: 2 additions & 0 deletions io/notion/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { notionEnv } from '@/io/env/notion'
const notion = new Client({
auth: notionEnv.NOTION_ACCESS_TOKEN,
notionVersion: '2025-09-03',
// withRetry and the shared pause in rateLimit.ts own retries; the SDK's own would bypass the pause
retry: false,
})

export default notion
Expand Down
3 changes: 2 additions & 1 deletion io/notion/rateLimit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ function rateLimited(retryAfter: string | null, status = 429): Error {
})
}

// This SDK version has no error code for 529, so it surfaces as an unknown HTTP error
// A 529 whose body is not Notion's JSON error surfaces as an unknown HTTP error; one with that
// body is an APIResponseError (service_overload), the same shape as the 429 above
return new UnknownHTTPResponseError({ status, message: 'service_overload', headers, rawBodyText })
}

Expand Down
4 changes: 1 addition & 3 deletions io/utils/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
* What to do after a failed attempt: wait a time the server asked for, back off, or stop retrying.
*/
export type RetryDecision =
| { kind: 'wait'; delayMs: number }
| { kind: 'backoff'; delayMs: number }
| { kind: 'give-up'; error: Error }
{ kind: 'wait'; delayMs: number } | { kind: 'backoff'; delayMs: number } | { kind: 'give-up'; error: Error }

/**
* Lets one API decide how its failures are retried, and hold back new attempts while it is paused.
Expand Down
Loading
Loading