10000 introduced dedicated exception classes related to XML parsing. · Basster/symfony@9fcb50f · GitHub
[go: up one dir, main page]

Skip to content

Commit 9fcb50f

Browse files
committed
introduced dedicated exception classes related to XML parsing.
1 parent da07b89 commit 9fcb50f

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ public function testLoadFile()
6363
}
6464

6565
/**
66-
* @expectedException \InvalidArgumentException
66+
* @expectedException \Symfony\Component\Config\Util\Exception\InvalidXmlException
6767
* @expectedExceptionMessage The XML is not valid
6868
*/
69-
public function testParse()
69+
public function testParseWithInvalidValidatorCallable()
7070
{
7171
$fixtures = __DIR__.'/../Fixtures/Util/';
7272

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/*
3+
* This file is part of the Symfony package.
4+
*
5+
* (c) Fabien Potencier <fabien@symfony.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Symfony\Component\Config\Util\Exception;
12+
13+
/**
14+
* Exception class for when XML parsing with an XSD schema file path or a callable validator produces errors unrelated
15+
* to the actual XML parsing.
16+
*
17+
* @author Ole Rößner <ole@roessner.it>
18+
*/
19+
class InvalidXmlException extends XmlParsingException
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Config\Util\Exception;
13+
14+
/**
15+
* Exception class for when XML cannot be parsed properly.
16+
*
17+
* @author Ole Rößner <ole@roessner.it>
18+
*/
19+
class XmlParsingException extends \InvalidArgumentException
20+
{
21+
}

src/Symfony/Component/Config/Util/XmlUtils.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Component\Config\Util;
1313

14+
use Symfony\Component\Config\Util\Exception\InvalidXmlException;
15+
use Symfony\Component\Config\Util\Exception\XmlParsingException;
16+
1417
/**
1518
* XMLUtils is a bunch of utility methods to XML operations.
1619
*
@@ -37,7 +40,8 @@ private function __construct()
3740
*
3841
* @return \DOMDocument
3942
*
40-
* @throws \InvalidArgumentException When loading of XML file returns error
43+
* @throws \Symfony\Component\Config\Util\Exception\XmlParsingException When parsing of XML file returns error
44+
* @throws \Symfony\Component\Config\Util\Exception\InvalidXmlException When parsing of XML with schema or callable produces any errors unrelated to the XML parsing itself
4145
* @throws \RuntimeException When DOM extension is missing
4246
*/
4347
public static function parse($content, $schemaOrCallable = null)
@@ -55,7 +59,7 @@ public static function parse($content, $schemaOrCallable = null)
5559
if (!$dom->loadXML($content, LIBXML_NONET | (defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) {
5660
libxml_disable_entity_loader($disableEntities);
5761

58-
throw new \InvalidArgumentException(implode("\n", static::getXmlErrors($internalErrors)));
62+
throw new XmlParsingException(implode("\n", static::getXmlErrors($internalErrors)));
5963
}
6064

6165
$dom->normalizeDocument();
@@ -65,7 +69,7 @@ public static function parse($content, $schemaOrCallable = null)
6569

6670
foreach ($dom->childNodes as $child) {
6771
if (XML_DOCUMENT_TYPE_NODE === $child->nodeType) {
68-
throw new \InvalidArgumentException('Document types are not allowed.');
72+
throw new XmlParsingException('Document types are not allowed.');
6973
}
7074
}
7175

@@ -86,15 +90,15 @@ public static function parse($content, $schemaOrCallable = null)
8690
} else {
8791
libxml_use_internal_errors($internalErrors);
8892

89-
throw new \InvalidArgumentException('The schemaOrCallable argument has to be a valid path to XSD file or callable.');
93+
throw new XmlParsingException('The schemaOrCallable argument has to be a valid path to XSD file or callable.');
9094
}
9195

9296
if (!$valid) {
9397
$messages = static::getXmlErrors($internalErrors);
9498
if (empty($messages)) {
95-
$messages = array('The XML is not valid.');
99+
throw new InvalidXmlException('The XML is not valid.', 0, $e);
96100
}
97-
throw new \InvalidArgumentException(implode("\n", $messages), 0, $e);
101+
throw new XmlParsingException(implode("\n", $messages), 0, $e);
98102
}
99103
}
100104

@@ -113,6 +117,7 @@ public static function parse($content, $schemaOrCallable = null)
113117
* @return \DOMDocument
114118
*
115119
* @throws \InvalidArgumentException When loading of XML file returns error
120+
* @throws \Symfony\Component\Config\Util\Exception\XmlParsingException When XML parsing returns any errors.
116121
* @throws \RuntimeException When DOM extension is missing
117122
*/
118123
public static function loadFile($file, $schemaOrCallable = null)
@@ -124,11 +129,8 @@ public static function loadFile($file, $schemaOrCallable = null)
124129

125130
try {
126131
return static::parse($content, $schemaOrCallable);
127-
} catch (\InvalidArgumentException $e) {
128-
if ('The XML is not valid.' !== $e->getMessage()) {
129-
throw $e;
130-
}
131-
throw new \InvalidArgumentException(sprintf('The XML file "%s" is not valid.', $file), 0, $e->getPrevious());
132+
} catch (InvalidXmlException $e) {
133+
throw new XmlParsingException(sprintf('The XML file "%s" is not valid.', $file), 0, $e->getPrevious());
132134
}
133135
}
134136

0 commit comments

Comments
 (0)
0