Skip to content

Commit 8783b88

Browse files
authored
feat: more informative error message on xml validation errors
1 parent 592c792 commit 8783b88

File tree

1 file changed

+14
-4
lines changed
  • lib/internal/Magento/Framework/Config

1 file changed

+14
-4
lines changed

lib/internal/Magento/Framework/Config/Dom.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,16 @@ public function __construct(
121121
* Retrieve array of xml errors
122122
*
123123
* @param string $errorFormat
124+
* @param \DOMDocument|null $dom
124125
* @return string[]
125126
*/
126-
private static function getXmlErrors($errorFormat)
127+
private static function getXmlErrors($errorFormat, $dom = null)
127128
{
128129
$errors = [];
129130
$validationErrors = libxml_get_errors();
130131
if (count($validationErrors)) {
131132
foreach ($validationErrors as $error) {
132-
$errors[] = self::_renderErrorMessage($error, $errorFormat);
133+
$errors[] = self::_renderErrorMessage($error, $errorFormat, $dom);
133134
}
134135
} else {
135136
$errors[] = 'Unknown validation error';
@@ -380,7 +381,7 @@ public static function validateDomDocument(
380381
try {
381382
$result = $dom->schemaValidate($schema);
382383
if (!$result) {
383-
$errors = self::getXmlErrors($errorFormat);
384+
$errors = self::getXmlErrors($errorFormat, $dom);
384385
}
385386
} catch (\Exception $exception) {
386387
$errors = self::getXmlErrors($errorFormat);
@@ -398,10 +399,11 @@ public static function validateDomDocument(
398399
*
399400
* @param \LibXMLError $errorInfo
400401
* @param string $format
402+
* @param \DOMDocument|null $dom
401403
* @return string
402404
* @throws \InvalidArgumentException
403405
*/
404-
private static function _renderErrorMessage(\LibXMLError $errorInfo, $format)
406+
private static function _renderErrorMessage(\LibXMLError $errorInfo, $format, $dom = null)
405407
{
406408
$result = $format;
407409
foreach ($errorInfo as $field => $value) {
@@ -424,6 +426,14 @@ private static function _renderErrorMessage(\LibXMLError $errorInfo, $format)
424426
}
425427
}
426428
}
429+
if ($dom) {
430+
$xml = explode(PHP_EOL, $dom->saveXml());
431+
$lines = array_slice($xml, max(0, $errorInfo->line - 5), 10, true);
432+
$result .= 'The xml was: '.PHP_EOL;
433+
foreach ($lines as $lineNumber => $line) {
434+
$result .= $lineNumber.':'.$line.PHP_EOL;
435+
}
436+
}
427437
return $result;
428438
}
429439

0 commit comments

Comments
 (0)