Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Playwright Client Errors

From Leeroopedia
Knowledge Sources
Domains Browser Automation, Error Handling
Last Updated 2026-02-12 00:00 GMT

Overview

Concrete tool for defining and managing Playwright-specific error classes and error serialization/deserialization provided by the Playwright library.

Description

This module defines the core error types used throughout the Playwright client:

  • TimeoutError extends Error and is thrown when an operation exceeds its configured timeout. Sets name to 'TimeoutError'.
  • TargetClosedError extends Error and is thrown when the target page, context, or browser has been closed during an operation.
  • isTargetClosedError() is a type guard that checks if an error is a TargetClosedError instance.
  • serializeError() converts an error (or arbitrary value) into a SerializedError protocol object, preserving message, stack, and name for Error instances.
  • parseError() reconstructs an Error from a SerializedError, restoring the correct error class (TimeoutError, TargetClosedError, or generic Error) based on the serialized name.

Usage

Use these error classes and utilities when handling or creating Playwright-specific errors. TimeoutError is exposed to end users via playwright.errors.TimeoutError. The serialization functions are used internally for error transport across the client-server protocol boundary.

Code Reference

Source Location

Signature

export class TimeoutError extends Error {
  constructor(message: string);
}

export class TargetClosedError extends Error {
  constructor(cause?: string);
}

export function isTargetClosedError(error: Error): boolean;

export function serializeError(e: any): SerializedError;

export function parseError(error: SerializedError): Error;

Import

import { TimeoutError, TargetClosedError, isTargetClosedError, serializeError, parseError } from 'playwright-core/src/client/errors';

I/O Contract

Inputs

Name Type Required Description
message (TimeoutError) string Yes The timeout error message
cause (TargetClosedError) string No Optional cause message; defaults to 'Target page, context or browser has been closed'
e (serializeError) any Yes The error or value to serialize
error (parseError) SerializedError Yes The serialized error object to reconstruct

Outputs

Name Type Description
serializeError() SerializedError A protocol-compatible serialized representation of the error
parseError() Error A reconstructed Error instance with the correct class (TimeoutError, TargetClosedError, or Error)
isTargetClosedError() boolean True if the error is a TargetClosedError instance

Usage Examples

import { TimeoutError, TargetClosedError, isTargetClosedError } from 'playwright-core';

try {
  await page.waitForSelector('.element', { timeout: 5000 });
} catch (e) {
  if (e instanceof TimeoutError) {
    console.error('Timed out waiting for element');
  }
  if (isTargetClosedError(e)) {
    console.error('Page was closed during operation');
  }
}

// Error serialization (internal usage)
const serialized = serializeError(new TimeoutError('timed out'));
const restored = parseError(serialized);
console.log(restored instanceof TimeoutError); // true

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment