Skip to content

Commit

Permalink
Include the constant name on PcreException message as preffix and fix…
Browse files Browse the repository at this point in the history
… test
  • Loading branch information
jfoulquie-tnw committed Apr 13, 2022
1 parent 34ba05a commit 16883cd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion generator/tests/SpecialCasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function testPregReplace()
require_once __DIR__.'/../../lib/Exceptions/PcreException.php';

$this->expectException(PcreException::class);
$this->expectExceptionMessage('PREG_BAD_UTF8_ERROR: Invalid UTF8 character');
$this->expectExceptionMessage('PREG_BAD_UTF8_ERROR: Malformed UTF-8 characters, possibly incorrectly encoded');
preg_replace("/([\s,]+)/u", "foo", "\xc3\x28");
}
}
11 changes: 10 additions & 1 deletion lib/Exceptions/PcreException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ class PcreException extends \Exception implements SafeExceptionInterface
{
public static function createFromPhpError(): self
{
return new self(\preg_last_error_msg(), \preg_last_error());
$errorConstantMap = [
PREG_INTERNAL_ERROR => 'PREG_INTERNAL_ERROR',
PREG_BACKTRACK_LIMIT_ERROR => 'PREG_BACKTRACK_LIMIT_ERROR',
PREG_RECURSION_LIMIT_ERROR => 'PREG_RECURSION_LIMIT_ERROR',
PREG_BAD_UTF8_ERROR => 'PREG_BAD_UTF8_ERROR',
PREG_BAD_UTF8_OFFSET_ERROR => 'PREG_BAD_UTF8_OFFSET_ERROR',
PREG_JIT_STACKLIMIT_ERROR => 'PREG_JIT_STACKLIMIT_ERROR',
];

return new self(($errorConstantMap[\preg_last_error()] ?? 'Unknown Error') . ': ' . \preg_last_error_msg(), \preg_last_error());
}
}

0 comments on commit 16883cd

Please sign in to comment.