Skip to content

Commit 503931f

Browse files
committed
Improve error messages consistency
1 parent b309c94 commit 503931f

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/InnerList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public static function fromHttpValue(string $httpValue): self
3636
$field = trim($httpValue);
3737

3838
if (1 !== preg_match("/^\((?<content>.*)\)(?<parameters>[^,]*)/", $field, $found)) {
39-
throw new SyntaxError("InnerList field `$httpValue` contains invalid characters.");
39+
throw new SyntaxError("The HTTP textual representation `$httpValue` for a inner list contains invalid characters.");
4040
}
4141

4242
if ('' !== $found['parameters'] && !str_starts_with($found['parameters'], ';')) {
43-
throw new SyntaxError("InnerList field `$httpValue` contains invalid characters.");
43+
throw new SyntaxError("The HTTP textual representation `$httpValue` for a inner list contains invalid characters.");
4444
}
4545

4646
/** @var string $content */

src/Item.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ public static function fromHttpValue(string $httpValue): self
6060
[$value, $parameters] = match (true) {
6161
$httpValue === '',
6262
1 === preg_match("/[\r\t\n]/", $httpValue),
63-
1 === preg_match("/[^\x20-\x7E]/", $httpValue) => throw new SyntaxError("Item field `$httpValue` contains invalid characters."),
63+
1 === preg_match("/[^\x20-\x7E]/", $httpValue) => throw new SyntaxError("The HTTP textual representation `$httpValue` for an item contains invalid characters."),
6464
1 === preg_match('/^(-?[0-9])/', $httpValue) => self::parseNumber($httpValue),
6565
$httpValue[0] == '"' => self::parseString($httpValue),
6666
$httpValue[0] == ':' => self::parseBytesSequence($httpValue),
6767
$httpValue[0] == '?' => self::parseBoolean($httpValue),
6868
1 === preg_match('/^([a-z*])/i', $httpValue) => self::parseToken($httpValue),
69-
default => throw new SyntaxError("Item field `$httpValue` is unknown or unsupported."),
69+
default => throw new SyntaxError("The HTTP textual representation `$httpValue` for an item is unknown or unsupported."),
7070
};
7171

7272
return new self($value, Parameters::fromHttpValue($parameters));
@@ -83,7 +83,7 @@ private static function parseToken(string $string): array
8383
}
8484

8585
if (1 !== preg_match('/'.$regexp.'/i', $string, $matches)) {
86-
throw new SyntaxError("The token item `$string` contains invalid characters.");
86+
throw new SyntaxError("The HTTP textual representation `$string` for a token contains invalid characters.");
8787
}
8888

8989
return [
@@ -98,7 +98,7 @@ private static function parseToken(string $string): array
9898
private static function parseBoolean(string $string): array
9999
{
100100
if (1 !== preg_match('/^\?[01]/', $string)) {
101-
throw new SyntaxError('The boolean item `'.$string.'` contains invalid characters.');
101+
throw new SyntaxError("The HTTP textual representation `$string` for a boolean contains invalid characters.");
102102
}
103103

104104
return [$string[1] === '1', substr($string, 2)];
@@ -110,7 +110,7 @@ private static function parseBoolean(string $string): array
110110
private static function parseBytesSequence(string $string): array
111111
{
112112
if (1 !== preg_match('/^:(?<bytes>[a-z0-9+\/=]*):/i', $string, $matches)) {
113-
throw new SyntaxError('The byte sequence item `'.$string.'` contains invalid characters.');
113+
throw new SyntaxError("The HTTP textual representation `$string` for a byte sequence contains invalid characters.");
114114
}
115115

116116
return [ByteSequence::fromEncoded($matches['bytes']), substr($string, strlen($matches[0]))];
@@ -122,13 +122,13 @@ private static function parseBytesSequence(string $string): array
122122
private static function parseNumber(string $string): array
123123
{
124124
if (1 !== preg_match('/^(?<number>-?\d+(?:\.\d+)?)(?:[^\d.]|$)/', $string, $found)) {
125-
throw new SyntaxError('The number item `'.$string.'` contains invalid format.');
125+
throw new SyntaxError("The HTTP textual representation `$string` for a number contains invalid characters.");
126126
}
127127

128128
$number = match (true) {
129129
1 === preg_match('/^-?\d{1,12}\.\d{1,3}$/', $found['number']) => (float) $found['number'],
130130
1 === preg_match('/^-?\d{1,15}$/', $found['number']) => (int) $found['number'],
131-
default => throw new SyntaxError('The number item `'.$string.'` contains too many digits'),
131+
default => throw new SyntaxError("The HTTP textual representation `$string` for a number contain too many digits."),
132132
};
133133

134134
return [$number, substr($string, strlen($found['number']))];
@@ -157,19 +157,19 @@ private static function parseString(string $string): array
157157
}
158158

159159
if ($string === '') {
160-
throw new SyntaxError('Invalid end of string');
160+
throw new SyntaxError("The HTTP textual representation `$originalString` for a string contains an invalid end string.");
161161
}
162162

163163
$char = $string[0];
164164
$string = substr($string, 1);
165165
if (!in_array($char, ['"', '\\'], true)) {
166-
throw new SyntaxError('Invalid escaped character in string `'.$originalString.'`');
166+
throw new SyntaxError("The HTTP textual representation `$originalString` for a string contains invalid characters.");
167167
}
168168

169169
$returnValue .= $char;
170170
}
171171

172-
throw new SyntaxError('Invalid end of string');
172+
throw new SyntaxError("The HTTP textual representation `$originalString` for a string contains an invalid end string.");
173173
}
174174

175175
public function value(): Token|ByteSequence|int|float|string|bool

0 commit comments

Comments
 (0)