Skip to content

Commit 0d9298a

Browse files
committed
Improve exception messages
1 parent f5c8753 commit 0d9298a

File tree

8 files changed

+33
-33
lines changed

8 files changed

+33
-33
lines changed

src/Dictionary.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private static function filterForbiddenState(InnerList|Item $member): InnerList|
267267
{
268268
foreach ($member->parameters as $offset => $item) {
269269
if ($item->parameters->hasMembers()) {
270-
throw new ForbiddenStateError('Parameter member `"'.$offset.'"` is in invalid state; Parameters instances can only contain bare items.');
270+
throw new ForbiddenStateError('Parameter member "'.$offset.'" is in invalid state; Parameters instances can only contain bare items.');
271271
}
272272
}
273273

src/InnerList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private static function filterForbiddenState(Item $member): Item
5959
{
6060
foreach ($member->parameters as $offset => $item) {
6161
if ($item->parameters->hasMembers()) {
62-
throw new ForbiddenStateError('Parameter member `"'.$offset.'"` is in invalid state; Parameters instances can only contain bare items.');
62+
throw new ForbiddenStateError('Parameter member "'.$offset.'" is in invalid state; Parameters instances can only contain bare items.');
6363
}
6464
}
6565

src/InvalidOffset.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ private function __construct(string $message)
1616
public static function dueToIndexNotFound(string|int $index): self
1717
{
1818
if (is_string($index)) {
19-
return new self('The member index can not be the string `'.$index.'`.');
19+
return new self('The member index can not be the string "'.$index.'".');
2020
}
2121

22-
return new self('No member exists with the index `'.$index.'`.');
22+
return new self('No member exists with the index "'.$index.'".');
2323
}
2424

2525
public static function dueToKeyNotFound(string|int $key): self
2626
{
2727
if (is_int($key)) {
28-
return new self('The member key can not be the integer`'.$key.'`.');
28+
return new self('The member key can not be the integer "'.$key.'".');
2929
}
3030

31-
return new self('No member exists with the key `'.$key.'`.');
31+
return new self('No member exists with the key "'.$key.'".');
3232
}
3333
}

src/Item.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private static function filterDecimal(float $value): float
117117
private static function filterString(string $value): string
118118
{
119119
if (1 === preg_match('/[^\x20-\x7E]/i', $value)) {
120-
throw new SyntaxError('The string `'.$value.'` contains invalid characters.');
120+
throw new SyntaxError('The string "'.$value.'" contains invalid characters.');
121121
}
122122

123123
return $value;
@@ -149,13 +149,13 @@ public static function fromHttpValue(string $httpValue): self
149149

150150
[$value, $parameters] = match (true) {
151151
1 === preg_match("/[\r\t\n]|[^\x20-\x7E]/", $itemString),
152-
'' === $itemString => throw new SyntaxError("The HTTP textual representation `$httpValue` for an item contains invalid characters."),
152+
'' === $itemString => throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for an item contains invalid characters.'),
153153
'"' === $itemString[0] => self::parseString($itemString),
154154
':' === $itemString[0] => self::parseBytesSequence($itemString),
155155
'?' === $itemString[0] => self::parseBoolean($itemString),
156156
1 === preg_match('/^(-?\d)/', $itemString) => self::parseNumber($itemString),
157157
1 === preg_match('/^([a-z*])/i', $itemString) => self::parseToken($itemString),
158-
default => throw new SyntaxError("The HTTP textual representation `$httpValue` for an item is unknown or unsupported."),
158+
default => throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for an item is unknown or unsupported.'),
159159
};
160160

161161
return new self($value, Parameters::fromHttpValue($parameters));
@@ -174,7 +174,7 @@ private static function parseToken(string $string): array
174174
}
175175

176176
if (1 !== preg_match('/'.$regexp.'/i', $string, $found)) {
177-
throw new SyntaxError("The HTTP textual representation `$string` for a Token contains invalid characters.");
177+
throw new SyntaxError("The HTTP textual representation \"$string\" for a Token contains invalid characters.");
178178
}
179179

180180
return [
@@ -191,7 +191,7 @@ private static function parseToken(string $string): array
191191
private static function parseBoolean(string $string): array
192192
{
193193
if (1 !== preg_match('/^\?[01]/', $string)) {
194-
throw new SyntaxError("The HTTP textual representation `$string` for a boolean contains invalid characters.");
194+
throw new SyntaxError("The HTTP textual representation \"$string\" for a boolean contains invalid characters.");
195195
}
196196

197197
return [$string[1] === '1', substr($string, 2)];
@@ -205,7 +205,7 @@ private static function parseBoolean(string $string): array
205205
private static function parseBytesSequence(string $string): array
206206
{
207207
if (1 !== preg_match('/^:(?<bytes>[a-z\d+\/=]*):/i', $string, $matches)) {
208-
throw new SyntaxError("The HTTP textual representation `$string` for a byte sequence contains invalid characters.");
208+
throw new SyntaxError("The HTTP textual representation \"$string\" for a byte sequence contains invalid characters.");
209209
}
210210

211211
return [ByteSequence::fromEncoded($matches['bytes']), substr($string, strlen($matches[0]))];
@@ -224,13 +224,13 @@ private static function parseNumber(string $string): array
224224
}
225225

226226
if (1 !== preg_match('/'.$regexp.'/', $string, $found)) {
227-
throw new SyntaxError("The HTTP textual representation `$string` for a number contains invalid characters.");
227+
throw new SyntaxError("The HTTP textual representation \"$string\" for a number contains invalid characters.");
228228
}
229229

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

236236
return [$number, substr($string, strlen($found['number']))];
@@ -261,19 +261,19 @@ private static function parseString(string $string): array
261261
}
262262

263263
if ($string === '') {
264-
throw new SyntaxError("The HTTP textual representation `$originalString` for a string contains an invalid end string.");
264+
throw new SyntaxError("The HTTP textual representation \"$originalString\" for a string contains an invalid end string.");
265265
}
266266

267267
$char = $string[0];
268268
$string = substr($string, 1);
269269
if (!in_array($char, ['"', '\\'], true)) {
270-
throw new SyntaxError("The HTTP textual representation `$originalString` for a string contains invalid characters.");
270+
throw new SyntaxError("The HTTP textual representation \"$originalString\" for a string contains invalid characters.");
271271
}
272272

273273
$returnValue .= $char;
274274
}
275275

276-
throw new SyntaxError("The HTTP textual representation `$originalString` for a string contains an invalid end string.");
276+
throw new SyntaxError("The HTTP textual representation \"$originalString\" for a string contains an invalid end string.");
277277
}
278278

