-
Notifications
You must be signed in to change notification settings - Fork 3.4k
chore: update errors to be stringified when object #32663
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
Merged
+0
−0
Merged
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8264a2e
chore: update errors to be stringified when object
jennifer-shehane 3038d10
handle circular dependencies
jennifer-shehane cce7a78
add serialize-error npm package
jennifer-shehane af72248
Merge branch 'develop' into stringify-studio-errors
jennifer-shehane dd86c86
downgrade serialize-error to cjs compatible version
jennifer-shehane 2d35253
move to prod dep
jennifer-shehane c9c54f7
move safeErrorSerialize further out into Cloud dir
jennifer-shehane fc7fafc
remove redundancies in studio tests
jennifer-shehane d03ee89
Merge branch 'develop' into stringify-studio-errors
jennifer-shehane d7b1911
handle undefined
jennifer-shehane 7a6d920
Merge branch 'stringify-studio-errors' of https://github.com/cypress-…
jennifer-shehane 0be6bef
fix undefined
jennifer-shehane 3e5f0ce
Merge branch 'develop' into stringify-studio-errors
jennifer-shehane 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,41 @@ | ||
/** | ||
* Safely serializes an error object to a string, handling circular references | ||
* and other non-serializable values that would cause JSON.stringify to throw. | ||
*/ | ||
export function safeErrorSerialize (error: unknown): string { | ||
mschile marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (typeof error === 'string') { | ||
return error | ||
} | ||
|
||
if (typeof error === 'object' && error !== null) { | ||
try { | ||
// Try JSON.stringify first, but catch any errors | ||
return JSON.stringify(error) | ||
} catch (e) { | ||
// If JSON.stringify fails (e.g., circular reference), fall back to a safer approach | ||
try { | ||
// Try to extract meaningful properties | ||
const errorObj = error as Record<string, unknown> | ||
const safeObj: Record<string, unknown> = {} | ||
|
||
// Common error properties | ||
const commonProps = ['name', 'message', 'code', 'errno', 'stack'] | ||
|
||
for (const prop of commonProps) { | ||
if (prop in errorObj && typeof errorObj[prop] === 'string') { | ||
safeObj[prop] = errorObj[prop] | ||
} | ||
} | ||
|
||
// Try to stringify the safe object | ||
return JSON.stringify(safeObj) | ||
} catch (e2) { | ||
// If even that fails, use a generic fallback | ||
return `[Non-serializable object: ${error.constructor?.name || 'Object'}]` | ||
cursor[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
// For primitives and other types, use String() as fallback | ||
return String(error) | ||
} | ||
cursor[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
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
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.