Skip to content

Preserve plain-object rejection reasons in global error handlers#181

Open
FeironoX5 wants to merge 10 commits intomasterfrom
fix/plain-object-rejection-new
Open

Preserve plain-object rejection reasons in global error handlers#181
FeironoX5 wants to merge 10 commits intomasterfrom
fix/plain-object-rejection-new

Conversation

@FeironoX5
Copy link
Copy Markdown
Contributor

Closes #171

Fix global error normalization so plain-object promise rejection reasons are captured as readable payloads instead of "[object Object]".

PR Description

Fix error normalization for global error handlers so rejected plain objects are preserved instead of being collapsed into "[object Object]".

This PR introduces a normalized CapturedError shape and uses it across the main catcher and console catcher. For unhandledrejection, plain-object reasons are now serialized into a readable title instead of going through String(obj). For browser ErrorEvents, the catcher now also builds a more useful fallback message when the original error object is unavailable (for example, cross-origin script errors).

Changes

  • Added fillCapturedError and getErrorFromErrorEvent helpers in src/utils/error.ts
  • Switched Catcher internals from Error | string to normalized CapturedError
  • Reused normalized error extraction in consoleCatcher
  • Preserved original rawError reference for deduplication/backtrace logic
  • Added tests for:
    • Error
    • DOMException
    • plain-object promise rejection reasons
    • string / null / undefined rejection reasons
    • circular objects
    • cross-origin-style ErrorEvent fallback
изображение

});
}

if (event.type === 'unhandledrejection') {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

to we need to lowercase event.type here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

no need, we're only subscribed to unhandledrejection events, so we can gurantee unhandledrejection type:

window.addEventListener('unhandledrejection', (event: PromiseRejectionEvent) => this.handleEvent(event));

* @param error - event from which to get backtrace
*/
private async getBacktrace(error: Error | string): Promise<HawkJavaScriptEvent['backtrace']> {
private async getBacktrace(error: unknown): Promise<HawkJavaScriptEvent['backtrace']> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why unknown is better than exact union type?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it's more honest in sense that error can have any type

});
}

return fillCapturedError(undefined);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

explain this case in comment and write a test for it

Copy link
Copy Markdown
Contributor Author

@FeironoX5 FeironoX5 Apr 11, 2026

Choose a reason for hiding this comment

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

No need in test case as it shouldn't be a possible scenario, comment addded

@FeironoX5 FeironoX5 force-pushed the fix/plain-object-rejection-new branch from f3c071b to f450310 Compare April 11, 2026 08:15
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.

Bad plain object handling as PromiseRejectionEvent reason

2 participants