Skip to content

Commit 7d4a961

Browse files
committed
Normalize Playwright exceptions
1 parent c3e6347 commit 7d4a961

File tree

16 files changed

+92
-51
lines changed

16 files changed

+92
-51
lines changed

src/Browser/Browser.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
namespace PlaywrightPHP\Browser;
1212

1313
use PlaywrightPHP\Configuration\PlaywrightConfig;
14+
use PlaywrightPHP\Exception\ProtocolErrorException;
15+
use PlaywrightPHP\Exception\RuntimeException;
1416
use PlaywrightPHP\Page\PageInterface;
1517
use PlaywrightPHP\Transport\TransportInterface;
1618

@@ -42,7 +44,7 @@ public function __construct(
4244
public function context(): BrowserContextInterface
4345
{
4446
if (null === $this->defaultContext) {
45-
throw new \RuntimeException('Default context is not available');
47+
throw new RuntimeException('Default context is not available');
4648
}
4749

4850
return $this->defaultContext;
@@ -60,7 +62,7 @@ public function newContext(array $options = []): BrowserContextInterface
6062
]);
6163

6264
if (!is_string($response['contextId'])) {
63-
throw new \RuntimeException('Invalid contextId returned from transport');
65+
throw new ProtocolErrorException('Invalid contextId returned from transport', 0);
6466
}
6567

6668
$context = new BrowserContext($this->transport, $response['contextId'], $this->config);

src/Browser/BrowserBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* @author Simon André <smn.andre@gmail.com>
2020
*/
21-
class BrowserBuilder
21+
final class BrowserBuilder
2222
{
2323
/**
2424
* @var array<string, mixed>

src/Browser/BrowserContext.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
use PlaywrightPHP\Configuration\PlaywrightConfig;
1414
use PlaywrightPHP\Event\EventDispatcherInterface;
15+
use PlaywrightPHP\Exception\ProtocolErrorException;
16+
use PlaywrightPHP\Exception\TransportException;
1517
use PlaywrightPHP\Network\NetworkThrottling;
1618
use PlaywrightPHP\Network\Route;
1719
use PlaywrightPHP\Page\Page;
@@ -60,10 +62,10 @@ public function dispatchEvent(string $eventName, array $params): void
6062
{
6163
if ('route' === $eventName) {
6264
if (!is_string($params['routeId'])) {
63-
throw new \RuntimeException('Invalid routeId in route event');
65+
throw new ProtocolErrorException('Invalid routeId in route event', 0);
6466
}
6567
if (!is_array($params['request'])) {
66-
throw new \RuntimeException('Invalid request data in route event');
68+
throw new ProtocolErrorException('Invalid request data in route event', 0);
6769
}
6870
$route = new Route(
6971
$this->transport,
@@ -126,11 +128,11 @@ public function newPage(array $options = []): PageInterface
126128

127129
if (isset($response['error'])) {
128130
$errorMsg = is_string($response['error']) ? $response['error'] : 'Unknown transport error';
129-
throw new \RuntimeException('Transport error in newPage: '.$errorMsg);
131+
throw new TransportException('Transport error in newPage: '.$errorMsg);
130132
}
131133

132134
if (!isset($response['pageId']) || !is_string($response['pageId'])) {
133-
throw new \RuntimeException('No valid pageId returned from transport in newPage');
135+
throw new ProtocolErrorException('No valid pageId returned from transport in newPage', 0);
134136
}
135137

136138
$page = new Page($this->transport, $this, $response['pageId'], $this->config);
@@ -147,7 +149,7 @@ public function clipboardText(): string
147149
]);
148150

149151
if (!is_string($response['value'])) {
150-
throw new \RuntimeException('Invalid clipboard text response');
152+
throw new ProtocolErrorException('Invalid clipboard text response', 0);
151153
}
152154

153155
return $response['value'];
@@ -210,7 +212,7 @@ public function cookies(?array $urls = null): array
210212
]);
211213

212214
if (!is_array($response['cookies'])) {
213-
throw new \RuntimeException('Invalid cookies response');
215+
throw new ProtocolErrorException('Invalid cookies response', 0);
214216
}
215217

216218
/** @phpstan-var array<array<string, mixed>> $cookies */
@@ -271,7 +273,7 @@ public function storageState(?string $path = null): array
271273
]);
272274

273275
if (!is_array($response['storageState'])) {
274-
throw new \RuntimeException('Invalid storageState response');
276+
throw new ProtocolErrorException('Invalid storageState response', 0);
275277
}
276278

