Skip to content

Commit 16883cd

Browse files
committed
Include the constant name on PcreException message as preffix and fix test
1 parent 34ba05a commit 16883cd

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

generator/tests/SpecialCasesTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function testPregReplace()
1515
require_once __DIR__.'/../../lib/Exceptions/PcreException.php';
1616

1717
$this->expectException(PcreException::class);
18-
$this->expectExceptionMessage('PREG_BAD_UTF8_ERROR: Invalid UTF8 character');
18+
$this->expectExceptionMessage('PREG_BAD_UTF8_ERROR: Malformed UTF-8 characters, possibly incorrectly encoded');
1919
preg_replace("/([\s,]+)/u", "foo", "\xc3\x28");
2020
}
2121
}

lib/Exceptions/PcreException.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ class PcreException extends \Exception implements SafeExceptionInterface
77
{
88
public static function createFromPhpError(): self
99
{
10-
return new self(\preg_last_error_msg(), \preg_last_error());
10+
$errorConstantMap = [
11+
PREG_INTERNAL_ERROR => 'PREG_INTERNAL_ERROR',
12+
PREG_BACKTRACK_LIMIT_ERROR => 'PREG_BACKTRACK_LIMIT_ERROR',
13+
PREG_RECURSION_LIMIT_ERROR => 'PREG_RECURSION_LIMIT_ERROR',
14+
PREG_BAD_UTF8_ERROR => 'PREG_BAD_UTF8_ERROR',
15+
PREG_BAD_UTF8_OFFSET_ERROR => 'PREG_BAD_UTF8_OFFSET_ERROR',
16+
PREG_JIT_STACKLIMIT_ERROR => 'PREG_JIT_STACKLIMIT_ERROR',
17+
];
18+
19+
return new self(($errorConstantMap[\preg_last_error()] ?? 'Unknown Error') . ': ' . \preg_last_error_msg(), \preg_last_error());
1120
}
1221
}

0 commit comments

Comments
 (0)