Files
trezor-suite/packages/utils/src/serializeError.ts
2025-01-29 12:28:33 +01:00

20 lines
844 B
TypeScript

/**
* Serialize an error of unknown type to a string.
*/
export const serializeError = (error: unknown): string => {
// Error instances are objects, but have no JSON printable properties.
// Instead, .toString() is their standard string representation. Though stack trace must be included separately
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/toString
if (error instanceof Error) {
return JSON.stringify({ message: error.toString(), stackTrace: error.stack });
}
// plain javascript object is not a conventional error type, but we have to count with it
if (typeof error === 'object') {
return JSON.stringify(error);
}
// assumed to be a primitive type; exotic types such as function will also be simply stringified
return `${error}`;
};