refactor(server): add getErrorMessage helper and fix error-related type-aware lint errors (#704)
* refactor(server): add getErrorMessage helper and fix error-related type-aware lint errors in session.ts * fix build: correct parseClientCapabilities type handling
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
Reference in New Issue
Block a user