mirror of
https://github.com/outline/outline.git
synced 2026-11-21 21:44:30 +03:00
20 lines
786 B
TypeScript
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);
|
|
}
|