{"record":{"id":"cb94341839469482","repo":"jackwener/OpenCLI","slug":"browser-page-required","errorCode":null,"errorMessage":"Browser page required","messagePattern":"Browser page required","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/boss/utils.js","lineNumber":18,"sourceCode":"import { ArgumentError, AuthRequiredError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';\n\n// ── Constants ───────────────────────────────────────────────────────────────\nconst BOSS_DOMAIN = 'www.zhipin.com';\nconst CHAT_URL = `https://${BOSS_DOMAIN}/web/chat/index`;\nconst COOKIE_EXPIRED_CODES = new Set([7, 37]);\nconst COOKIE_EXPIRED_MSG = 'Cookie 已过期！请在当前 Chrome 浏览器中重新登录 BOSS 直聘。';\nconst AMBIGUOUS_AUTH_CODE = 37;\nconst ENVIRONMENT_REJECTED_MARKERS = ['环境存在异常', '环境异常', 'abnormal environment'];\nconst RECRUITER_ONLY_MSG = '该命令仅支持招聘端（BOSS 端）账号，请使用招聘者账号登录后重试。';\nconst DEFAULT_TIMEOUT = 15_000;\n// ── Core helpers ────────────────────────────────────────────────────────────\n/**\n * Assert that page is available (non-null).\n */\nexport function requirePage(page) {\n    if (!page)\n        throw new CommandExecutionError('Browser page required');\n}\nexport function readPositiveInteger(raw, name, fallback, max) {\n    const value = raw === undefined || raw === null || raw === '' ? fallback : Number(raw);\n    if (!Number.isInteger(value) || value < 1) {\n        throw new ArgumentError(`boss ${name} must be a positive integer`);\n    }\n    if (max !== undefined && value > max) {\n        throw new ArgumentError(`boss ${name} must be <= ${max}`);\n    }\n    return value;\n}\nexport function readRequiredString(raw, name) {\n    const value = String(raw ?? '').trim();\n    if (!value) {\n        throw new ArgumentError(`boss ${name} cannot be empty`);\n    }\n    return value;\n}","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/boss/utils.js#L1-L36","documentation":"requirePage is the guard used by all boss commands to assert that a browser page object was provided. If page is null/undefined the command cannot drive a browser, so it throws this CommandExecutionError immediately.","triggerScenarios":"Invoking a boss command without an active browser session (opencli running without a browser connection), or programmatically calling the underlying func with page omitted/null.","commonSituations":"Running the CLI in an environment where no browser was launched/attached; browser crashed and the session returned null; forgetting to open/connect a browser before running boss commands; test harnesses passing no page.","solutions":["Launch or connect a browser session before running the command (opencli's browser open/connect flow)","Check that the previous browser process did not crash — restart it if needed","When calling func directly, always pass the wrapped page object","Add a session health check before batch runs so a dead browser fails fast","Catch CommandExecutionError and prompt the user to start a browser session"],"exampleFix":"// before\nawait cli('boss', 'search', { query: 'java' }); // no browser attached\n// after\nawait opencli.browser.open();          // ensure a session exists\nconst page = await opencli.browser.getPage();\nif (!page) throw new Error('start a browser session first');\nawait cli('boss', 'search', { query: 'java' });","handlingStrategy":"validation","validationCode":"if (!page) throw new Error('open a browser session before running boss commands');","typeGuard":"function hasPage(s) { return Boolean(s && typeof s.evaluate === 'function'); }","tryCatchPattern":"try {\n  return await cli('boss', cmd, opts);\n} catch (e) {\n  if (String(e.message).includes('Browser page required')) {\n    await openBrowserSession();\n    return cli('boss', cmd, opts);\n  }\n  throw e;\n}","preventionTips":["Open/connect the browser session before any boss command","Health-check the session in long-running batch jobs","Restart crashed browser processes promptly","Never call command internals without a page object"],"tags":["missing-context","browser-session","precondition"],"backgroundTag":"missing-required-context","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}