Register a handler and subscribe to ``log.entryAdded`` if needed. Args: callback: User callback invoked with the shaped payload. category: ``console`` or ``error``. legacy: When ``True`` the callback receives the generated ``ConsoleLogEntr
(self, callback: Callable, category: str, legacy: bool = False)
| 328 | self._categories: dict[int, str] = {} |
| 329 | |
| 330 | def add_handler(self, callback: Callable, category: str, legacy: bool = False) -> int: |
| 331 | """Register a handler and subscribe to ``log.entryAdded`` if needed. |
| 332 | |
| 333 | Args: |
| 334 | callback: User callback invoked with the shaped payload. |
| 335 | category: ``console`` or ``error``. |
| 336 | legacy: When ``True`` the callback receives the generated |
| 337 | ``ConsoleLogEntry`` / ``JavascriptLogEntry`` dataclasses; |
| 338 | otherwise it receives :class:`ConsoleMessage` / |
| 339 | :class:`ScriptError`. |
| 340 | """ |
| 341 | entry_type = self._CATEGORY_ENTRY_TYPES[category] |
| 342 | |
| 343 | def _dispatch(params: Any) -> None: |
| 344 | if not isinstance(params, dict) or params.get("type") != entry_type: |
| 345 | return |
| 346 | if legacy: |
| 347 | payload = _legacy_log_entry(params) |
| 348 | elif category == self.CONSOLE: |
| 349 | payload = console_message_from_log_entry(params) |
| 350 | else: |
| 351 | payload = script_error_from_log_entry(params) |
| 352 | callback(payload) |
| 353 | |
| 354 | conn = self._script._conn |
| 355 | with self._lock: |
| 356 | callback_id = conn.add_callback(_EventRef(self.EVENT), _dispatch) |
| 357 | if not self._categories: |
| 358 | try: |
| 359 | self._subscription_id = _subscribe_to_event(conn, self.EVENT) |
| 360 | except Exception: |
| 361 | conn.remove_callback(_EventRef(self.EVENT), callback_id) |
| 362 | raise |
| 363 | self._categories[callback_id] = category |
| 364 | return callback_id |
| 365 | |
| 366 | def remove_handler(self, callback_id: int) -> None: |
| 367 | """Remove a handler; drops the session subscription with the last one.""" |
nothing calls this directly
no test coverage detected