8000 improve exceptions when parsing malformed files · Seldaek/symfony@3849cd8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3849cd8

Browse files
committed
improve exceptions when parsing malformed files
1 parent 6430c82 commit 3849cd8

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2020
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2121
use Symfony\Component\Config\Resource\FileResource;
22+
use Symfony\Component\Yaml\Exception\ParseException;
2223
use Symfony\Component\Yaml\Parser as YamlParser;
2324

2425
/**
@@ -286,7 +287,13 @@ protected function loadFile($file)
286287
$this->yamlParser = new YamlParser();
287288
}
288289

289-
return $this->validate($this->yamlParser->parse(file_get_contents($file)), $file);
290+
try {
291+
$configuration = $this->yamlParser->parse(file_get_contents($file));
292+
} catch (ParseException $e) {
293+
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
294+
}
295+
296+
return $this->validate($configuration, $file);
290297
}
291298

292299
/**

src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Routing\RouteCollection;
1515
use Symfony\Component\Routing\Route;
1616
use Symfony\Component\Config\Resource\FileResource;
17+
use Symfony\Component\Yaml\Exception\ParseException;
1718
use Symfony\Component\Yaml\Parser as YamlParser;
1819
use Symfony\Component\Config\Loader\FileLoader;
1920

@@ -60,7 +61,11 @@ public function load($file, $type = null)
6061
$this->yamlParser = new YamlParser();
6162
}
6263

63-
$config = $this->yamlParser->parse(file_get_contents($path));
64+
try {
65+
$config = $this->yamlParser->parse(file_get_contents($path));
66+
} catch (ParseException $e) {
67+
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
68+
}
6469

6570
$collection = new RouteCollection();
6671
$collection->addResource(new FileResource($path));

src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Mapping\Loader;
1313

1414
use Symfony\Component\Validator\Mapping\ClassMetadata;
15+
use Symfony\Component\Yaml\Exception\ParseException;
1516
use Symfony\Component\Yaml\Parser as YamlParser;
1617

1718
/**
@@ -117,7 +118,11 @@ protected function parseNodes(array $nodes)
117118
*/
118119
private function parseFile($path)
119120
{
120-
$classes = $this->yamlParser->parse(file_get_contents($path));
121+
try {
122+
$classes = $this->yamlParser->parse(file_get_contents($path));
123+
} catch (ParseException $e) {
124+
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid.', $path), 0, $e);
125+
}
121126

122127
// empty file
123128
if (null === $classes) {

0 commit comments

Comments
 (0)
0