From ba4bf2976eb81275d49bf60e61d78e82190d6695 Mon Sep 17 00:00:00 2001
From: Sven Kirschbaum <kirschbaum@medien.rwth-aachen.de>
Date: Tue, 22 Aug 2023 11:37:32 +0200
Subject: [PATCH] fix: Provide more specific error message if the xml can not
 be parsed

---
 api/util/StackQuestionLoader.php | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/api/util/StackQuestionLoader.php b/api/util/StackQuestionLoader.php
index 8e47281bf..15026ba1b 100644
--- a/api/util/StackQuestionLoader.php
+++ b/api/util/StackQuestionLoader.php
@@ -13,12 +13,20 @@ class StackQuestionLoader
 {
     static public function loadXML($xml) {
         //TODO: Consider defaults
-        $xmlData = new SimpleXMLElement($xml);
+        try {
+            $xmlData = new SimpleXMLElement($xml);
+        } catch (\Exception $e) {
+            throw new \stack_exception("The provided file does not contain valid XML");
+        }
         $question = new \qtype_stack_question();
 
         //Throw error if more then one question element is contained in the xml
         if (count($xmlData->question) != 1) {
-            throw new \stack_exception("The provided xml file contains more than one question element");
+            throw new \stack_exception("The provided XML file does not contain exactly one question element");
+        }
+
+        if (((string) $xmlData->question->attributes()->type) !== "stack") {
+            throw new \stack_exception("The provided question is not of type STACK");
         }
 
         //Collect included files
-- 
GitLab