Johannes Schmitt %!s(int64=13) %!d(string=hai) anos
pai
achega
dbd2e5a81c

+ 18 - 1
Exception/XmlErrorException.php

@@ -8,7 +8,24 @@ class XmlErrorException extends RuntimeException
 
     public function __construct(\LibXMLError $error)
     {
-        parent::__construct(sprintf('%d: Could not parse XML: %s in %s (line: %d, column: %d)', $error->level, $error->message, $error->file, $error->line, $error->column));
+        switch ($error->level) {
+            case LIBXML_ERR_WARNING:
+                $level = 'WARNING';
+                break;
+
+            case LIBXML_ERR_FATAL:
+                $level = 'FATAL';
+                break;
+
+            case LIBXML_ERR_ERROR:
+                $level = 'ERROR';
+                break;
+
+            default:
+                $level = 'UNKNOWN';
+        }
+
+        parent::__construct(sprintf('[%s] %s in %s (line: %d, column: %d)', $level, $error->message, $error->file, $error->line, $error->column));
 
         $this->xmlError = $error;
     }

+ 13 - 0
Tests/Metadata/Driver/XmlDriverTest.php

@@ -7,6 +7,19 @@ use JMS\SerializerBundle\Metadata\Driver\XmlDriver;
 
 class XmlDriverTest extends BaseDriverTest
 {
+    /**
+     * @expectedException JMS\SerializerBundle\Exception\XmlErrorException
+     * @expectedExceptionMessage [FATAL] Start tag expected, '<' not found
+     */
+    public function testInvalidXml()
+    {
+        $driver = $this->getDriver();
+
+        $ref = new \ReflectionMethod($driver, 'loadMetadataFromFile');
+        $ref->setAccessible(true);
+        $ref->invoke($driver, new \ReflectionClass('stdClass'), __DIR__.'/xml/invalid.xml');
+    }
+
     protected function getDriver()
     {
         return new XmlDriver(new FileLocator(array(

+ 1 - 0
Tests/Metadata/Driver/xml/invalid.xml

@@ -0,0 +1 @@
+This contains no valid XML markup.