-
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
13
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.
+301
−75
Open
Changes from all commits
Commits
Show all changes
13 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 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,116 @@ | ||
| import type { HawkJavaScriptEvent } from '@/types'; | ||
| import { Sanitizer } from '@hawk.so/core'; | ||
|
|
||
| /** | ||
| * Represents a captured error in a normalized form. | ||
| * | ||
| * Motivation: | ||
| * - `Error | string` is unclear and hard to work with. | ||
| * - Fields can be filled from an event or from the error itself. | ||
| */ | ||
| export type CapturedError = { | ||
| /** Human-readable non-empty error message used as a title in the dashboard */ | ||
| title: string; | ||
| /** Error type (e.g. 'TypeError', 'NetworkError'), or undefined if unknown */ | ||
| type: HawkJavaScriptEvent['type']; | ||
| /** The original (unsanitized) value — use for instanceof checks and backtrace parsing only */ | ||
| rawError: unknown; | ||
| }; | ||
|
|
||
| /** | ||
| * Extracts a human-readable title from an unknown sanitized error. | ||
| * Prefers `.message` on objects, falls back to the value itself for strings, | ||
| * and serializes everything else. | ||
| * | ||
| * @param sanitizedError - Value returned by `Sanitizer.sanitize(error)` | ||
| * @returns The error title string, or undefined if absent or empty | ||
| */ | ||
| function getTitleFromError(sanitizedError: unknown): string | undefined { | ||
| if (sanitizedError == null) { | ||
| return undefined; | ||
| } | ||
|
|
||
| let message: unknown = sanitizedError; | ||
| if (typeof sanitizedError === 'object' && 'message' in sanitizedError) { | ||
| message = (sanitizedError 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 sanitized error. | ||
| * | ||
| * @param sanitizedError - Value returned by `Sanitizer.sanitize(error)` | ||
| * @returns The error name string, or undefined if absent or empty | ||
| */ | ||
| function getTypeFromError(sanitizedError: unknown): string | undefined { | ||
| return (sanitizedError as {name: string})?.name || undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a CapturedError from an unknown error value and optional fallbacks. | ||
| * The thrown value is first passed through `Sanitizer.sanitize(error)`. | ||
| * | ||
| * @param error - Any value thrown or rejected | ||
| * @param fallbackValues - Fallback values from event if they can't be extracted from the error | ||
| * @returns A normalized `CapturedError` object | ||
| */ | ||
| export function composeCapturedError( | ||
| error: unknown, | ||
| fallbackValues: { title?: string; type?: string } = {} | ||
| ): CapturedError { | ||
| /** | ||
| * @todo we should consider moving Sanitizer to utils | ||
| */ | ||
| const sanitizedError = Sanitizer.sanitize(error); | ||
|
|
||
| return { | ||
| title: getTitleFromError(sanitizedError) || fallbackValues.title || '<unknown error>', | ||
| type: getTypeFromError(sanitizedError) || fallbackValues.type, | ||
| rawError: error, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts and normalizes error 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 A normalized CapturedError object | ||
| */ | ||
| export function getErrorFromErrorEvent(event: ErrorEvent | PromiseRejectionEvent): CapturedError { | ||
| if (event.type === 'error') { | ||
| event = event as ErrorEvent; | ||
|
|
||
| return composeCapturedError(event.error, { | ||
| title: event.message && (event.filename ? `'${event.message}' at ${event.filename}:${event.lineno}:${event.colno}` : event.message), | ||
| }); | ||
| } | ||
|
|
||
| if (event.type === 'unhandledrejection') { | ||
FeironoX5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| event = event as PromiseRejectionEvent; | ||
|
|
||
| return composeCapturedError(event.reason, { | ||
| type: 'UnhandledRejection', | ||
| }); | ||
| } | ||
|
|
||
| /* | ||
| Fallback case: ensures function always returns CapturedError. | ||
| composeCapturedError(undefined) yields object with undefined fields. | ||
| */ | ||
| return composeCapturedError(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.
Uh oh!
There was an error while loading. Please reload this page.