Skip to content
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

fix: log full error object if default toString is not enough #814

Closed
wants to merge 1 commit into from

Conversation

shadowusr
Copy link
Member

What's done?

On rare occasions, error may be some exotic object which can't be properly stringified and is displayed as [object Object]. This PR changes this behaviour so that if default toString is not informative, JSON.stringify() version is displayed.

Comment on lines +12 to +18
const stringifyError = error => {
try {
return error.toString() === "[object Object]" ? JSON.stringify(error) : error;
} catch {
return error;
}
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Предлагаю подстраховаться и сделать так:

return !error || typeof error === "object"
    ? JSON.stringify(error)
    : error;

typeof дает object как раз только на всякие null и объекты, а такая логика с toString не будет работать в редких кейсах, когда Symbol.toStringTag переопределен, или еще что-то странное прокинут в ошибку. Например, массив/дату/промис/...

@shadowusr shadowusr closed this Jan 31, 2024
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.

2 participants