Files
donutbrowser/src/lib/error-utils.ts
T
2025-08-05 06:57:18 +04:00

32 lines
1.0 KiB
TypeScript

/**
* Extracts the root error message from nested error strings
* Removes redundant "Failed to..." prefixes to show only the most specific error
*/
export function extractRootError(error: unknown): string {
if (!error) return "Unknown error";
const errorStr = error instanceof Error ? error.message : String(error);
// Split by common error prefixes and take the last meaningful part
const errorParts = errorStr.split(/Failed to [^:]+: /);
const rootError = errorParts[errorParts.length - 1];
// Clean up any remaining nested structure
const cleanError = rootError.replace(/^"([^"]+)"$/, "$1");
return cleanError || errorStr;
}
/**
* Shows error toast with cleaned error message
*/
export function showCleanErrorToast(error: unknown, prefix?: string) {
const rootError = extractRootError(error);
const message = prefix ? `${prefix}: ${rootError}` : rootError;
// Import dynamically to avoid circular dependencies
import("./toast-utils").then(({ showErrorToast }) => {
showErrorToast(message);
});
}