Skip to content

Commit e6a956c

Browse files
authored
Merge pull request php-webdriver#1027 from OndraM/feature/exceptions-tree
Refactor internal exceptions
2 parents 160c23a + 6a8998c commit e6a956c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+425
-153
lines changed

.php-cs-fixer.dist.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
'implode_call' => true,
2727
'is_null' => true,
2828
'lambda_not_used_import' => true,
29-
'linebreak_after_opening_tag' => true,
3029
'list_syntax' => true,
3130
'lowercase_cast' => true,
3231
'lowercase_static_reference' => true,

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
This project versioning adheres to [Semantic Versioning](http://semver.org/).
33

44
## Unreleased
5+
### Added
6+
- `PhpWebDriverExceptionInterface` as a common interface to identify all exceptions thrown in php-webdriver
57

68
### Changed
79
- Require PHP ^7.3.
10+
- Throw `UnexpectedResponseException` instead of `UnknownErrorException` in `findElement()` and `findElements()` methods.
11+
- Throw custom php-webdriver exceptions instead of native PHP SPL exceptions.
12+
- Do not mix internal non-W3C WebDriver exceptions, separate them into own namespace.
813

914
## 1.13.0 - 2022-10-03
1015
### Added

lib/AbstractWebDriverCheckboxOrRadio.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Facebook\WebDriver;
44

5+
use Facebook\WebDriver\Exception\InvalidElementStateException;
56
use Facebook\WebDriver\Exception\NoSuchElementException;
67
use Facebook\WebDriver\Exception\UnexpectedTagNameException;
7-
use Facebook\WebDriver\Exception\WebDriverException;
88
use Facebook\WebDriver\Support\XPathEscaper;
99

1010
/**
@@ -30,7 +30,7 @@ public function __construct(WebDriverElement $element)
3030

3131
$this->name = $element->getAttribute('name');
3232
if ($this->name === null) {
33-
throw new WebDriverException('The input does not have a "name" attribute.');
33+
throw new InvalidElementStateException('The input does not have a "name" attribute.');
3434
}
3535

3636
$this->element = $element;

lib/Cookie.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Facebook\WebDriver;
44

5-
use InvalidArgumentException;
5+
use Facebook\WebDriver\Exception\Internal\LogicException;
66

77
/**
88
* Set values of an cookie.
@@ -36,10 +36,10 @@ public function __construct($name, $value)
3636
public static function createFromArray(array $cookieArray)
3737
{
3838
if (!isset($cookieArray['name'])) {
39-
throw new InvalidArgumentException('Cookie name should be set');
39+
throw LogicException::forError('Cookie name should be set');
4040
}
4141
if (!isset($cookieArray['value'])) {
42-
throw new InvalidArgumentException('Cookie value should be set');
42+
throw LogicException::forError('Cookie value should be set');
4343
}
4444
$cookie = new self($cookieArray['name'], $cookieArray['value']);
4545

@@ -107,7 +107,7 @@ public function getPath()
107107
public function setDomain($domain)
108108
{
109109
if (mb_strpos($domain, ':') !== false) {
110-
throw new InvalidArgumentException(sprintf('Cookie domain "%s" should not contain a port', $domain));
110+
throw LogicException::forError(sprintf('Cookie domain "%s" should not contain a port', $domain));
111111
}
112112

113113
$this->offsetSet('domain', $domain);
@@ -258,11 +258,11 @@ public function offsetUnset($offset)
258258
protected function validateCookieName($name)
259259
{
260260
if ($name === null || $name === '') {
261-
throw new InvalidArgumentException('Cookie name should be non-empty');
261+
throw LogicException::forError('Cookie name should be non-empty');
262262
}
263263

264264
if (mb_strpos($name, ';') !== false) {
265-
throw new InvalidArgumentException('Cookie name should not contain a ";"');
265+
throw LogicException::forError('Cookie name should not contain a ";"');
266266
}
267267
}
268268

@@ -272,7 +272,7 @@ protected function validateCookieName($name)
272272
protected function validateCookieValue($value)
273273
{
274274
if ($value === null) {
275-
throw new InvalidArgumentException('Cookie value is required when setting a cookie');
275+
throw LogicException::forError('Cookie value is required when setting a cookie');
276276
}
277277
}
278278
}

lib/Exception/DriverServerDiedException.php

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Facebook\WebDriver\Exception\Internal;
4+
5+
use Facebook\WebDriver\Exception\PhpWebDriverExceptionInterface;
6+
7+
/**
8+
* The driver server process is unexpectedly no longer available.
9+
*/
10+
class DriverServerDiedException extends \RuntimeException implements PhpWebDriverExceptionInterface
11+
{
12+
public function __construct(\Exception $previous = null)
13+
{
14+
parent::__construct('The driver server has died.', 0, $previous);
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Facebook\WebDriver\Exception\Internal;
4+
5+
use Facebook\WebDriver\Exception\PhpWebDriverExceptionInterface;
6+
7+
/**
8+
* Exception class thrown when a filesystem related operation failure happens.
9+
*/
10+
class IOException extends \LogicException implements PhpWebDriverExceptionInterface
11+
{
12+
public static function forFileError(string $message, string $path): self
13+
{
14+
return new self(sprintf($message . ' ("%s")', $path));
15+
}
16+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Facebook\WebDriver\Exception\Internal;
4+
5+
use Facebook\WebDriver\Exception\PhpWebDriverExceptionInterface;
6+
7+
/**
8+
* Exception thrown when error in program logic occurs. This includes invalid domain data and unexpected data states.
9+
*/
10+
class LogicException extends \LogicException implements PhpWebDriverExceptionInterface
11+
{
12+
public static function forError(string $message): self
13+
{
14+
return new self($message);
15+
}
16+
17+
public static function forInvalidHttpMethod(string $url, string $httpMethod, array $params): self
18+
{
19+
return new self(
20+
sprintf(
21+
'The http method called for "%s" is "%s", but it has to be POST' .
22+
' if you want to pass the JSON params %s',
23+
$url,
24+
$httpMethod,
25+
json_encode($params)
26+
)
27+
);
28+
}
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Facebook\WebDriver\Exception\Internal;
4+
5+
use Facebook\WebDriver\Exception\PhpWebDriverExceptionInterface;
6+
use Symfony\Component\Process\Process;
7+
8+
/**
9+
* Exception thrown if an error which can only be found on runtime occurs.
10+
*/
11+
class RuntimeException extends \RuntimeException implements PhpWebDriverExceptionInterface
12+
{
13+
public static function forError(string $message): self
14+
{
15+
return new self($message);
16+
}
17+
18+
/**
19+
* @param Process $process
20+
* @return RuntimeException
21+
*/
22+
public static function forDriverError(Process $process): self
23+
{
24+
return new self(
25+
sprintf(
26+
'Error starting driver executable "%s": %s',
27+
$process->getCommandLine(),
28+
$process->getErrorOutput()
29+
)
30+
);
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Facebook\WebDriver\Exception\Internal;
4+
5+
use Facebook\WebDriver\Exception\PhpWebDriverExceptionInterface;
6+
7+
/**
8+
* Exception thrown on invalid or unexpected server response.
9+
*/
10+
class UnexpectedResponseException extends \RuntimeException implements PhpWebDriverExceptionInterface
11+
{
12+
public static function forError(string $message): self
13+
{
14+
return new self($message);
15+
}
16+
17+
public static function forJsonDecodingError(int $jsonLastError, string $rawResults): self
18+
{
19+
return new self(
20+
sprintf(
21+
"JSON decoding of remote response failed.\n" .
22+
"Error code: %d\n" .
23+
"The response: '%s'\n",
24+
$jsonLastError,
25+
$rawResults
26+
)
27+
);
28+
}
29+
}

0 commit comments

Comments
 (0)