import type { WebErrorCode, WebResult } from "../shared/web"; const messages: Record = { invalid_selector: "Use a valid CSS selector, or omit the selector to read the loaded page.", invalid_url: "Use an absolute HTTP or HTTPS URL without credentials.", blocked_url: "Private or local URLs and redirects are not allowed.", timeout: "The website did not respond within the research time limit. Try another source.", cancelled: "Web research was cancelled.", http_error: "The website returned an unsuccessful HTTP response. Try another source.", unsupported_content: "This reader supports HTML, Markdown and plain text. Use a text version of this source.", request_failed: "The website could not be read. Check the URL or try another source.", search_blocked: "The search service blocked or rate-limited this request. Try again later or read a known URL.", unexpected_search_page: "The search page did not contain recognizable results. Try again later or read a known URL.", browser_unavailable: "The browser service is unavailable. Try again later or read a known URL.", cleanup_failed: "The browser session could not be confirmed closed. It has a short inactivity expiry; try again later.", }; export class WebFailure extends Error { constructor( public code: WebErrorCode, public status?: number, ) { super(messages[code]); } } export function webFailure(code: WebErrorCode, status?: number): WebResult { return { ok: false, code, message: messages[code], ...(status === undefined ? {} : { status }), }; } export function webError(error: unknown, signal?: AbortSignal): WebResult { if (signal?.aborted) return webFailure( signal.reason instanceof WebFailure ? signal.reason.code : "cancelled", ); if (error instanceof WebFailure) return webFailure(error.code, error.status); return webFailure("request_failed"); }