Files
outline/shared/utils/error.ts
T
Tom Moor 66eee9007f chore: Upgrade to Typescript 6 (#12737)
* chore: Upgrade TypeScript to 6.0
2026-06-18 17:34:05 -04:00

20 lines
786 B
TypeScript

/**
* Coerce an unknown value, such as a value caught in a try/catch, to an Error.
*
* @param value the value to coerce, typically a caught error.
* @returns the value itself when it is already an Error, otherwise a new Error wrapping its string representation.
*/
export function toError(value: unknown): Error {
return value instanceof Error ? value : new Error(String(value));
}
/**
* Extract a human-readable message from an unknown value, such as a value caught in a try/catch.
*
* @param value the value to read a message from, typically a caught error.
* @returns the Error's message when it is an Error, otherwise its string representation.
*/
export function errToString(value: unknown): string {
return value instanceof Error ? value.message : String(value);
}