Magento is fairly protective (as it should be) regarding the display of errors. When developer mode is enabled (as it should be during development), the application allows runtime error feedback to bubble up to the user. For XML compilation errors though this feedback is fairly useless:
Fatal error: Uncaught exception 'Exception' with message 'Warning: simplexml_load_string(): Entity: line 4: parser error : XML declaration allowed only at the start of the document in [...]/lib/Varien/Simplexml/Config.php on line 510' in [...]app/code/core/Mage/Core/functions.php on line 245
This results from Varien_Simplexml_Config::loadFile()
presenting ::loadString()
with a string which cannot be parsed:
public function loadFile($filePath)
{
if (!is_readable($filePath)) {
//throw new Exception('Can not read xml file '.$filePath);
return false;
}
$fileData = file_get_contents($filePath);
$fileData = $this->processFileData($fileData);
return $this->loadString($fileData, $this->_elementClass);
}
There are several potential solutions, including using libxml_use_internal_errors
, but the calling method does not communicate the $filePath
param, so context would be lost. One possibility would be to throw a more explicit exception:
public function loadFile($filePath)
{
if (!is_readable($filePath)) {
//throw new Exception('Can not read xml file '.$filePath);
return false;
}
$fileData = file_get_contents($filePath);
$fileData = $this->processFileData($fileData);
try{
return $this->loadString($fileData, $this->_elementClass);
}
catch (Exception $e){
Mage::throwException (
sprintf(
"%s: error parsing %s:\r\n%s",
__METHOD__,
$filePath,
$e->getMessage()
)
);
}
}
This at least provides output like the following:
Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Varien_Simplexml_Config::loadFile: error parsing [...]/app/code/local/Some/Example/etc/config.xml: Warning: simplexml_load_string(): Entity: line 4: parser error : XML declaration allowed only at the start of the document in [...]/lib/Varien/Simplexml/Config.php on line 534' in [...]/app/Mage.php on line 594
Are there some advantages/disadvantages/alternate approaches to consider here?