Skip to content

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

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

Preserve plain-object rejection reasons in global error handlers#181
FeironoX5 wants to merge 8 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
изображение

* Prefers `.message` on objects, falls back to the value itself for strings,
* and serializes everything else.
*
* @param safeError - Sanitized error value (any shape)
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.

what does it meat "sanitized" in this context? Clarify it in the jsdoc please.

Also maybe sanitizedError?

* @param fallbackValues - Fallback values from event if they can't be extracted from the error
* @returns A normalized `CapturedError` object
*/
export function fillCapturedError(
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.

Suggested change
export function fillCapturedError(
export function composeCapturedError(

event = event as ErrorEvent;

return fillCapturedError(event.error, {
title: event.message && `'${event.message}' at ${event.filename || '<unknown file>'}:${event.lineno}:${event.colno}`,
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.

is there any case when filename is undefined but lineno and colno are not?

Maybe they also can be undefined?

});
}

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?

* @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?

});
}

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

@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