279279
/**

src/MapKey.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function fromString(string $httpValue): self
2222
{
2323
$instance = self::fromStringBeginning($httpValue);
2424
if ($instance->value !== $httpValue) {
25-
throw new SyntaxError("No valid http value key could be extracted from `$httpValue`.");
25+
throw new SyntaxError("No valid http value key could be extracted from \"$httpValue\".");
2626
}
2727

2828
return $instance;
@@ -34,7 +34,7 @@ public static function fromString(string $httpValue): self
3434
public static function fromStringBeginning(string $httpValue): self
3535
{
3636
if (1 !== preg_match('/^(?<key>[a-z*][a-z\d.*_-]*)/', $httpValue, $found)) {
37-
throw new SyntaxError("No valid http value key could be extracted from `$httpValue`.");
37+
throw new SyntaxError("No valid http value key could be extracted from \"$httpValue\".");
3838
}
3939

4040
return new self($found['key']);

src/OrderedList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private static function filterForbiddenState(InnerList|Item $member): InnerList|
6363
{
6464
foreach ($member->parameters as $offset => $item) {
6565
if ($item->parameters->hasMembers()) {
66-
throw new ForbiddenStateError('Parameter member `"'.$offset.'"` is in invalid state; Parameters instances can only contain bare items.');
66+
throw new ForbiddenStateError('Parameter member "'.$offset.'" is in invalid state; Parameters instances can only contain bare items.');
6767
}
6868
}
6969

src/Parameters.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private static function filterMember(Item $item, string|int $offset = null): Ite
4646

4747
$message = 'Parameters instances can only contain bare items.';
4848
if (null !== $offset) {
49-
$message = 'Parameter member `"'.$offset.'"` is in invalid state; '.$message;
49+
$message = 'Parameter member "'.$offset.'" is in invalid state; '.$message;
5050
}
5151

5252
throw new ForbiddenStateError($message);
@@ -121,7 +121,7 @@ public static function fromHttpValue(string $httpValue): self
121121
foreach (explode(';', $httpValue) as $pair) {
122122
[$key, $value] = explode('=', $pair, 2) + [1 => '?1'];
123123
if (rtrim($key) !== $key || ltrim($value) !== $value) {
124-
throw new SyntaxError("The HTTP textual representation `$pair` for a parameter pair contains invalid characters.");
124+
throw new SyntaxError("The HTTP textual representation \"$pair\" for a parameter pair contains invalid characters.");
125125
}
126126

127127
$key = trim($key);
@@ -139,7 +139,7 @@ public static function fromHttpValue(string $httpValue): self
139139
public function toHttpValue(): string
140140
{
141141
$formatter = fn (Item $member, string $offset): string => match (true) {
142-
$member->parameters->hasMembers() => throw new ForbiddenStateError('Parameter member `"'.$offset.'"` is in invalid state; Parameters instances can only contain bare items.'),
142+
$member->parameters->hasMembers() => throw new ForbiddenStateError('Parameter member "'.$offset.'" is in invalid state; Parameters instances can only contain bare items.'),
143143
true === $member->value() => ';'.$offset,
144144
default => ';'.$offset.'='.$member->toHttpValue(),
145145
};

src/Parser.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ public static function parseInnerList(string $httpValue): array
8686
{
8787
$remainder = ltrim($httpValue, ' ');
8888
if ('(' !== $remainder[0]) {
89-
throw new SyntaxError("The HTTP textual representation `$httpValue` for a inner list is missing a parenthesis.");
89+
throw new SyntaxError("The HTTP textual representation \"$httpValue\" for a inner list is missing a parenthesis.");
9090
}
9191

9292
[$list, $offset] = self::parseInnerListValue($remainder);
9393
$remainder = self::removeOptionalWhiteSpaces(substr($remainder, $offset));
9494
if ('' !== $remainder) {
95-
throw new SyntaxError("The HTTP textual representation `$httpValue` for a inner list contains invalid data.");
95+
throw new SyntaxError("The HTTP textual representation \"$httpValue\" for a inner list contains invalid data.");
9696
}
9797

9898
return $list;
@@ -190,11 +190,11 @@ private static function parseInnerListValue(string $httpValue): array
190190

191191
$list[] = Item::from($value, $parameters);
192192
if ('' !== $remainder && !in_array($remainder[0], [' ', ')'], true)) {
193-
throw new SyntaxError("The HTTP textual representation `$remainder` for a inner list is using invalid characters.");
193+
throw new SyntaxError("The HTTP textual representation \"$remainder\" for a inner list is using invalid characters.");
194194
}
195195
}
196196

197-
throw new SyntaxError("Unexpected end of line for The HTTP textual representation `$remainder` for a inner list.");
197+
throw new SyntaxError("Unexpected end of line for The HTTP textual representation \"$remainder\" for a inner list.");
198198
}
199199

200200
/**
@@ -255,7 +255,7 @@ private static function parseParameters(string $httpValue): array
255255
private static function parseBoolean(string $httpValue): array
256256
{
257257
if (1 !== preg_match('/^\?[01]/', $httpValue)) {
258-
throw new SyntaxError("Invalid character in the HTTP textual representation of a boolean value `$httpValue`.");
258+
throw new SyntaxError("Invalid character in the HTTP textual representation of a boolean value \"$httpValue\".");
259259
}
260260

261261
return ['1' === $httpValue[1], 2];
@@ -275,7 +275,7 @@ private static function parseNumber(string $httpValue): array
275275
return match (true) {
276276
1 === preg_match('/^-?\d{1,12}\.\d{1,3}$/', $found['number']) => [(float) $found['number'], strlen($found['number'])],
277277
1 === preg_match('/^-?\d{1,15}$/', $found['number']) => [(int) $found['number'], strlen($found['number'])],
278-
default => throw new SyntaxError("The number format in the HTTP textual representation `$httpValue` contains too much digit."),
278+
default => throw new SyntaxError("The number format in the HTTP textual representation \"$httpValue\" contains too much digit."),
279279
};
280280
}
281281

@@ -301,7 +301,7 @@ private static function parseString(string $httpValue): array
301301
}
302302

303303
if (1 === preg_match("/[^\x20-\x7E]/", $char)) {
304-
throw new SyntaxError("Invalid character in the HTTP textual representation of a string `$httpValue`.");
304+
throw new SyntaxError("Invalid character in the HTTP textual representation of a string \"$httpValue\".");
305305
}
306306

307307
$httpValue = substr($httpValue, 1);
@@ -314,13 +314,13 @@ private static function parseString(string $httpValue): array
314314
$offset += 1;
315315
$httpValue = substr($httpValue, 1);
316316
if (!in_array($char, ['"', '\\'], true)) {
317-
throw new SyntaxError("Invalid characters in the HTTP textual representation of a string `$httpValue`.");
317+
throw new SyntaxError("Invalid characters in the HTTP textual representation of a string \"$httpValue\".");
318318
}
319319

320320
$output .= $char;
321321
}
322322

323-
throw new SyntaxError("Invalid end of string in the HTTP textual representation of a string `$httpValue`.");
323+
throw new SyntaxError("Invalid end of string in the HTTP textual representation of a string \"$httpValue\".");
324324
}
325325

326326
/**
@@ -347,7 +347,7 @@ private static function parseToken(string $httpValue): array
347347
private static function parseByteSequence(string $httpValue): array
348348
{
349349
if (1 !== preg_match('/^(?<sequence>:(?<byte>[a-z\d+\/=]*):)/i', $httpValue, $matches)) {
350-
throw new SyntaxError("Invalid characters in the HTTP textual representation of a Byte Sequence `$httpValue`.");
350+
throw new SyntaxError("Invalid characters in the HTTP textual representation of a Byte Sequence \"$httpValue\".");
351351
}
352352

353353
return [ByteSequence::fromEncoded($matches['byte']), strlen($matches['sequence'])];

0 commit comments

Comments
 (0)