Skip to content

Commit 88fb076

Browse files
committed
Improve exception messages
1 parent 77257b1 commit 88fb076

File tree

5 files changed

+15
-15
lines changed

5 files changed

+15
-15
lines changed

src/ByteSequence.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ public static function fromEncoded(Stringable|string $encodedValue): self
2323
{
2424
$encodedValue = (string) $encodedValue;
2525
if (1 !== preg_match('/^[a-z\d+\/=]*$/i', $encodedValue)) {
26-
throw new SyntaxError('Invalid character in byte sequence');
26+
throw new SyntaxError('Invalid character in byte sequence.');
2727
}
2828

2929
$decoded = base64_decode($encodedValue, true);
3030
if (false === $decoded) {
31-
throw new SyntaxError('Unable to base64 decode the byte sequence');
31+
throw new SyntaxError('Unable to base64 decode the byte sequence.');
3232
}
3333

3434
return new self($decoded);

src/Item.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static function fromToken(string|Stringable $value, iterable $parameters
9898
private static function filterDecimal(float $value): float
9999
{
100100
if (abs(floor($value)) > 999_999_999_999) {
101-
throw new SyntaxError('Integer portion of decimals is limited to 12 digits');
101+
throw new SyntaxError('Integer portion of decimals is limited to 12 digits.');
102102
}
103103

104104
return $value;
@@ -126,7 +126,7 @@ private static function filterString(string $value): string
126126
private static function filterInteger(int $value): int
127127
{
128128
if ($value > 999_999_999_999_999 || $value < -999_999_999_999_999) {
129-
throw new SyntaxError('Integers are limited to 15 digits');
129+
throw new SyntaxError('Integers are limited to 15 digits.');
130130
}
131131

132132
return $value;
@@ -186,7 +186,7 @@ private static function parseToken(string $string): array
186186
private static function parseBoolean(string $string): array
187187
{
188188
if (1 !== preg_match('/^\?[01]/', $string)) {
189-
throw new SyntaxError("The HTTP textual representation \"$string\" for a boolean contains invalid characters.");
189+
throw new SyntaxError("The HTTP textual representation \"$string\" for a Boolean contains invalid characters.");
190190
}
191191

192192
return [$string[1] === '1', substr($string, 2)];
@@ -200,7 +200,7 @@ private static function parseBoolean(string $string): array
200200
private static function parseBytesSequence(string $string): array
201201
{
202202
if (1 !== preg_match('/^:(?<bytes>[a-z\d+\/=]*):/i', $string, $matches)) {
203-
throw new SyntaxError("The HTTP textual representation \"$string\" for a byte sequence contains invalid characters.");
203+
throw new SyntaxError("The HTTP textual representation \"$string\" for a Byte sequence contains invalid characters.");
204204
}
205205

206206
return [ByteSequence::fromEncoded($matches['bytes']), substr($string, strlen($matches[0]))];
@@ -219,13 +219,13 @@ private static function parseNumber(string $string): array
219219
}
220220

221221
if (1 !== preg_match('/'.$regexp.'/', $string, $found)) {
222-
throw new SyntaxError("The HTTP textual representation \"$string\" for a number contains invalid characters.");
222+
throw new SyntaxError("The HTTP textual representation \"$string\" for a Number contains invalid characters.");
223223
}
224224

225225
$number = match (true) {
226226
1 === preg_match('/^-?\d{1,12}\.\d{1,3}$/', $found['number']) => (float) $found['number'],
227227
1 === preg_match('/^-?\d{1,15}$/', $found['number']) => (int) $found['number'],
228-
default => throw new SyntaxError("The HTTP textual representation \"$string\" for a number contain too many digits."),
228+
default => throw new SyntaxError("The HTTP textual representation \"$string\" for a Number contain too many digits."),
229229
};
230230

231231
return [$number, substr($string, strlen($found['number']))];
@@ -256,19 +256,19 @@ private static function parseString(string $string): array
256256
}
257257

258258
if ($string === '') {
259-
throw new SyntaxError("The HTTP textual representation \"$originalString\" for a string contains an invalid end string.");
259+
throw new SyntaxError("The HTTP textual representation \"$originalString\" for a String contains an invalid end string.");
260260
}
261261

262262
$char = $string[0];
263263
$string = substr($string, 1);
264264
if (!in_array($char, ['"', '\\'], true)) {
265-
throw new SyntaxError("The HTTP textual representation \"$originalString\" for a string contains invalid characters.");
265+
throw new SyntaxError("The HTTP textual representation \"$originalString\" for a String contains invalid characters.");
266266
}
267267

268268
$returnValue .= $char;
269269
}
270270

271-
throw new SyntaxError("The HTTP textual representation \"$originalString\" for a string contains an invalid end string.");
271+
throw new SyntaxError("The HTTP textual representation \"$originalString\" for a String contains an invalid end string.");
272272
}
273273

274274
/**

src/Parameters.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public static function fromHttpValue(Stringable|string $httpValue): self
115115
foreach (explode(';', $httpValue) as $pair) {
116116
[$key, $value] = explode('=', $pair, 2) + [1 => '?1'];
117117
if (rtrim($key) !== $key || ltrim($value) !== $value) {
118-
throw new SyntaxError('The HTTP textual representation "'.$pair.'" for a parameter pair contains invalid characters.');
118+
throw new SyntaxError('The HTTP textual representation "'.$pair.'" for a Parameter pair contains invalid characters.');
119119
}
120120

121121
$key = trim($key);
@@ -382,7 +382,7 @@ public function offsetUnset($offset): void
382382
public function offsetSet(mixed $offset, mixed $value): void
383383
{
384384
if (!is_string($offset)) {
385-
throw new SyntaxError('The offset for a parameter member is expected to be a string; "'.gettype($offset).'" given.');
385+
throw new SyntaxError('The offset for a Parameter member is expected to be a string; "'.gettype($offset).'" given.');
386386
}
387387

388388
$this->set($offset, $value);

src/TestRecordCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private function __construct()
3030
public function add(TestRecord $test): void
3131
{
3232
if (isset($this->elements[$test->name])) {
33-
throw new RuntimeException('Already existing test name `'.$test->name.'`');
33+
throw new RuntimeException('Already existing test name `'.$test->name.'`.');
3434
}
3535

3636
$this->elements[$test->name] = $test;

src/Token.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class Token
1212
private function __construct(public readonly string $value)
1313
{
1414
if (1 !== preg_match("/^([a-z*][a-z\d:\/!#\$%&'*+\-.^_`|~]*)$/i", $this->value)) {
15-
throw new SyntaxError('Invalid characters in token');
15+
throw new SyntaxError('Invalid characters in token.');
1616
}
1717
}
1818

0 commit comments

Comments
 (0)