-
Notifications
You must be signed in to change notification settings - Fork 4
Preserve plain-object rejection reasons in global error handlers #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FeironoX5
wants to merge
14
commits into
master
Choose a base branch
from
fix/plain-object-rejection-new
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+334
−85
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
28eb7b3
fix: preserve content when promise is rejected with plain object
FeironoX5 033bdfb
fix: move stringify reason method to utils
FeironoX5 e2f8a61
fix: fixed import
FeironoX5 47c6967
test: tests added for getErrorFromEvent
FeironoX5 ad0313a
test: remove sanitizer mocking
FeironoX5 c335586
fix: extract error utilities
FeironoX5 b2c8981
refactor(javascript): normalize browser error event extraction
FeironoX5 f450310
Merge branch 'master' into fix/plain-object-rejection-new
FeironoX5 93c21d3
fix: tests and lint fixed
FeironoX5 46b2fd1
fix(javascript): simplify global error titles and rename error composer
FeironoX5 8850693
test(error): cover unsupported event fallback in error utils
FeironoX5 1aadaf4
fix: lint fixed
FeironoX5 ae85a13
fix: small improvements
FeironoX5 759e649
refactor: move error sanitization out of error utils
FeironoX5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import type { HawkJavaScriptEvent } from '@/types'; | ||
|
|
||
| /** | ||
| * Represents a raw error source before title/type normalization. | ||
| * Fallback values are provided by the event itself when raw error data is missing. | ||
| */ | ||
| export type ErrorSource = { | ||
| /** The original unsanitized value — use for instanceof checks and backtrace parsing only */ | ||
| rawError: unknown; | ||
| /** Fallback human-readable title used when rawError does not provide one */ | ||
| fallbackTitle?: string; | ||
| /** Fallback error type provided by the caller */ | ||
| fallbackType?: HawkJavaScriptEvent['type']; | ||
| }; | ||
|
|
||
| /** | ||
| * Extracts a human-readable title from an unknown value. | ||
| * Prefers `.message` on objects, falls back to the value itself for strings, | ||
| * and serializes everything else. | ||
| * | ||
| * @param value - Any already-safe value prepared by the caller | ||
| * @returns The error title string, or undefined if absent or empty | ||
| */ | ||
| export function getTitleFromError(value: unknown): string | undefined { | ||
| if (value == null) { | ||
| return undefined; | ||
| } | ||
|
|
||
| let message: unknown = value; | ||
| if (typeof value === 'object' && 'message' in value) { | ||
| message = (value as {message?: unknown}).message; | ||
| } | ||
|
|
||
| if (typeof message === 'string') { | ||
| return message || undefined; | ||
| } | ||
|
|
||
| try { | ||
| return JSON.stringify(message); | ||
| } catch { | ||
| /** | ||
| * If no JSON global is available or serialization fails, | ||
| * fall back to string conversion | ||
| */ | ||
| return String(message); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extracts an error type name from an unknown value. | ||
| * | ||
| * @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 { | ||
| if (typeof value !== 'object' || value === null || !('name' in value)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const name = (value as {name?: unknown}).name; | ||
|
|
||
| return typeof name === 'string' && name ? name : undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts raw error data and event-level fallbacks from ErrorEvent or PromiseRejectionEvent. | ||
| * Handles CORS-restricted errors (where event.error is undefined) by falling back to event.message. | ||
| * | ||
| * @param event - The error or promise rejection event | ||
| * @returns Raw error source with optional event-level fallback values | ||
| */ | ||
| export function getErrorFromErrorEvent(event: ErrorEvent | PromiseRejectionEvent): ErrorSource { | ||
| if (event.type === 'error') { | ||
| event = event as ErrorEvent; | ||
|
|
||
| return { | ||
| rawError: event.error, | ||
| fallbackTitle: event.message | ||
| ? (event.filename ? `'${event.message}' at ${event.filename}:${event.lineno}:${event.colno}` : event.message) | ||
| : undefined, | ||
| }; | ||
| } | ||
|
|
||
| if (event.type === 'unhandledrejection') { | ||
|
FeironoX5 marked this conversation as resolved.
|
||
| event = event as PromiseRejectionEvent; | ||
|
|
||
| return { | ||
| rawError: event.reason, | ||
| fallbackType: 'UnhandledRejection', | ||
| }; | ||
| } | ||
|
|
||
| return { rawError: undefined }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it will be simpler