Skip to content

Commit 139f0d0

Browse files
committed
Improve Exception Handling
Added more exceptions to better handle exceptions in the package. To avoid BC Break all new Exception extends the Pdp\Exception which becomes the marker/base exception for the package.
1 parent f977096 commit 139f0d0

11 files changed

+147
-54
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ All Notable changes to `PHP Domain Parser` **5.x** series will be documented in
1818
- `Pdp\Domain::resolve` attach a public suffix to the `Pdp\Domain`.
1919
- `Pdp\Domain::isResolvable` tells whether the current `Pdp\Domain` can have a public suffix attached to it or not.
2020
- `Pdp\PublicSuffix::createFromDomain` returns a new `Pdp\PublicSuffix` object from a `Pdp\Domain`object
21+
- `Pdp\Exception` sub namespace to organize exception. All exception extends the `Pdp\Exception` class to prevent BC break.
2122

2223
### Fixed
2324

src/Domain.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
namespace Pdp;
1717

1818
use JsonSerializable;
19+
use Pdp\Exception\CouldNotProcessDomain;
20+
use Pdp\Exception\InvalidDomain;
1921
use TypeError;
2022

2123
/**
@@ -88,9 +90,7 @@ public function __construct($domain = null, PublicSuffix $publicSuffix = null)
8890
*
8991
* @param PublicSuffix $publicSuffix
9092
*
91-
* @throws Exception If the domain can not contain a public suffix
92-
* @throws Exception If the domain value is the same as the public suffix value
93-
* @throws Exception If the domain can not be match with the public suffix
93+
* @throws InvalidDomain If the public suffic can not be attached to the domain
9494
*
9595
* @return PublicSuffix
9696
*/
@@ -101,17 +101,17 @@ private function setPublicSuffix(PublicSuffix $publicSuffix): PublicSuffix
101101
}
102102

103103
if (!$this->isResolvable()) {
104-
throw new Exception(sprintf('The domain `%s` can not contain a public suffix', $this->domain));
104+
throw new InvalidDomain(sprintf('The domain `%s` can not contain a public suffix', $this->domain));
105105
}
106106

107107
$publicSuffix = $this->normalize($publicSuffix);
108108
$psContent = $publicSuffix->getContent();
109109
if ($this->domain === $psContent) {
110-
throw new Exception(sprintf('The public suffix `%s` can not be equal to the domain name `%s`', $psContent, $this->domain));
110+
throw new InvalidDomain(sprintf('The public suffix `%s` can not be equal to the domain name `%s`', $psContent, $this->domain));
111111
}
112112

113113
if ('.'.$psContent !== substr($this->domain, - strlen($psContent) - 1)) {
114-
throw new Exception(sprintf('The public suffix `%s` can not be assign to the domain name `%s`', $psContent, $this->domain));
114+
throw new InvalidDomain(sprintf('The public suffix `%s` can not be assign to the domain name `%s`', $psContent, $this->domain));
115115
}
116116

