-
Notifications
You must be signed in to change notification settings - Fork 169
feat: populate rate limited properties on RatelimitedLinearError #694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: populate rate limited properties on RatelimitedLinearError #694
Conversation
🦋 Changeset detectedLatest commit: f59c38d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation using Number(... ) ?? undefined
will yield 0
or NaN
for missing or invalid headers rather than undefined
. It’s recommended to explicitly check the header’s presence and guard against NaN
. Consider extracting a helper for header parsing to reduce repetition.
Summary of changes
Added Rate Limit Properties to RatelimitedLinearError
-
Introduced seven new properties on
RatelimitedLinearError
:retryAfter
requestsLimit
requestsRemaining
requestsResetAt
complexityLimit
complexityRemaining
complexityResetAt
-
Each property is populated from response headers using
Number(error?.response?.headers?.get(...)) ?? undefined
.
Changelog
- Created a new changeset for version bump:
.changeset/fuzzy-mangos-yawn.md
packages/sdk/src/error.ts
Outdated
this.retryAfter = Number(error?.response?.headers?.get("retry-after")) ?? undefined; | ||
this.requestsLimit = Number(error?.response?.headers?.get("x-ratelimit-requests-limit")) ?? undefined; | ||
this.requestsRemaining = Number(error?.response?.headers?.get("x-ratelimit-requests-remaining")) ?? undefined; | ||
this.requestsResetAt = Number(error?.response?.headers?.get("x-ratelimit-requests-reset")) ?? undefined; | ||
this.complexityLimit = Number(error?.response?.headers?.get("x-ratelimit-complexity-limit") 8000 ) ?? undefined; | ||
this.complexityRemaining = Number(error?.response?.headers?.get("x-ratelimit-complexity-remaining")) ?? undefined; | ||
this.complexityResetAt = Number(error?.response?.headers?.get("x-ratelimit-complexity-reset")) ?? undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using Number(... ) ?? undefined
does not guard against missing or invalid headers correctly:
- If
headers.get(...)
returnsnull
,Number(null)
yields0
, so you’ll get0
instead ofundefined
when the header is absent. - If it returns
undefined
,Number(undefined)
yieldsNaN
, which is not replaced byundefined
due toNaN ?? undefined
evaluating toNaN
.
You should explicitly check whether the header value is null
or undefined
before converting it, and also guard against NaN
results.
Suggestion
Refactor header parsing to handle missing headers and invalid numbers correctly. For example:
const headers = error?.response?.headers;
const parseHeader = (key: string): number | undefined => {
const value = headers?.get(key);
if (value == null) return undefined;
const num = Number(value);
return Number.isNaN(num) ? undefined : num;
};
this.retryAfter = parseHeader('retry-after');
this.requestsLimit = parseHeader('x-ratelimit-requests-limit');
this.requestsRemaining = parseHeader('x-ratelimit-requests-remaining');
this.requestsResetAt = parseHeader('x-ratelimit-requests-reset');
this.complexityLimit = parseHeader('x-ratelimit-complexity-limit');
this.complexityRemaining = parseHeader('x-ratelimit-complexity-remaining');
this.complexityResetAt = parseHeader('x-ratelimit-complexity-reset');
Reply with "@CharlieHelps yes please" if you'd like me to add a commit with this suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks like a good suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@guillaumelachaud fixed
8e945b5
to
0b3c924
Compare
0b3c924
to
f59c38d
Compare
No description provided.