14

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?

  • I feel like a community module to improve some of these bad core error messages would be epic. The other error message that's really bad is when it can't find a given phtml. – kalenjordan Oct 31 '13 at 13:26
14

The approach I always take is a simple one-liner:

find . -type f -name '*.xml' -exec xmllint --noout {} \;

libxml2-utils required though...

  • 1
    and shows false positives in the wsi files: namespace error : xmlns:typens: 'urn:{{var wsdl.name}}' is not a valid URI – Alex May 28 '13 at 10:21
  • Workaround: find . -type f -not -name 'wsi.xml' -not -name 'wsdl.xml' -not -name 'wsdl2.xml' -name '*.xml' -exec xmllint --noout {} \; (feel free to edit the answer) – Alex May 28 '13 at 10:24
  • For config.xml: I'm a fan of find . -type f -name 'config.xml' -exec xmllint --noout {} \; – benmarks May 31 '13 at 0:41
  • @Alex I am getting the same error, do you have fix for that? – Butterfly Apr 27 '16 at 8:01
  • Did you try the line I posted? – Alex Apr 27 '16 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.