Propper handle libxml errors
authorjoshuaruesweg <ruesweg@woltlab.com>
Thu, 4 Mar 2021 16:03:41 +0000 (17:03 +0100)
committerjoshuaruesweg <ruesweg@woltlab.com>
Tue, 16 Mar 2021 15:19:13 +0000 (16:19 +0100)
wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php

index d26bbe21b53ae74e096f5dd523f82060d5aa8fba..7fdaa5908feadec549df23a3a09a035b2177f28c 100644 (file)
@@ -187,10 +187,19 @@ final class UnfurlResponse
      */
     private function readDomDocument(): void
     {
-        \libxml_use_internal_errors(true);
-        $this->domDocument = new \DOMDocument();
-        if (!$this->domDocument->loadHTML('<?xml version="1.0" encoding="UTF-8"?>' . $this->body)) {
-            throw new ParsingFailed("Could not parse body.");
+        $useInternalErrors = \libxml_use_internal_errors(true);
+        \libxml_clear_errors();
+        try {
+            $this->domDocument = new \DOMDocument();
+            if (!$this->domDocument->loadHTML('<?xml version="1.0" encoding="UTF-8"?>' . $this->body)) {
+                throw new ParsingFailed("DOMDocument::loadHTML() failed");
+            }
+            foreach (\libxml_get_errors() as $error) {
+                throw new ParsingFailed("libxml error: {$error->message}.", $error->code);
+            }
+        } finally {
+            \libxml_use_internal_errors($useInternalErrors);
+            \libxml_clear_errors();
         }
     }