277279
/** @phpstan-var array<string, mixed> $storageState */
@@ -420,13 +422,13 @@ public function disableNetworkThrottling(): void
420422
private function validateTransportArray(mixed $data, string $context = ''): array
421423
{
422424
if (!is_array($data)) {
423-
throw new \RuntimeException("Invalid {$context} data in transport response");
425+
throw new ProtocolErrorException("Invalid {$context} data in transport response", 0);
424426
}
425427

426428
$result = [];
427429
foreach ($data as $key => $value) {
428430
if (!is_string($key)) {
429-
throw new \RuntimeException("Invalid {$context} payload: non-string key in transport response");
431+
throw new ProtocolErrorException("Invalid {$context} payload: non-string key in transport response", 0);
430432
}
431433
$result[$key] = $value;
432434
}

src/Browser/StorageState.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace PlaywrightPHP\Browser;
1212

13+
use PlaywrightPHP\Exception\RuntimeException;
14+
1315
/**
1416
* Storage state container for cookies and localStorage data.
1517
*
@@ -64,7 +66,7 @@ public static function fromFile(string $filePath): self
6466

6567
$content = \file_get_contents($filePath);
6668
if (false === $content) {
67-
throw new \RuntimeException(\sprintf('Failed to read storage state file: %s', $filePath));
69+
throw new RuntimeException(\sprintf('Failed to read storage state file: %s', $filePath));
6870
}
6971

7072
return self::fromJson($content);
@@ -111,12 +113,12 @@ public function saveToFile(string $filePath): void
111113
{
112114
$directory = \dirname($filePath);
113115
if (!\is_dir($directory) && !\mkdir($directory, 0755, true) && !\is_dir($directory)) {
114-
throw new \RuntimeException(\sprintf('Failed to create directory: %s', $directory));
116+
throw new RuntimeException(\sprintf('Failed to create directory: %s', $directory));
115117
}
116118

117119
$result = \file_put_contents($filePath, $this->toJson(JSON_PRETTY_PRINT));
118120
if (false === $result) {
119-
throw new \RuntimeException(\sprintf('Failed to write storage state file: %s', $filePath));
121+
throw new RuntimeException(\sprintf('Failed to write storage state file: %s', $filePath));
120122
}
121123
}
122124

src/Configuration/PlaywrightConfigBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,16 @@ public function build(): PlaywrightConfig
265265
if ($this->slowMoMs < 0) {
266266
throw new \InvalidArgumentException('slowMoMs must be >= 0');
267267
}
268-
if (null !== $this->channel && '' === $this->channel) {
268+
if ('' === $this->channel) {
269269
$this->channel = null;
270270
}
271-
if (null !== $this->downloadsDir && '' === $this->downloadsDir) {
271+
if ('' === $this->downloadsDir) {
272272
$this->downloadsDir = null;
273273
}
274-
if (null !== $this->videosDir && '' === $this->videosDir) {
274+
if ('' === $this->videosDir) {
275275
$this->videosDir = null;
276276
}
277-
if ($this->tracingEnabled && null !== $this->traceDir && '' === $this->traceDir) {
277+
if ($this->tracingEnabled && '' === $this->traceDir) {
278278
$this->traceDir = null;
279279
}
280280

src/Console/ConsoleMessage.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace PlaywrightPHP\Console;
1212

13+
use PlaywrightPHP\Exception\ProtocolErrorException;
14+
1315
/**
1416
* @author Simon André <smn.andre@gmail.com>
1517
*/
@@ -32,7 +34,7 @@ public function type(): string
3234
{
3335
$type = $this->data['type'];
3436
if (!is_string($type)) {
35-
throw new \RuntimeException('Invalid console message type');
37+
throw new ProtocolErrorException('Invalid console message type', 0);
3638
}
3739

3840
return $type;
@@ -42,7 +44,7 @@ public function text(): string
4244
{
4345
$text = $this->data['text'];
4446
if (!is_string($text)) {
45-
throw new \RuntimeException('Invalid console message text');
47+
throw new ProtocolErrorException('Invalid console message text', 0);
4648
}
4749

4850
return $text;
@@ -55,7 +57,7 @@ public function args(): array
5557
{
5658
$args = $this->data['args'];
5759
if (!is_array($args)) {
58-
throw new \RuntimeException('Invalid console message args');
60+
throw new ProtocolErrorException('Invalid console message args', 0);
5961
}
6062

6163
return $args;
@@ -68,13 +70,13 @@ public function location(): array
6870
{
6971
$location = $this->data['location'];
7072
if (!is_array($location)) {
71-
throw new \RuntimeException('Invalid console message location');
73+
throw new ProtocolErrorException('Invalid console message location', 0);
7274
}
7375

7476
$result = [];
7577
foreach ($location as $key => $value) {
7678
if (!is_string($key)) {
77-
throw new \RuntimeException('Invalid console message location: non-string key');
79+
throw new ProtocolErrorException('Invalid console message location: non-string key', 0);
7880
}
7981
$result[$key] = $value;
8082
}

src/Exception/RuntimeException.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the playwright-php/playwright package.
7+
* For the full copyright and license information, please view
8+
* the LICENSE file that was distributed with this source code.
9+
*/
10+
11+
namespace PlaywrightPHP\Exception;
12+
13+
/**
14+
* Thrown when a runtime error occurs that is specific to Playwright operations.
15+
*
16+
* @author Simon André <smn.andre@gmail.com>
17+
*/
18+
class RuntimeException extends PlaywrightException
19+
{
20+
}

src/Frame/Frame.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace PlaywrightPHP\Frame;
1212

1313
use PlaywrightPHP\Exception\PlaywrightException;
14+
use PlaywrightPHP\Exception\ProtocolErrorException;
1415
use PlaywrightPHP\Locator\Locator;
1516
use PlaywrightPHP\Locator\LocatorInterface;
1617
use PlaywrightPHP\Transport\TransportInterface;
@@ -75,7 +76,7 @@ public function name(): string
7576
$response = $this->sendCommand('frame.name');
7677
$value = $response['value'] ?? null;
7778
if (!is_string($value)) {
78-
throw new \RuntimeException('Invalid frame.name response');
79+
throw new ProtocolErrorException('Invalid frame.name response', 0);
7980
}
8081

8182
return $value;
@@ -86,7 +87,7 @@ public function url(): string
8687
$response = $this->sendCommand('frame.url');
8788
$value = $response['value'] ?? null;
8889
if (!is_string($value)) {
89-
throw new \RuntimeException('Invalid frame.url response');
90+
throw new ProtocolErrorException('Invalid frame.url response', 0);
9091
}
9192

9293
return $value;

src/Locator/Locator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace PlaywrightPHP\Locator;
1212

1313
use PlaywrightPHP\Exception\PlaywrightException;
14+
use PlaywrightPHP\Exception\ProtocolErrorException;
1415
use PlaywrightPHP\Exception\TimeoutException;
1516
use PlaywrightPHP\Frame\FrameLocator;
1617
use PlaywrightPHP\Frame\FrameLocatorInterface;
@@ -138,7 +139,7 @@ public function innerHTML(): string
138139
$response = $this->sendCommand('locator.innerHTML');
139140
$value = $response['value'];
140141
if (!is_string($value)) {
141-
throw new \RuntimeException('Invalid innerHTML response');
142+
throw new ProtocolErrorException('Invalid innerHTML response', 0);
142143
}
143144

144145
return $value;
@@ -149,7 +150,7 @@ public function innerText(): string
149150
$response = $this->sendCommand('locator.innerText');
150151
$value = $response['value'];
151152
if (!is_string($value)) {
152-
throw new \RuntimeException('Invalid innerText response');
153+
throw new ProtocolErrorException('Invalid innerText response', 0);
153154
}
154155

155156
return $value;
@@ -160,7 +161,7 @@ public function inputValue(): string
160161
$response = $this->sendCommand('locator.inputValue');
161162
$value = $response['value'];
162163
if (!is_string($value)) {
163-
throw new \RuntimeException('Invalid inputValue response');
164+
throw new ProtocolErrorException('Invalid inputValue response', 0);
164165
}
165166

166167
return $value;

src/Network/Request.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace PlaywrightPHP\Network;
1212

13+
use PlaywrightPHP\Exception\ProtocolErrorException;
14+
1315
/**
1416
* @author Simon André <smn.andre@gmail.com>
1517
*/
@@ -27,7 +29,7 @@ public function url(): string
2729
{
2830
$url = $this->data['url'];
2931
if (!is_string($url)) {
30-
throw new \RuntimeException('Invalid URL in request data');
32+
throw new ProtocolErrorException('Invalid URL in request data', 0);
3133
}
3234

3335
return $url;
@@ -37,7 +39,7 @@ public function method(): string
3739
{
3840
$method = $this->data['method'];
3941
if (!is_string($method)) {
40-
throw new \RuntimeException('Invalid method in request data');
42+
throw new ProtocolErrorException('Invalid method in request data', 0);
4143
}
4244

4345
return $method;
@@ -96,7 +98,7 @@ public function resourceType(): string
9698
{
9799
$resourceType = $this->data['resourceType'];
98100
if (!is_string($resourceType)) {
99-
throw new \RuntimeException('Invalid resourceType in request data');
101+
throw new ProtocolErrorException('Invalid resourceType in request data', 0);
100102
}
101103

102104
return $resourceType;

0 commit comments

Comments
 (0)