Skip to content

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

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

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

Comment thread packages/javascript/src/utils/error.ts Outdated
Comment thread packages/javascript/src/utils/error.ts Outdated
Comment thread packages/javascript/src/utils/error.ts Outdated
Comment thread packages/javascript/src/utils/error.ts
Comment thread packages/javascript/src/catcher.ts Outdated
Comment thread packages/javascript/src/utils/error.ts Outdated
@FeironoX5 FeironoX5 force-pushed the fix/plain-object-rejection-new branch from f3c071b to f450310 Compare April 11, 2026 08:15
* @param value - Any already-safe value prepared by the caller
* @returns The error name string, or undefined if absent or empty
*/
export function getTypeFromError(value: unknown): HawkJavaScriptEvent['type'] | undefined {
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 will be simpler

* @param context - any additional data passed by user
*/
private async prepareErrorFormatted(error: Error | string, context?: EventContext): Promise<CatcherMessage<typeof Catcher.type>> {
private async prepareErrorFormatted(error: ErrorSource, context?: EventContext): Promise<CatcherMessage<typeof Catcher.type>> {
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
private async prepareErrorFormatted(error: ErrorSource, context?: EventContext): Promise<CatcherMessage<typeof Catcher.type>> {
private async prepareErrorFormatted(errorOrRejectionReasion: ErrorSource, context?: EventContext): Promise<CatcherMessage<typeof Catcher.type>> {

Comment on lines +78 to +89
fallbackTitle: event.message
? (event.filename ? `'${event.message}' at ${event.filename}:${event.lineno}:${event.colno}` : event.message)
: undefined,
};
}

if (event.type === 'unhandledrejection') {
event = event as PromiseRejectionEvent;

return {
rawError: event.reason,
fallbackType: '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.

specifying of fallback title and type is a part of internal business logic of getErrorFromErrorEvent. It should not be exported

event = event as PromiseRejectionEvent;

return {
rawError: event.reason,
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.

reason is not an error

if (event instanceof ErrorEvent && error === undefined) {
error = (event as ErrorEvent).message;
}
const error = getErrorFromErrorEvent(event);
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.

maybe it should return only type, title, backtrace?

const { type, title, backtrace } = getErrorDetailsFromEvent(event);

stack: event.error?.stack || '',
type,
message,
stack: (errorSource.rawError as Error)?.stack || '',
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.

rawError is typed as unknown, so casting to Error before accessing .stack is unsafe — the cast bypasses the type system without any runtime guarantee. The optional chain prevents a throw but silently produces undefined for non-Error values.

Prefer an explicit guard (same fix applies on line 222):

stack: errorSource.rawError instanceof Error ? errorSource.rawError.stack ?? '' : '',

@@ -212,8 +218,8 @@ export class ConsoleCatcher {
method: 'error',
timestamp: new Date(),
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.

Shouldn't we use here const type from 202 line?

*/
export function getErrorFromErrorEvent(event: ErrorEvent | PromiseRejectionEvent): ErrorSource {
if (event.type === 'error') {
event = event as ErrorEvent;
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.

I know that it's no-op, but event = event as ErrorEvent reads ike mutation. Perhaps it'll be better to bind a local const:

const errorEvent = event as ErrorEvent;
return {
  rawError: errorEvent.error,
  fallbackTitle: ...,
};

Same for unhandledrejection branch.

const sanitizedError = Sanitizer.sanitize(rawError);
const throwableError = rawError instanceof Error ? rawError : undefined;
const title = getTitleFromError(sanitizedError) ?? fallbackTitle ?? '<unknown error>';
const type = getTypeFromError(sanitizedError) ?? fallbackType;
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.

When rawError is an Error (e.g. Promise.reject(new TypeError('...'))), getTypeFromError returns 'TypeError' and fallbackType: 'UnhandledRejection' is silently discarded.

Previously, type was always the error's own name for Error objects – so this is not a regression – but it's a non-obvious, especially since the console catcher diverges here (see consoleCatcher.ts:220).

Perhaps we should leave comment explaining current behavior.


it('should return undefined when name is absent', () => {
expect(getTypeFromError(Sanitizer.sanitize({ code: 'ERR_001' }))).toBeUndefined();
});
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.

Two cases worth adding to this describe block:

  1. Sanitized DOMExceptionSanitizer.sanitize converts it to a string placeholder ('<instance of DOMException>'), so getTypeFromError returns undefined. The getTitleFromError suite already covers this input, but getTypeFromError doesn't:
it('should return undefined for sanitized DOMException', () => {
  expect(getTypeFromError(Sanitizer.sanitize(new DOMException('Network error', 'NetworkError')))).toBeUndefined();
});
  1. Plain object with a name field — some libraries reject with { name, message } shapes; this is the path that returns a non-undefined type from a plain object:
it('should return name from plain object with name field', () => {
  expect(getTypeFromError(Sanitizer.sanitize({ name: 'CustomError', code: 42 }))).toBe('CustomError');
});

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

3 participants