Skip to content

Commit

Permalink
improve exceptions when parsing malformed files
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Sep 8, 2015
1 parent 6430c82 commit 3849cd8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Routing/Loader/YamlFileLoader.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
Expand Down
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 3849cd8

Please sign in to comment.