From 25759ffc5fe532a239b1487ca6b025140d2e144f Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 27 Sep 2018 17:28:56 +1200 Subject: [PATCH] NEW Show file path on PHP parser exceptions --- src/Core/Manifest/ClassManifest.php | 7 ++-- .../Manifest/ClassManifestErrorHandler.php | 33 +++++++++++++++++++ .../ClassManifestErrorHandlerTest.php | 20 +++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/Core/Manifest/ClassManifestErrorHandler.php create mode 100644 src/Core/Manifest/ClassManifestErrorHandlerTest.php diff --git a/src/Core/Manifest/ClassManifest.php b/src/Core/Manifest/ClassManifest.php index 9b64c8c4c..23bdc4d52 100644 --- a/src/Core/Manifest/ClassManifest.php +++ b/src/Core/Manifest/ClassManifest.php @@ -8,6 +8,7 @@ use PhpParser\NodeTraverser; use PhpParser\NodeVisitor\NameResolver; use PhpParser\Parser; use PhpParser\ParserFactory; +use PhpParser\ErrorHandler\ErrorHandler; use Psr\SimpleCache\CacheInterface; use SilverStripe\Core\Cache\CacheFactory; use SilverStripe\Dev\TestOnly; @@ -490,11 +491,13 @@ class ClassManifest $changed = true; // Build from php file parser $fileContents = ClassContentRemover::remove_class_content($pathname); + // Not injectable, error handling is an implementation detail. + $errorHandler = new ClassManifestErrorHandler($pathname); try { - $stmts = $this->getParser()->parse($fileContents); + $stmts = $this->getParser()->parse($fileContents, $errorHandler); } catch (Error $e) { // if our mangled contents breaks, try again with the proper file contents - $stmts = $this->getParser()->parse(file_get_contents($pathname)); + $stmts = $this->getParser()->parse(file_get_contents($pathname), $errorHandler); } $this->getTraverser()->traverse($stmts); diff --git a/src/Core/Manifest/ClassManifestErrorHandler.php b/src/Core/Manifest/ClassManifestErrorHandler.php new file mode 100644 index 000000000..b5faf50da --- /dev/null +++ b/src/Core/Manifest/ClassManifestErrorHandler.php @@ -0,0 +1,33 @@ +pathname = $pathname; + } + + public function handleError(Error $error) + { + $newMessage = sprintf('%s in %s', $error->getRawMessage(), $this->pathname); + $error->setRawMessage($newMessage); + throw $error; + } +} diff --git a/src/Core/Manifest/ClassManifestErrorHandlerTest.php b/src/Core/Manifest/ClassManifestErrorHandlerTest.php new file mode 100644 index 000000000..0a9ec22a5 --- /dev/null +++ b/src/Core/Manifest/ClassManifestErrorHandlerTest.php @@ -0,0 +1,20 @@ +handleError($e); + } +}