* refactor(server): add getErrorMessage helper and fix error-related type-aware lint errors in session.ts * fix build: correct parseClientCapabilities type handling
28 lines
712 B
TypeScript
28 lines
712 B
TypeScript
/**
|
|
* Extracts a human-readable error message from an unknown error value.
|
|
* Handles Error instances, string errors, and other types safely.
|
|
*/
|
|
export function getErrorMessage(error: unknown): string {
|
|
if (error instanceof Error) {
|
|
return error.message;
|
|
}
|
|
if (typeof error === "string") {
|
|
return error;
|
|
}
|
|
return String(error);
|
|
}
|
|
|
|
/**
|
|
* Extracts an error message from an unknown error value, with a fallback
|
|
* for when no message can be extracted.
|
|
*/
|
|
export function getErrorMessageOr(error: unknown, fallback: string): string {
|
|
if (error instanceof Error) {
|
|
return error.message;
|
|
}
|
|
if (typeof error === "string" && error.length > 0) {
|
|
return error;
|
|
}
|
|
return fallback;
|
|
}
|