8000 merged branch gimler/pimp_exception (PR #7433) · symfony/symfony@6da248e · GitHub
[go: up one dir, main page]

Skip to content

Commit 6da248e

Browse files
committed
merged branch gimler/pimp_exception (PR #7433)
This PR was submitted for the master branch but it was merged into the 2.2 branch instead (closes #7433). Commits ------- 9f84528 change wrapped exception message to be more usefull Discussion ---------- change wrapped exception message to be more usefull | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT When you try to parse a yml file with the XMLLoader you get the following Exception ``` [Symfony\Component\DependencyInjection\Exception\InvalidArgumentException] [ERROR 4] Start tag expected, '<' not found (in n/a - line 1, column 1) [InvalidArgumentException] [ERROR 4] Start tag expected, '<' not found (in n/a - line 1, column 1) ``` after the patch ``` [Symfony\Component\DependencyInjection\Exception\InvalidArgumentException] Unable to parse file "/home/.../src/Application/FOS/UserBundle/DependencyInjection/../Resources/config/services.yml". [InvalidArgumentException] [ERROR 4] Start tag expected, '<' not found (in n/a - line 1, column 1) ```
2 parents 6c531c1 + d015a0f commit 6da248e

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ protected function parseFile($file)
210210
try {
211211
$dom = XmlUtils::loadFile($file, array($this, 'validateSchema'));
212212
} catch (\InvalidArgumentException $e) {
213-
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
213+
throw new InvalidArgumentException(sprintf('Unable to parse file "%s".', $file), $e->getCode(), $e);
214214
}
215215

216216
$this->validateExtensions($dom, $file);

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testLoad()
5050
$loader->load('foo.xml');
5151
$this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist');
5252
} catch (\Exception $e) {
53-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
53+
$this->assertInstanceOf('InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the loaded file does not exist');
5454
$this->assertStringStartsWith('The file "foo.xml" does not exist (in:', $e->getMessage(), '->load() throws an InvalidArgumentException if the loaded file does not exist');
5555
}
5656
}
@@ -66,7 +66,11 @@ public function testParseFile()
6666
$m->invoke($loader, self::$fixturesPath.'/ini/parameters.ini');
6767
$this->fail('->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
6868
} catch (\Exception $e) {
69-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
69+
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
70+
$this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'parameters.ini'), $e->getMessage(), '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
71+
72+
$e = $e->getPrevious();
73+
$this->assertInstanceOf('InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
7074
$this->assertStringStartsWith('[ERROR 4] Start tag expected, \'<\' not found (in', $e->getMessage(), '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
7175
}
7276

@@ -76,7 +80,11 @@ public function testParseFile()
7680
$m->invoke($loader, self::$fixturesPath.'/xml/nonvalid.xml');
7781
$this->fail('->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
7882
} catch (\Exception $e) {
79-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
83+
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
84+
$this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'nonvalid.xml'), $e->getMessage(), '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file');
85+
86+
$e = $e->getPrevious();
87+
$this->assertInstanceOf('InvalidArgumentException', $e, '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
8088
$this->assertStringStartsWith('[ERROR 1845] Element \'nonvalid\': No matching global declaration available for the validation root. (in', $e->getMessage(), '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD');
8189
}
8290

@@ -262,8 +270,12 @@ public function testExtensions()
262270
$loader->load('extensions/services3.xml');
263271
$this->fail('->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
264272
} catch (\Exception $e) {
265-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
266-
$this->assertRegexp('/The attribute \'bar\' is not allowed/', $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
273+
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
274+
$this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'services3.xml'), $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
275+
276+
$e = $e->getPrevious();
277+
$this->assertInstanceOf('InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
278+
$this->assertContains('The attribute \'bar\' is not allowed', $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
267279
}
268280

269281
// non-registered extension
@@ -295,8 +307,12 @@ public function testExtensionInPhar()
295307
$loader->load('extensions/services7.xml');
296308
$this->fail('->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
297309
} catch (\Exception $e) {
298-
$this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
299-
$this->assertRegexp('/The attribute \'bar\' is not allowed/', $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
310+
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
311+
$this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'services7.xml'), $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
312+
313+
$e = $e->getPrevious();
314+
$this->assertInstanceOf('InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
315+
$this->assertContains('The attribute \'bar\' is not allowed', $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD');
300316
}
301317
}
302318

@@ -333,15 +349,23 @@ public function testNoNamingConflictsForAnonymousServices()
333349
$this->assertEquals('BarClass2', $inner2->getClass(), '->load() uses the same configuration as for the anonymous ones');
334350
}
335351

336-
/**
337-
* @expectedException \InvalidArgumentException
338-
* @expectedExceptionMessage Document types are not allowed.
339-
*/
340352
public function testDocTypeIsNotAllowed()
341353
{
342354
$container = new ContainerBuilder();
343355

344-
$loader1 = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
345-
$loader1->load('withdoctype.xml');
356+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
357+
358+
// document types are not allowed.
359+
try {
360+
$loader->load('withdoctype.xml');
361+
$this->fail('->load() throws an InvalidArgumentException if the configuration contains a document type');
362+
} catch (\Exception $e) {
363+
$this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\Exception\\InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration contains a document type');
364+
$this->assertRegExp(sprintf('#^Unable to parse file ".+%s".$#', 'withdoctype.xml'), $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration contains a document type');
365+
366+
$e = $e->getPrevious();
367+
$this->assertInstanceOf('InvalidArgumentException', $e, '->load() throws an InvalidArgumentException if the configuration contains a document type');
368+
$this->assertSame('Document types are not allowed.', $e->getMessage(), '->load() throws an InvalidArgumentException if the configuration contains a document type');
369+
}
346370
}
347371
}

0 commit comments

Comments
 (0)
0