117117
return $publicSuffix;
@@ -423,14 +423,14 @@ public function resolve($publicSuffix): self
423423
*
424424
* @param mixed $subDomain the subdomain to add
425425
*
426-
* @throws Exception If the Sub domain is invalid or can not be added to the current Domain
426+
* @throws CouldNotProcessDomain If the Sub domain can not be added to the current Domain
427427
*
428428
* @return self
429429
*/
430430
public function withSubDomain($subDomain): self
431431
{
432432
if (null === $this->publicSuffix->getContent()) {
433-
throw new Exception('A subdomain can not be added to a domain without a public suffix part.');
433+
throw new CouldNotProcessDomain('A subdomain can not be added to a domain without a public suffix part.');
434434
}
435435

436436
$subDomain = $this->filterSubDomain($subDomain);
@@ -489,14 +489,14 @@ private function filterSubDomain($subDomain)
489489
*
490490
* @param mixed $publicSuffix
491491
*
492-
* @throws Exception If the public suffix is invalid or can not be added to the current Domain
492+
* @throws CouldNotProcessDomain If the public suffix can not be added to the current Domain
493493
*
494494
* @return self
495495
*/
496496
public function withPublicSuffix($publicSuffix): self
497497
{
498498
if (null === $this->publicSuffix->getContent()) {
499-
throw new Exception('A public suffix can not be added to a domain without a public suffix part.');
499+
throw new CouldNotProcessDomain('A public suffix can not be added to a domain without a public suffix part.');
500500
}
501501

502502
if (!$publicSuffix instanceof PublicSuffix) {
@@ -556,16 +556,15 @@ public function append($label): self
556556
* @param int $key
557557
* @param mixed $label
558558
*
559-
* @throws Exception If the key is out of bounds
560-
* @throws Exception If the label is invalid
559+
* @throws CouldNotProcessDomain If the key is out of bounds
561560
*
562561
* @return self
563562
*/
564563
public function withLabel(int $key, $label): self
565564
{
566565
$nb_labels = count($this->labels);
567566
if ($key < - $nb_labels - 1 || $key > $nb_labels) {
568-
throw new Exception(sprintf('the given key `%s` is invalid', $key));
567+
throw new CouldNotProcessDomain(sprintf('the given key `%s` is invalid', $key));
569568
}
570569

571570
if (0 > $key) {
@@ -605,37 +604,38 @@ public function withLabel(int $key, $label): self
605604
* @param int $key
606605
* @param int ...$keys remaining keys to remove
607606
*
608-
* @throws Exception If the key is out of bounds
607+
* @throws CouldNotProcessDomain If the key is out of bounds
609608
*
610609
* @return self
611610
*/
612611
public function withoutLabel(int $key, int ...$keys): self
613612
{
614613
array_unshift($keys, $key);
615614
$nb_labels = count($this->labels);
616-
$mapper = function (int $key) use ($nb_labels): int {
615+
foreach ($keys as &$key) {
617616
if (- $nb_labels > $key || $nb_labels - 1 < $key) {
618-
throw new Exception(sprintf('the key `%s` is invalid', $key));
617+
throw new CouldNotProcessDomain(sprintf('the key `%s` is invalid', $key));
619618
}
620619

621620
if (0 > $key) {
622-
return $nb_labels + $key;
621+
$key += $nb_labels;
623622
}
623+
}
624+
unset($key);
624625

625-
return $key;
626-
};
627-
628-
$deleted_keys = array_keys(array_count_values(array_map($mapper, $keys)));
629-
$filter = function ($key) use ($deleted_keys): bool {
630-
return !in_array($key, $deleted_keys, true);
631-
};
626+
$deleted_keys = array_keys(array_count_values($keys));
627+
$labels = [];
628+
foreach ($this->labels as $key => $label) {
629+
if (!in_array($key, $deleted_keys, true)) {
630+
$labels[] = $label;
631+
}
632+
}
632633

633-
$labels = array_filter($this->labels, $filter, ARRAY_FILTER_USE_KEY);
634634
if (empty($labels)) {
635635
return new self();
636636
}
637637

638-
$domain = implode('.', array_reverse(array_values($labels)));
638+
$domain = implode('.', array_reverse($labels));
639639
$psContent = $this->publicSuffix->getContent();
640640
if (null === $psContent || '.'.$psContent !== substr($domain, - strlen($psContent) - 1)) {
641641
return new self($domain);

src/Exception/CouldNotLoadRules.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* PHP Domain Parser: Public Suffix List based URL parsing.
5+
*
6+
* @see http://github.com/jeremykendall/php-domain-parser for the canonical source repository
7+
*
8+
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Pdp\Exception;
17+
18+
use Pdp\Exception as BaseException;
19+
20+
class CouldNotLoadRules extends BaseException
21+
{
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* PHP Domain Parser: Public Suffix List based URL parsing.
5+
*
6+
* @see http://github.com/jeremykendall/php-domain-parser for the canonical source repository
7+
*
8+
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Pdp\Exception;
17+
18+
use Pdp\Exception as BaseException;
19+
20+
class CouldNotProcessDomain extends BaseException
21+
{
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* PHP Domain Parser: Public Suffix List based URL parsing.
5+
*
6+
* @see http://github.com/jeremykendall/php-domain-parser for the canonical source repository
7+
*
8+
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Pdp\Exception;
17+
18+
use Pdp\Exception as BaseException;
19+
20+
class CouldNotResolvePublicSuffix extends BaseException
21+
{
22+
}

src/Exception/InvalidDomain.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* PHP Domain Parser: Public Suffix List based URL parsing.
5+
*
6+
* @see http://github.com/jeremykendall/php-domain-parser for the canonical source repository
7+
*
8+
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Pdp\Exception;
17+
18+
use Pdp\Exception as BaseException;
19+
20+
class InvalidDomain extends BaseException
21+
{
22+
}

src/IDNAConverterTrait.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Pdp;
1717

18+
use Pdp\Exception\InvalidDomain;
1819
use TypeError;
1920

2021
/**
@@ -71,7 +72,7 @@ private static function getIdnErrors(int $error_bit): string
7172
*
7273
* @param string $domain
7374
*
74-
* @throws Exception if the string can not be converted to ASCII using IDN UTS46 algorithm
75+
* @throws InvalidDomain if the string can not be converted to ASCII using IDN UTS46 algorithm
7576
*
7677
* @return string
7778
*/
@@ -87,7 +88,7 @@ private function idnToAscii(string $domain): string
8788
return $output;
8889
}
8990

90-
throw new Exception(sprintf('The host `%s` is invalid : %s', $domain, self::getIdnErrors($arr['errors'])));
91+
throw new InvalidDomain(sprintf('The host `%s` is invalid : %s', $domain, self::getIdnErrors($arr['errors'])));
9192
}
9293

9394
/**
@@ -97,7 +98,7 @@ private function idnToAscii(string $domain): string
9798
*
9899
* @param string $domain
99100
*
100-
* @throws Exception if the string can not be converted to UNICODE using IDN UTS46 algorithm
101+
* @throws InvalidDomain if the string can not be converted to UNICODE using IDN UTS46 algorithm
101102
*
102103
* @return string
103104
*/
@@ -108,7 +109,7 @@ private function idnToUnicode(string $domain): string
108109
return $output;
109110
}
110111

111-
throw new Exception(sprintf('The host `%s` is invalid : %s', $domain, self::getIdnErrors($arr['errors'])));
112+
throw new InvalidDomain(sprintf('The host `%s` is invalid : %s', $domain, self::getIdnErrors($arr['errors'])));
112113
}
113114

114115
/**
@@ -121,7 +122,7 @@ private function idnToUnicode(string $domain): string
121122
*
122123
* @param mixed $domain
123124
*
124-
* @throws Exception If the domain is invalid
125+
* @throws InvalidDomain If the domain is invalid
125126
*
126127
* @return string[]
127128
*/
@@ -145,7 +146,7 @@ private function setLabels($domain = null): array
145146

146147
$domain = (string) $domain;
147148
if (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
148-
throw new Exception(sprintf('The domain `%s` is invalid: this is an IPv4 host', $domain));
149+
throw new InvalidDomain(sprintf('The domain `%s` is invalid: this is an IPv4 host', $domain));
149150
}
150151

151152
$formatted_domain = rawurldecode($domain);
@@ -165,13 +166,13 @@ private function setLabels($domain = null): array
165166
// a domain name can not contains URI delimiters or space
166167
static $gen_delims = '/[:\/?#\[\]@ ]/';
167168
if (preg_match($gen_delims, $formatted_domain)) {
168-
throw new Exception(sprintf('The domain `%s` is invalid: it contains invalid characters', $domain));
169+
throw new InvalidDomain(sprintf('The domain `%s` is invalid: it contains invalid characters', $domain));
169170
}
170171

171172
// if the domain name does not contains UTF-8 chars then it is malformed
172173
static $pattern = '/[^\x20-\x7f]/';
173174
if (!preg_match($pattern, $formatted_domain)) {
174-
throw new Exception(sprintf('The domain `%s` is invalid: the labels are malformed', $domain));
175+
throw new InvalidDomain(sprintf('The domain `%s` is invalid: the labels are malformed', $domain));
175176
}
176177

177178
//if a domain name contains UTF-8 chars it must be convertible using IDNA UTS46
@@ -180,6 +181,6 @@ private function setLabels($domain = null): array
180181
return array_reverse(explode('.', $this->idnToUnicode($ascii_domain)));
181182
}
182183

183-
throw new Exception(sprintf('The domain `%s` is invalid : %s', $domain, self::getIdnErrors($arr['errors'])));
184+
throw new InvalidDomain(sprintf('The domain `%s` is invalid : %s', $domain, self::getIdnErrors($arr['errors'])));
184185
}
185186
}

src/Manager.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Pdp;
1717

18+
use Pdp\Exception\CouldNotLoadRules;
1819
use Psr\SimpleCache\CacheInterface;
1920

2021
/**
@@ -57,8 +58,7 @@ public function __construct(CacheInterface $cache, HttpClient $http)
5758
*
5859
* @param string $source_url the Public Suffix List URL
5960
*
60-
* @throws Exception If the PSL can not be fetch from the source URL and its cache backend
61-
* @throws Exception If the PSL cache copy is corrupted
61+
* @throws CouldNotLoadRules If the PSL rules can not be loaded
6262
*
6363
* @return Rules
6464
*/
@@ -68,15 +68,15 @@ public function getRules(string $source_url = self::PSL_URL): Rules
6868
$cacheRules = $this->cache->get($cacheKey);
6969

7070
if (null === $cacheRules && !$this->refreshRules($source_url)) {
71-
throw new Exception(sprintf('Unable to load the public suffix list rules for %s', $source_url));
71+
throw new CouldNotLoadRules(sprintf('Unable to load the public suffix list rules for %s', $source_url));
7272
}
7373

7474
$rules = json_decode($cacheRules ?? $this->cache->get($cacheKey), true);
7575
if (JSON_ERROR_NONE === json_last_error()) {
7676
return new Rules($rules);
7777
}
7878

79-
throw new Exception('The public suffix list cache is corrupted: '.json_last_error_msg(), json_last_error());
79+
throw new CouldNotLoadRules('The public suffix list cache is corrupted: '.json_last_error_msg(), json_last_error());
8080
}
8181

8282
/**

0 commit comments

Comments
 (0)