Skip to content

Instantly share code, notes, and snippets.

@adamzero1
Last active June 29, 2024 11:41
Show Gist options
  • Save adamzero1/2c25dbcf1caec0ad9b55e7f6fd7972d8 to your computer and use it in GitHub Desktop.
Save adamzero1/2c25dbcf1caec0ad9b55e7f6fd7972d8 to your computer and use it in GitHub Desktop.
Get more descriptive error when DOM validation fails in Magento

Get a more debugable error message when DOM validation fails in Magento.

Original Error Message

1 exception(s):
Exception #0 (Magento\Framework\Config\Dom\ValidationException): Element 'arguments': This element is not expected.
Line: 1471

Error Message With Patch

1 exception(s):
Exception #0 (Magento\Framework\Config\Dom\ValidationException): Element 'arguments': This element is not expected.
Line: 1471
The xml was: 
1466:            <argument name="show_category" xsi:type="boolean">true</argument>
1467:         </arguments>
1468:      </block>
1469:      <referenceBlock name="product_view_detail" remove="true"/>
1470:      <block name="abc.gtm.product_view_detail" class="ABC\GoogleTagManager\Block\Detail" template="ABC_GoogleTagManager::detailproduct.phtml"/>
1471:      <arguments>
1472:         <argument name="show_category" xsi:type="boolean">true</argument>
1473:      </arguments>
1474:   </referenceContainer>
1475:</body>

This patch is NOT intended for production use, it is intended to help debug an issue, after the issue has been resolved it should be removed.

Installing & Applying the patch

wget https://gist.githubusercontent.com/adamzero1/2c25dbcf1caec0ad9b55e7f6fd7972d8/raw/b19f6779625aa29122c0bc500ae213ab0b6bb212/nice_dom_error_message.patch
patch -p0 < nice_dom_error_message.patch

Reverting and uninstalling patch

patch -p0 -R < nice_dom_error_message.patch
rm nice_dom_error_message.patch
--- vendor/magento/framework/Config/Dom.php 2023-01-03 14:33:27.739851841 +0000
+++ "vendor/magento/framework/Config/Dom.php" 2023-01-03 14:33:35.580142053 +0000
@@ -123,13 +123,13 @@
* @param string $errorFormat
* @return string[]
*/
- private static function getXmlErrors($errorFormat)
+ private static function getXmlErrors($errorFormat, $dom = null)
{
$errors = [];
$validationErrors = libxml_get_errors();
if (count($validationErrors)) {
foreach ($validationErrors as $error) {
- $errors[] = self::_renderErrorMessage($error, $errorFormat);
+ $errors[] = self::_renderErrorMessage($error, $errorFormat, $dom);
}
} else {
$errors[] = 'Unknown validation error';
@@ -380,7 +380,7 @@
try {
$result = $dom->schemaValidate($schema);
if (!$result) {
- $errors = self::getXmlErrors($errorFormat);
+ $errors = self::getXmlErrors($errorFormat, $dom);
}
} catch (\Exception $exception) {
$errors = self::getXmlErrors($errorFormat);
@@ -401,7 +401,7 @@
* @return string
* @throws \InvalidArgumentException
*/
- private static function _renderErrorMessage(\LibXMLError $errorInfo, $format)
+ private static function _renderErrorMessage(\LibXMLError $errorInfo, $format, $dom = null)
{
$result = $format;
foreach ($errorInfo as $field => $value) {
@@ -424,6 +424,15 @@
}
}
}
+ if($dom){
+ $xml = explode(PHP_EOL, $dom->saveXml());
+ $lines = array_slice($xml, max(0, $errorInfo->line - 5), 10, true);
+ $result .= 'The xml was: '.PHP_EOL;
+ foreach($lines as $lineNumber => $line){
+ $result .= $lineNumber.':'.$line.PHP_EOL;
+ }
+ }
+
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment