Skip to main content

Error Handling

nokap provides a clear exception hierarchy so you can catch and respond to specific failure modes. Every exception inherits from a common NokapError base class, which means you can handle all nokap errors with a single except clause or drill down into specific error types for more targeted recovery. This page documents each exception, when it’s raised, and how to handle it gracefully in your code.

Exception Hierarchy

All nokap exceptions inherit from NokapError, making it easy to catch any nokap-specific error:

NokapError
├── ChromeNotFoundError   # Chrome binary not found
├── ChromeStartError      # Chrome failed to start
├── ConnectionError_      # CDP connection failed
├── NavigationError       # Page navigation failed
├── PageLoadTimeout       # Page didn't load in time
├── SelectorError         # CSS selector matched nothing
└── CDPError              # Chrome DevTools Protocol error

Catching All Errors

import nokap
from nokap import NokapError

try:
    nokap.webshot("https://example.com", "out.png")
except NokapError as e:
    print(f"nokap error: {e}")

Specific Error Handling

ChromeNotFoundError

Raised when nokap cannot locate a Chrome or Chromium binary:

from nokap import ChromeNotFoundError

try:
    nokap.webshot("https://example.com", "out.png")
except ChromeNotFoundError:
    print("Please install Chrome, run `nokap install-chrome`, or set CHROME_PATH")

Fix: Install Chrome/Chromium, run nokap install-chrome to download chrome-headless-shell, or set the CHROME_PATH environment variable.

ChromeStartError

Raised when Chrome is found but fails to start (e.g., missing shared libraries on Linux):

from nokap import ChromeStartError

try:
    nokap.webshot("https://example.com", "out.png")
except ChromeStartError as e:
    print(f"Chrome failed to start: {e}")
    print(f"Chrome stderr: {e.stderr}")

Fix: Check that Chrome runs from the command line. To pass extra flags such as --no-sandbox (sometimes needed when running as root in Linux containers), set the NOKAP_CHROME_ARGS environment variable.

Inside a macOS agent sandbox, the error message includes a hint about what the sandbox blocked. Use nokap install-chrome when full Chrome can’t start. Pipes are the default on macOS and Linux and avoid localhost binding. nokap selects --single-process if Chrome’s required macOS user-directory lookups fail with an I/O error, and retries with that flag when Chrome reports a Mach service registration denial. See Sandboxed Environments.

PageLoadTimeout

Raised when a page doesn’t finish loading within the timeout period:

from nokap import PageLoadTimeout

try:
    nokap.webshot("https://slow-site.example.com", "out.png")
except PageLoadTimeout as e:
    print(f"Timed out after {e.timeout}s loading: {e.url}")

SelectorError

Raised when a CSS selector matches no elements on the page:

from nokap import SelectorError

try:
    nokap.webshot("page.html", "out.png", selector="#missing")
except SelectorError as e:
    print(f"Selector not found: {e.selector}")

Fix: Check that the selector matches an element in the loaded page. Use browser DevTools to verify.

CDPError

Raised when Chrome returns an error response to a CDP command:

from nokap import CDPError

try:
    nokap.webshot("https://example.com", "out.png")
except CDPError as e:
    print(f"CDP protocol error: {e}")

Recovery Pattern

Module calls without renderer= use a shared default renderer. If its browser exits between calls, the next webshot() or from_html() call creates a replacement. Calling nokap.close() also resets the shared default:

import nokap

# First call launches Chrome
nokap.webshot("https://example.com", "first.png")

# Close the shared default renderer
nokap.close()

# Next call creates a new default renderer
nokap.webshot("https://example.com", "second.png")

An explicitly constructed Renderer has a fixed lifetime. Capture methods raise an error if it has been closed or its browser has exited; they do not relaunch Chrome. Passing renderer= to a module function preserves this behavior. Construct a new renderer to continue.

Next Steps

With a clear understanding of nokap’s exception hierarchy, you can write capture code that fails gracefully (whether that means retrying on a timeout, logging a missing selector, or alerting when Chrome can’t be found).

Now that you know how to handle errors, the performance guide will help you maximize throughput when processing many pages or running in batch pipelines.