Skip to content

Instantly share code, notes, and snippets.

@jkohlin
Last active October 31, 2023 15:48
Show Gist options
  • Save jkohlin/ab36837c6261b1e8ab3f9f0bce8c7178 to your computer and use it in GitHub Desktop.
Save jkohlin/ab36837c6261b1e8ab3f9f0bce8c7178 to your computer and use it in GitHub Desktop.
stringifyUnknown
// Useful when passing an error from try/catch to a logger
export function stringifyUnknown(obj: unknown): string {
const defaultErrorString = 'unknown error'
const notNullish = obj ?? defaultErrorString
if (typeof notNullish === 'string') {
return notNullish
}
if (typeof notNullish === 'object') {
return JSON.stringify(notNullish, ['message', 'arguments', 'type', 'name', 'stack'], 2)
}
return String(notNullish) || defaultErrorString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment