Skip to content

Commit 3a8d742

Browse files
committed
Improve Exception names
1 parent c09056e commit 3a8d742

File tree

7 files changed

+24
-24
lines changed

7 files changed

+24
-24
lines changed

src/Field.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public function __construct(HandledRequestCache|Item|StructuredFieldProvider|Str
5151
*
5252
* @param array<string, string> $server
5353
*
54-
* @throws Exception If the field is not found
54+
* @throws OffsetOutOfBoundsException If the field is not found
5555
*/
5656
public static function fromSapi(array $server = [], string $name = self::SAPI_NAME): self
5757
{
58-
array_key_exists($name, $server) || throw new IndexOutOfBounds('The field `'.$name.'` is not present.');
58+
array_key_exists($name, $server) || throw new OffsetOutOfBoundsException('The field `'.$name.'` is not present.');
5959

6060
return self::fromHttpValue($server[$name]);
6161
}
@@ -248,17 +248,17 @@ public function offsetExists(mixed $offset): bool
248248
*/
249249
public function offsetGet(mixed $offset): HandledRequestCache
250250
{
251-
return $this->nth($offset) ?? throw new IndexOutOfBounds('Offset '.$offset.' does not exist in the request cache.');
251+
return $this->nth($offset) ?? throw new OffsetOutOfBoundsException('Offset '.$offset.' does not exist in the request cache.');
252252
}
253253

254254
public function offsetUnset(mixed $offset): void
255255
{
256-
throw new ReadOnlyAccess(self::class.' instances are read-only and cannot be modified using '.ArrayAccess::class.' methods.');
256+
throw new ReadOnlyAccessException(self::class.' instances are read-only and cannot be modified using '.ArrayAccess::class.' methods.');
257257
}
258258

259259
public function offsetSet(mixed $offset, mixed $value): void
260260
{
261-
throw new ReadOnlyAccess(self::class.' instances are read-only and cannot be modified using '.ArrayAccess::class.' methods.');
261+
throw new ReadOnlyAccessException(self::class.' instances are read-only and cannot be modified using '.ArrayAccess::class.' methods.');
262262
}
263263

264264
/**

src/Forward.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(
2222
public bool $stored = false,
2323
) {
2424
if (null !== $this->statusCode && ($this->statusCode < 100 || $this->statusCode >= 600)) {
25-
throw new InvalidSyntax('The forward statusCode must be a valid HTTP status code when present.');
25+
throw new InvalidSyntaxException('The forward statusCode must be a valid HTTP status code when present.');
2626
}
2727
}
2828

@@ -34,8 +34,8 @@ public static function fromReason(ForwardedReason|Token|string $reason): self
3434
private static function filterReason(ForwardedReason|Token|string $reason): ForwardedReason
3535
{
3636
return match (true) {
37-
is_string($reason) => ForwardedReason::tryFromToken(Token::tryFromString($reason)) ?? throw new InvalidSyntax('`'.$reason.'` is an invalid forward reason.'),
38-
$reason instanceof Token => ForwardedReason::tryFromToken($reason) ?? throw new InvalidSyntax('`'.$reason->toString().'` is an invalid forward reason.'),
37+
is_string($reason) => ForwardedReason::tryFromToken(Token::tryFromString($reason)) ?? throw new InvalidSyntaxException('`'.$reason.'` is an invalid forward reason.'),
38+
$reason instanceof Token => ForwardedReason::tryFromToken($reason) ?? throw new InvalidSyntaxException('`'.$reason->toString().'` is an invalid forward reason.'),
3939
default => $reason,
4040
};
4141
}

src/ForwardedReason.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function toToken(): Token
3333

3434
public static function fromToken(Token|Stringable|string $token): self
3535
{
36-
return self::tryFromToken($token) ?? throw new InvalidSyntax('The token represents an invalid value.');
36+
return self::tryFromToken($token) ?? throw new InvalidSyntaxException('The token represents an invalid value.');
3737
}
3838

3939
public static function tryFromToken(Token|Stringable|string|null $token): ?self

src/HandledRequestCache.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ private function __construct(
3030
public Token|string|null $detail,
3131
) {
3232
match (true) {
33-
!Type::Token->supports($this->servedBy) && !Type::String->supports($this->servedBy) => throw new InvalidSyntax('The handled request cache identifier must be a Token or a string.'),
34-
null !== $this->forward && $this->hit => throw new InvalidSyntax('The handled request cache can not be both a hit and forwarded.'),
35-
null === $this->forward && !$this->hit => throw new InvalidSyntax('The handled request cache must be a hit or forwarded.'),
36-
null !== $this->key && !Type::String->supports($this->key) => throw new InvalidSyntax('The `key` parameter must be a string or null.'),
37-
null !== $this->ttl && !Type::Integer->supports($this->ttl) => throw new InvalidSyntax('The `ttl` parameter must be a integer or null.'),
38-
null !== $this->detail && !Type::String->supports($this->detail) && !Type::Token->supports($this->detail) => throw new InvalidSyntax('The `detail` parameter must be a string or a Token when present.'),
33+
!Type::Token->supports($this->servedBy) && !Type::String->supports($this->servedBy) => throw new InvalidSyntaxException('The handled request cache identifier must be a Token or a string.'),
34+
null !== $this->forward && $this->hit => throw new InvalidSyntaxException('The handled request cache can not be both a hit and forwarded.'),
35+
null === $this->forward && !$this->hit => throw new InvalidSyntaxException('The handled request cache must be a hit or forwarded.'),
36+
null !== $this->key && !Type::String->supports($this->key) => throw new InvalidSyntaxException('The `key` parameter must be a string or null.'),
37+
null !== $this->ttl && !Type::Integer->supports($this->ttl) => throw new InvalidSyntaxException('The `ttl` parameter must be a integer or null.'),
38+
null !== $this->detail && !Type::String->supports($this->detail) && !Type::Token->supports($this->detail) => throw new InvalidSyntaxException('The `detail` parameter must be a string or a Token when present.'),
3939
default => null,
4040
};
4141
}
@@ -54,7 +54,7 @@ public static function serverIdentifierAsString(string $identifier): self
5454
public static function serverIdentifierAsToken(Token|string $identifier): self
5555
{
5656
if (!$identifier instanceof Token) {
57-
$identifier = Token::tryFromString($identifier) ?? throw new InvalidSyntax('The handled request cache identifier must be a valid Token.');
57+
$identifier = Token::tryFromString($identifier) ?? throw new InvalidSyntaxException('The handled request cache identifier must be a valid Token.');
5858
}
5959

6060
return new self($identifier, hit: true, forward: null, ttl:null, key:null, detail: null);
@@ -66,14 +66,14 @@ public static function serverIdentifierAsToken(Token|string $identifier): self
6666
public static function fromHttpValue(StructuredFieldProvider|Item|Stringable|string $item, ?int $statusCode = null): self
6767
{
6868
if (null !== $statusCode && ($statusCode < 100 || $statusCode > 599)) {
69-
throw new InvalidSyntax('The default forward status code must be a valid HTTP status code when present.');
69+
throw new InvalidSyntaxException('The default forward status code must be a valid HTTP status code when present.');
7070
}
7171

7272
if ($item instanceof StructuredFieldProvider) {
7373
$className = $item::class;
7474
$item = $item->toStructuredField();
7575
if (!$item instanceof Item) {
76-
throw new InvalidSyntax('The structured field provider `'.$className.'` must return an '.Item::class.' data type.');
76+
throw new InvalidSyntaxException('The structured field provider `'.$className.'` must return an '.Item::class.' data type.');
7777
}
7878
}
7979

@@ -83,7 +83,7 @@ public static function fromHttpValue(StructuredFieldProvider|Item|Stringable|str
8383

8484
$validation = self::validator()->validate($item);
8585
if ($validation->isFailed()) {
86-
throw new InvalidSyntax('The submitted item is an invalid handled request cache status', previous: $validation->errors->toException());
86+
throw new InvalidSyntaxException('The submitted item is an invalid handled request cache status', previous: $validation->errors->toException());
8787
}
8888

8989
/** @var ValidatedItem $validatedItem */
@@ -187,7 +187,7 @@ public function wasHit(): self
187187
public function wasForwarded(Forward|ForwardedReason|Token|string $forward): self
188188
{
189189
$forward = match (true) {
190-
is_string($forward) => Forward::fromReason(ForwardedReason::tryFrom($forward) ?? throw new InvalidSyntax('The submitted string is not a valid server identifier.')),
190+
is_string($forward) => Forward::fromReason(ForwardedReason::tryFrom($forward) ?? throw new InvalidSyntaxException('The submitted string is not a valid server identifier.')),
191191
$forward instanceof Token => Forward::fromReason(ForwardedReason::fromToken($forward)),
192192
$forward instanceof ForwardedReason => Forward::fromReason($forward),
193193
default => $forward,
@@ -240,7 +240,7 @@ public function withDetailAsString(?string $detail): self
240240
public function withDetailAsToken(Token|string|null $detail): self
241241
{
242242
if (is_string($detail)) {
243-
$detail = Token::tryFromString($detail) ?? throw new InvalidSyntax('The handled request cache detail must be a valid Token.');
243+
$detail = Token::tryFromString($detail) ?? throw new InvalidSyntaxException('The handled request cache detail must be a valid Token.');
244244
}
245245

246246
return match (true) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
namespace Bakame\Http\CacheStatus;
66

7-
class IndexOutOfBounds extends Exception
7+
class InvalidSyntaxException extends Exception
88
{
99
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
namespace Bakame\Http\CacheStatus;
66

7-
class ReadOnlyAccess extends Exception
7+
class OffsetOutOfBoundsException extends Exception
88
{
99
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
namespace Bakame\Http\CacheStatus;
66

7-
class InvalidSyntax extends Exception
7+
class ReadOnlyAccessException extends Exception
88
{
99
}

0 commit comments

Comments
 (0)