Skip to content

Commit

Permalink
Ref #11957 - diagnosis would be easier if the underling error had bee…
Browse files Browse the repository at this point in the history
…n exposed.
  • Loading branch information
iliepandia committed Apr 25, 2018
1 parent db244bc commit 0eb7d3d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/Utility/Xml.php
Expand Up @@ -157,18 +157,16 @@ protected static function _loadXml($input, $options)
$xml = new DOMDocument();
$xml->loadXML($input, $flags);
}
return $xml;
} catch (Exception $e) {
$xml = null;
}
if ($hasDisable && !$options['loadEntities']) {
libxml_disable_entity_loader(false);
}
libxml_use_internal_errors($internalErrors);
if ($xml === null) {
throw new XmlException('Xml cannot be read.');
throw new XmlException('Xml cannot be read. ' . $e->getMessage(), null, $e);
} finally {
//CakePHP requires PHP 5.6 so we can safely use finally to restore error handling
if ($hasDisable && !$options['loadEntities']) {
libxml_disable_entity_loader(false);
}
libxml_use_internal_errors($internalErrors);
}

return $xml;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/Utility/XmlTest.php
Expand Up @@ -18,6 +18,7 @@
use Cake\Core\Configure;
use Cake\ORM\Entity;
use Cake\TestSuite\TestCase;
use Cake\Utility\Exception\XmlException;
use Cake\Utility\Xml;

/**
Expand Down Expand Up @@ -64,6 +65,18 @@ public function tearDown()
Configure::write('App.encoding', $this->_appEncoding);
}

public function testExceptionChainingForInvalidInput(){
try {
$value = "invalid-xml-input<<";
Xml::build($value); //should throw an XmlException
$this->assertFalse(true,'This line should not be executed because of exception above.');
}catch(XmlException $exception){
$cause = $exception->getPrevious();
$this->assertNotNull($cause);
$this->assertInstanceOf(\Exception::class, $cause );
}
}

/**
* testBuild method
*
Expand Down

0 comments on commit 0eb7d3d

Please sign in to comment.