Skip to content

fix(error): add toJSON to APIError so response headers serialize correctly#1848

Open
anishesg wants to merge 1 commit intoopenai:masterfrom
proudhare:fix/ph-issue-1477
Open

fix(error): add toJSON to APIError so response headers serialize correctly#1848
anishesg wants to merge 1 commit intoopenai:masterfrom
proudhare:fix/ph-issue-1477

Conversation

@anishesg
Copy link
Copy Markdown

  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

The native Headers class in Node.js does not enumerate its entries when passed to JSON.stringify, so calling JSON.stringify(error) on any APIError (including RateLimitError) produces "headers": {} — hiding every response header including x-ratelimit-reset-requests, x-ratelimit-remaining-tokens, and the other rate-limit headers users need for retry logic.

The fix adds a toJSON() method to APIError in src/core/error.ts that converts this.headers to a plain key/value object via Object.fromEntries(this.headers.entries()). this.headers itself still holds the native Headers instance so callers using .headers.get(...) are unaffected; the change only improves serialization.

Additional context & links

  • Node.js Headers does not implement toJSON, and JSON.stringify only walks own enumerable properties, so the internal slot holding headers entries is invisible to it.
  • Users who already call error.headers.get('x-ratelimit-reset-requests') directly will see no change in behavior.

Fixes #1477

…ectly

- [ ] I understand that this repository is auto-generated and my pull request may not be merged

Signed-off-by: anish k <ak8686@princeton.edu>
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9d003bf731

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/core/error.ts
Comment on lines +48 to +52
return {
status: this.status,
headers: this.headers ? Object.fromEntries(this.headers.entries()) : undefined,
error: this.error,
code: this.code,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve subclass fields in APIError JSON output

toJSON() now returns a fixed object literal, so JSON.stringify() no longer includes enumerable fields added by subclasses (for example OAuthError.error_code) or by runtime assignment (for example APIConnectionError.cause). Before this change those properties were serialized automatically, so this is a regression for log pipelines or cross-process error transport that depend on those fields.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good point, I'll switch it to use Object.assign to pull in the subclass properties instead of hardcoding the fields.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good point, I'll switch it to use Object.assign to pull in the subclass properties instead of hardcoding the fields.

Comment thread src/core/error.ts
code: this.code,
param: this.param,
type: this.type,
request_id: this.requestID,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep request ID key consistent with APIError property

The serializer emits request_id, but the public error property is requestID; prior to toJSON() the serialized key was requestID because it was an enumerable own property. This silently changes the JSON contract and can break consumers that parse serialized errors to read the request ID.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'll rename the property to request_id to match the serialized output.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'll rename the property to request_id to match the serialized output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to access rate limit headers due to exceptions

1 participant