improve exceptions when parsing malformed files

This commit is contained in:
Christian Flothmann 2015-09-08 21:57:35 +02:00
parent 6430c828f6
commit 3849cd80b9
3 changed files with 20 additions and 3 deletions

View File

@ -19,6 +19,7 @@ use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException; use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser as YamlParser; use Symfony\Component\Yaml\Parser as YamlParser;
/** /**
@ -286,7 +287,13 @@ class YamlFileLoader extends FileLoader
$this->yamlParser = new YamlParser(); $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);
} }
/** /**

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Routing\Loader;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Symfony\Component\Config\Resource\FileResource; use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser as YamlParser; use Symfony\Component\Yaml\Parser as YamlParser;
use Symfony\Component\Config\Loader\FileLoader; use Symfony\Component\Config\Loader\FileLoader;
@ -60,7 +61,11 @@ class YamlFileLoader extends FileLoader
$this->yamlParser = new YamlParser(); $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 = new RouteCollection();
$collection->addResource(new FileResource($path)); $collection->addResource(new FileResource($path));

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Mapping\Loader; namespace Symfony\Component\Validator\Mapping\Loader;
use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser as YamlParser; use Symfony\Component\Yaml\Parser as YamlParser;
/** /**
@ -117,7 +118,11 @@ class YamlFileLoader extends FileLoader
*/ */
private function parseFile($path) 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 // empty file
if (null === $classes) { if (null === $classes) {