From 3849cd80b9fa68baaed0c0b8da1302443162ecd2 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Tue, 8 Sep 2015 21:57:35 +0200 Subject: [PATCH] improve exceptions when parsing malformed files --- .../DependencyInjection/Loader/YamlFileLoader.php | 9 ++++++++- src/Symfony/Component/Routing/Loader/YamlFileLoader.php | 7 ++++++- .../Validator/Mapping/Loader/YamlFileLoader.php | 7 ++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php index fc1fff203246..28adaa458048 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php @@ -19,6 +19,7 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; use Symfony\Component\DependencyInjection\Exception\RuntimeException; use Symfony\Component\Config\Resource\FileResource; +use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Parser as YamlParser; /** @@ -286,7 +287,13 @@ protected function loadFile($file) $this->yamlParser = new YamlParser(); } - return $this->validate($this->yamlParser->parse(file_get_contents($file)), $file); + try { + $configuration = $this->yamlParser->parse(file_get_contents($file)); + } catch (ParseException $e) { + throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e); + } + + return $this->validate($configuration, $file); } /** diff --git a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php index 3653eaa55078..f5438f256f13 100644 --- a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php @@ -14,6 +14,7 @@ use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; use Symfony\Component\Config\Resource\FileResource; +use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Parser as YamlParser; use Symfony\Component\Config\Loader\FileLoader; @@ -60,7 +61,11 @@ public function load($file, $type = null) $this->yamlParser = new YamlParser(); } - $config = $this->yamlParser->parse(file_get_contents($path)); + try { + $config = $this->yamlParser->parse(file_get_contents($path)); + } catch (ParseException $e) { + throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e); + } $collection = new RouteCollection(); $collection->addResource(new FileResource($path)); diff --git a/src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php index 6075b270baa6..ea4e2641a6fc 100644 --- a/src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Validator\Mapping\Loader; use Symfony\Component\Validator\Mapping\ClassMetadata; +use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Parser as YamlParser; /** @@ -117,7 +118,11 @@ protected function parseNodes(array $nodes) */ private function parseFile($path) { - $classes = $this->yamlParser->parse(file_get_contents($path)); + try { + $classes = $this->yamlParser->parse(file_get_contents($path)); + } catch (ParseException $e) { + throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid.', $path), 0, $e); + } // empty file if (null === $classes) {