memos/web/src/lib/error.ts
Johnny 85f4fc7a75 refactor: remove MemoContentContext and integrate MemoViewContext
- Deleted MemoContentContext and its associated types.
- Updated Tag and TaskListItem components to use MemoViewContext instead.
- Refactored MemoContent component to eliminate context provider and directly use derived values.
- Simplified MemoViewContext to only include essential data.
- Enhanced error handling in various components by introducing a centralized error handling utility.
- Improved type safety across components and hooks by refining TypeScript definitions.
- Updated remark plugins to enhance tag parsing and preserve node types.
2025-12-28 12:46:12 +08:00

38 lines
961 B
TypeScript

export function getErrorMessage(error: unknown, fallback = "Unknown error"): string {
if (error instanceof Error) {
return error.message;
}
if (typeof error === "string") {
return error;
}
if (error && typeof error === "object" && "message" in error) {
return String(error.message);
}
return fallback;
}
export function handleError(
error: unknown,
toast: (message: string) => void,
options?: {
context?: string;
fallbackMessage?: string;
onError?: (error: unknown) => void;
},
): void {
const contextPrefix = options?.context ? `${options.context}: ` : "";
const fallback = options?.fallbackMessage;
const errorMessage = options?.context ? `${contextPrefix}${getErrorMessage(error, fallback)}` : getErrorMessage(error, fallback);
console.error(error);
toast(errorMessage);
options?.onError?.(error);
}
export function isError(value: unknown): value is Error {
return value instanceof Error;
}