UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

2 lines • 4.69 kB
//!extensions/browser/src/browser/errors.ts const BROWSER_ENDPOINT_BLOCKED_MESSAGE=`browser endpoint blocked by policy`;const BROWSER_NAVIGATION_BLOCKED_MESSAGE=`browser navigation blocked by policy`;const BROWSER_ERROR_REASONS={noDisplayForHeadedProfile:`no_display_for_headed_profile`,navigationBlocked:`navigation_blocked`};const BROWSER_ACT_ERROR_CODES={kindRequired:`ACT_KIND_REQUIRED`,invalidRequest:`ACT_INVALID_REQUEST`,selectorUnsupported:`ACT_SELECTOR_UNSUPPORTED`,evaluateDisabled:`ACT_EVALUATE_DISABLED`,unsupportedForExistingSession:`ACT_EXISTING_SESSION_UNSUPPORTED`,targetIdMismatch:`ACT_TARGET_ID_MISMATCH`,operationFailed:`ACT_OPERATION_FAILED`};const BROWSER_ACT_ERROR_CODE_VALUES=new Set(Object.values(BROWSER_ACT_ERROR_CODES));function isBrowserActErrorCode(e){return typeof e===`string`&&BROWSER_ACT_ERROR_CODE_VALUES.has(e)}const NO_DISPLAY_HEADLESS_SOURCES=[`request`,`env`,`profile`,`config`,`default`];var BrowserError=class extends Error{constructor(e,t=500,n){super(e,n);this.name=new.target.name;this.status=t}};var BrowserActionError=class extends BrowserError{constructor(...e){super(...e);this.code=BROWSER_ACT_ERROR_CODES.operationFailed}};var BrowserCdpEndpointBlockedError=class extends BrowserError{constructor(e){super(BROWSER_ENDPOINT_BLOCKED_MESSAGE,400,e)}};var BrowserValidationError=class extends BrowserError{constructor(e){super(e,400)}};var BrowserTargetAmbiguousError=class extends BrowserError{constructor(){super(`ambiguous browser tab reference`,409)}};var BrowserTabNotFoundError=class extends BrowserError{constructor(e){const t=e?.input?.trim();const n=t?/^\d+$/.test(t)?`tab not found: browser tab "${t}" not found. Numeric values are not tab targets; use a stable tab id like "t1", a label, or a raw targetId. For positional selection, use "openclaw browser tab select ${t}".`:`tab not found: browser tab "${t}" not found. Use action=tabs and pass suggestedTargetId, tabId, label, or raw targetId.`:`tab not found`;super(n,404)}};var BrowserProfileNotFoundError=class extends BrowserError{constructor(e){super(e,404)}};var BrowserConflictError=class extends BrowserError{constructor(e){super(e,409)}};var BrowserResetUnsupportedError=class extends BrowserError{constructor(e){super(e,400)}};var BrowserProfileUnavailableError=class extends BrowserError{constructor(e,t){super(e,409,t);this.metadata=t?.metadata}};var BrowserResourceExhaustedError=class extends BrowserError{constructor(e){super(e,507)}};function toBrowserErrorResponse(e){if(e instanceof BrowserActionError)return{status:e.status,message:e.message,code:e.code};if(e instanceof BrowserProfileUnavailableError&&e.metadata)return{status:e.status,message:e.message,...e.metadata};if(e instanceof BrowserError)return{status:e.status,message:e.message};if(e instanceof Error&&e.name===`BlockedBrowserTargetError`)return{status:409,message:e.message,reason:BROWSER_ERROR_REASONS.navigationBlocked};if(e instanceof Error&&e.name===`SsrFBlockedError`)return{status:400,message:BROWSER_NAVIGATION_BLOCKED_MESSAGE,reason:BROWSER_ERROR_REASONS.navigationBlocked};if(e instanceof Error&&e.name===`InvalidBrowserNavigationUrlError`)return{status:400,message:e.message,reason:BROWSER_ERROR_REASONS.navigationBlocked};return null}function parseNoDisplayDetails(e){if(!e||typeof e!==`object`||Array.isArray(e))return null;const t=e;if(typeof t.profile!==`string`||t.profile.length===0||t.requestedHeadless!==false||!NO_DISPLAY_HEADLESS_SOURCES.includes(t.headlessSource)||t.displayPresent!==false)return null;return{profile:t.profile,requestedHeadless:false,headlessSource:t.headlessSource,displayPresent:false}}function parseBrowserErrorPayload(e){if(!e||typeof e!==`object`||Array.isArray(e))return null;const t=e;if(typeof t.error!==`string`||t.error.length===0)return null;const n=isBrowserActErrorCode(t.code)?t.code:void 0;const r=t.unrecognizedCode===true||t.code!==void 0&&!n?true:void 0;const i=n?{code:n}:r?{unrecognizedCode:true}:{};if(t.reason===BROWSER_ERROR_REASONS.navigationBlocked)return{error:t.error,...i,reason:t.reason};if(t.reason===BROWSER_ERROR_REASONS.noDisplayForHeadedProfile){const e=parseNoDisplayDetails(t.details);if(e)return{error:t.error,...i,reason:t.reason,details:e}}return{error:t.error,...i}}export{BrowserConflictError as a,BrowserProfileUnavailableError as c,BrowserTabNotFoundError as d,BrowserTargetAmbiguousError as f,toBrowserErrorResponse as h,BrowserCdpEndpointBlockedError as i,BrowserResetUnsupportedError as l,parseBrowserErrorPayload as m,BROWSER_ERROR_REASONS as n,BrowserError as o,BrowserValidationError as p,BrowserActionError as r,BrowserProfileNotFoundError as s,BROWSER_ACT_ERROR_CODES as t,BrowserResourceExhaustedError as u};