fix: YAML parser error are more detailed (#34)

Closes #33
This commit is contained in:
Julien BIANCHI 2020-07-23 20:55:49 +02:00 committed by GitHub
parent c025ca9266
commit 6ab4ce942b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 708 additions and 336 deletions

1017
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,8 +0,0 @@
{
"version": 2,
"services": {
"foo": {
"image": "bar"
}
}
}

View file

@ -0,0 +1,4 @@
version
services:
foo:
image: bar

View file

@ -19,7 +19,7 @@ describe('Reading configuration', function () {
it('should report if YAML is invalid', function () {
expect(function () {
readConfiguration(__DIR__.'/fixtures/read-configuration/invalid.json');
readConfiguration(__DIR__.'/fixtures/read-configuration/invalid.yml');
})
->toThrow(new InvalidArgumentException());
});

View file

@ -5,6 +5,7 @@ namespace PMSIpilot\DockerComposeViz;
use Fhaculty\Graph\Edge;
use Fhaculty\Graph\Graph;
use Fhaculty\Graph\Vertex;
use InvalidArgumentException;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
@ -15,10 +16,6 @@ const WITHOUT_PORTS = 4;
/**
* @internal
*
* @param OutputInterface $output
*
* @return callable
*/
function logger(OutputInterface $output): callable
{
@ -31,19 +28,17 @@ function logger(OutputInterface $output): callable
* @public
*
* @param string $path Path to a YAML file
*
* @return array
*/
function readConfiguration(string $path): array
{
if (false === file_exists($path)) {
throw new \InvalidArgumentException(sprintf('File "%s" does not exist', $path));
throw new InvalidArgumentException(sprintf('File "%s" does not exist', $path));
}
try {
return Yaml::parse(file_get_contents($path));
} catch (ParseException $exception) {
throw new \InvalidArgumentException(sprintf('File "%s" does not contain valid YAML', $path));
throw new InvalidArgumentException(sprintf('File "%s" does not contain valid YAML', $path), $exception->getCode(), $exception);
}
}
@ -461,8 +456,6 @@ function addNetwork(Graph $graph, string $name, string $type = null)
* @param string|null $alias Alias associated to the linked element
* @param bool|null $bidirectional Biderectional or not
* @param bool|null $condition Wether the alias represents a condition or not
*
* @return Edge\Directed
*/
function addRelation(Vertex $from, Vertex $to, string $type, string $alias = null, bool $bidirectional = false, bool $condition = false): Edge\Directed
{