Ver Fonte

added an XmlErrorException

Johannes Schmitt há 13 anos atrás
pai
commit
a85056a5ed

+ 20 - 0
Exception/XmlErrorException.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace JMS\SerializerBundle\Exception;
+
+class XmlErrorException extends RuntimeException
+{
+    private $xmlError;
+
+    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));
+
+        $this->xmlError = $error;
+    }
+
+    public function getXmlError()
+    {
+        return $this->xmlError;
+    }
+}

+ 2 - 2
Metadata/Driver/XmlDriver.php

@@ -2,6 +2,7 @@
 
 namespace JMS\SerializerBundle\Metadata\Driver;
 
+use JMS\SerializerBundle\Exception\XmlErrorException;
 use JMS\SerializerBundle\Annotation\ExclusionPolicy;
 use JMS\SerializerBundle\Metadata\PropertyMetadata;
 use Metadata\MethodMetadata;
@@ -17,8 +18,7 @@ class XmlDriver extends AbstractFileDriver
         libxml_use_internal_errors($previous);
 
         if (false === $elem) {
-            $error = libxml_get_last_error();
-            throw new \RuntimeException(sprintf('%d: Could not parse XML: %s in %s (line: %d, column: %d)', $error->level, $error->message, $error->file, $error->line, $error->column));
+            throw new XmlErrorException(libxml_get_last_error());
         }
 
         $metadata = new ClassMetadata($name = $class->getName());

+ 3 - 1
Serializer/XmlDeserializationVisitor.php

@@ -18,6 +18,8 @@
 
 namespace JMS\SerializerBundle\Serializer;
 
+use JMS\SerializerBundle\Exception\XmlErrorException;
+
 use JMS\SerializerBundle\Exception\RuntimeException;
 use JMS\SerializerBundle\Metadata\PropertyMetadata;
 use JMS\SerializerBundle\Metadata\ClassMetadata;
@@ -61,7 +63,7 @@ class XmlDeserializationVisitor extends AbstractVisitor
         libxml_use_internal_errors($previous);
 
         if (false === $doc) {
-            throw new RuntimeException(sprintf('Could not parse XML: %s', implode("\n", libxml_get_errors())));
+            throw new XmlErrorException(libxml_get_last_error());
         }
 
         return $doc;