From 021479093f7103fcaff52a46a21e1fb74c717e45 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 31 Oct 2019 16:23:58 -0500 Subject: [PATCH] Handle some db errors better and improve error logging #27 --- src/db.php | 39 +++++++++++++++++++++++++++++---------- src/msq.php | 19 +++++++++++-------- src/msqur.php | 28 +++++++++++++++------------- 3 files changed, 55 insertions(+), 31 deletions(-) diff --git a/src/db.php b/src/db.php index 88186f1..f87ca2c 100644 --- a/src/db.php +++ b/src/db.php @@ -27,26 +27,27 @@ private function connect() { if (isset($this->db) && $this->db instanceof PDO) { - if (DEBUG) debug("DEBUG: Reusing DB connection."); + //if (DEBUG) debug("Reusing DB connection."); } else { try { - if (DEBUG) debug('
Connecting to DB: ' . "mysql:dbname=" . DB_NAME . ";host=" . DB_HOST . "," . DB_USERNAME . ", [****]" . '
'); + //if (DEBUG) debug('Connecting to DB: ' . "mysql:dbname=" . DB_NAME . ";host=" . DB_HOST . "," . DB_USERNAME . ", [****]"); $this->db = new PDO("mysql:dbname=" . DB_NAME . ";host=" . DB_HOST, DB_USERNAME, DB_PASSWORD); //Persistent connection: //$this->db = new PDO("mysql:dbname=" . DB_NAME . ";host=" . DB_HOST, DB_USERNAME, DB_PASSWORD, array(PDO::ATTR_PERSISTENT => true); } catch (PDOException $e) { + error("Could not connect to database"); echo '
Error connecting to database.
'; $this->dbError($e); $this->db = null; //Redundant. } } - if (DEBUG) debug('
Connecting to DB: ' . (($this->db != null) ? 'Connected.' : 'Connection FAILED') . '
'); + //if (DEBUG) debug('Connecting to DB: ' . (($this->db != null) ? 'Connected.' : 'Connection FAILED')); return ($this->db != null); } @@ -68,6 +69,8 @@ public function addMSQ($file, $engineid) try { //TODO Compress? + + //TODO transaction so we can rollback (`$db->beginTransaction()`) $st = $this->db->prepare("INSERT INTO msqs (xml) VALUES (:xml)"); $xml = file_get_contents($file['tmp_name']); //Convert encoding to UTF-8 @@ -87,11 +90,23 @@ public function addMSQ($file, $engineid) $dt = new DateTime(); $dt = $dt->format('Y-m-d H:i:s'); DB::tryBind($st, ":uploaded", $dt); - if ($st->execute()) $id = $this->db->lastInsertId(); - else $id = -1; + if ($st->execute()) { + $id = $this->db->lastInsertId(); + } else { + error("Error inserting metadata"); + if (DEBUG) { + print_r($st->errorInfo()); + } + $id = -1; + } $st->closeCursor(); + } else { + error("Error inserting XML data"); + if (DEBUG) { + print_r($st->errorInfo()); + } + $id = -1; } - else $id = -1; } catch (PDOException $e) { @@ -626,6 +641,7 @@ public function updateMetadata($id, $metadata) if (!array_keys_exist($metadata, 'fileFormat', 'signature', 'firmware', 'author')) { + if (DEBUG) debug('Invalid MSQ metadata: ' . $metadata); echo '
Incomplete MSQ metadata.
'; return false; } @@ -708,6 +724,7 @@ private function dbError($e) { if (DEBUG) { + error("DB Error: " . $e->getMessage()); echo '
Error executing database query:
'; echo $e->getMessage(); echo '
'; @@ -722,7 +739,7 @@ private function dbError($e) */ public function getXML($id) { - if (DEBUG) debug('
Getting XML for id: ' . $id . '
'); + if (DEBUG) debug('Getting XML for id: ' . $id); if (!$this->connect()) return null; @@ -732,14 +749,16 @@ public function getXML($id) { $st = $this->db->prepare("SELECT xml FROM msqs INNER JOIN metadata ON metadata.msq = msqs.id WHERE metadata.id = :id LIMIT 1"); DB::tryBind($st, ":id", $id); - if ($st->execute()) + if ($st->execute() && $st->rowCount() === 1) { - if (DEBUG) debug('
XML Found...
'); + if (DEBUG) debug('XML Found.'); $result = $st->fetch(PDO::FETCH_ASSOC); $st->closeCursor(); $xml = $result['xml']; + } else { + //TODO Send real 404 + echo '
404 MSQ not found.
'; } - else echo '
XML not found.
'; } catch (PDOException $e) { diff --git a/src/msq.php b/src/msq.php index 8b4f867..e96159c 100644 --- a/src/msq.php +++ b/src/msq.php @@ -48,13 +48,20 @@ private function msqConstant($constant, $value, $help) public function parseMSQ($xml, &$engine, &$metadata) { $html = array(); - if (DEBUG) debug('
Parsing MSQ...
'); + if (DEBUG) debug('Parsing XML...'); $errorCount = 0; //Keep track of how many things go wrong. + libxml_use_internal_errors(true); $msq = simplexml_load_string($xml); - - if ($msq) - { + + if ($msq === false) { + error("Failed to parse XML."); + foreach(libxml_get_errors() as $error) { + error($error->message); + } + + $html['header'] = '
Unable to parse MSQ.
'; + } else if ($msq) { $msqHeader = '
'; $msqHeader .= "
Format Version: " . $msq->versionInfo['fileFormat'] . "
"; $msqHeader .= "
MS Signature: " . $msq->versionInfo['signature'] . "
"; @@ -176,10 +183,6 @@ public function parseMSQ($xml, &$engine, &$metadata) } } } - else - { - $html['header'] = '
Unable to parse tune.
'; - } return $html; } diff --git a/src/msqur.php b/src/msqur.php index ae35f6e..00d3924 100644 --- a/src/msqur.php +++ b/src/msqur.php @@ -154,20 +154,22 @@ public function view($id) $engine = array(); $metadata = array(); $xml = $this->db->getXML($id); - $groupedHtml = $msq->parseMSQ($xml, $engine, $metadata); - $this->db->updateMetadata($id, $metadata); - $this->db->updateEngine($id, $engine); - - $html = ""; - foreach($groupedHtml as $group => $v) - { - //TODO Group name as fieldset legend or sth - $html .= "
"; - $html .= $v; - $html .= '
'; + if ($xml !== null) { + $groupedHtml = $msq->parseMSQ($xml, $engine, $metadata); + $this->db->updateMetadata($id, $metadata); + $this->db->updateEngine($id, $engine); + + $html = ""; + foreach($groupedHtml as $group => $v) + { + //TODO Group name as fieldset legend or sth + $html .= "
"; + $html .= $v; + $html .= '
'; + } + + $this->db->updateCache($id, $html); } - - $this->db->updateCache($id, $html); } } //TODO else show 404