Skip to content
Open
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
20 changes: 20 additions & 0 deletions src/core/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ export class APIError<
this.type = data?.['type'];
}

/**
* Returns a JSON-serializable representation of this error.
*
* The native `Headers` class does not serialize its entries with
* `JSON.stringify`, so this method converts them to a plain object so that
* rate-limit headers (e.g. `x-ratelimit-reset-requests`) and other response
* headers are visible when the error is logged or serialized.
*/
toJSON() {
return {
status: this.status,
headers: this.headers ? Object.fromEntries(this.headers.entries()) : undefined,
error: this.error,
code: this.code,
Comment on lines +48 to +52
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.

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.

};
}

private static makeMessage(status: number | undefined, error: any, message: string | undefined) {
const msg =
error?.message ?
Expand Down