Handle some db errors better and improve error logging #27

This commit is contained in:
Nick 2019-10-31 16:23:58 -05:00
parent 0a1be8ff3f
commit 021479093f
3 changed files with 55 additions and 31 deletions

View File

@ -27,26 +27,27 @@ class DB
{ {
if (isset($this->db) && $this->db instanceof PDO) if (isset($this->db) && $this->db instanceof PDO)
{ {
if (DEBUG) debug("DEBUG: Reusing DB connection."); //if (DEBUG) debug("Reusing DB connection.");
} }
else else
{ {
try try
{ {
if (DEBUG) debug('<div class="debug">Connecting to DB: ' . "mysql:dbname=" . DB_NAME . ";host=" . DB_HOST . "," . DB_USERNAME . ", [****]" . '</div>'); //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); $this->db = new PDO("mysql:dbname=" . DB_NAME . ";host=" . DB_HOST, DB_USERNAME, DB_PASSWORD);
//Persistent connection: //Persistent connection:
//$this->db = new PDO("mysql:dbname=" . DB_NAME . ";host=" . DB_HOST, DB_USERNAME, DB_PASSWORD, array(PDO::ATTR_PERSISTENT => true); //$this->db = new PDO("mysql:dbname=" . DB_NAME . ";host=" . DB_HOST, DB_USERNAME, DB_PASSWORD, array(PDO::ATTR_PERSISTENT => true);
} }
catch (PDOException $e) catch (PDOException $e)
{ {
error("Could not connect to database");
echo '<div class="error">Error connecting to database.</div>'; echo '<div class="error">Error connecting to database.</div>';
$this->dbError($e); $this->dbError($e);
$this->db = null; //Redundant. $this->db = null; //Redundant.
} }
} }
if (DEBUG) debug('<div class="debug">Connecting to DB: ' . (($this->db != null) ? 'Connected.' : 'Connection FAILED') . '</div>'); //if (DEBUG) debug('Connecting to DB: ' . (($this->db != null) ? 'Connected.' : 'Connection FAILED'));
return ($this->db != null); return ($this->db != null);
} }
@ -68,6 +69,8 @@ class DB
try try
{ {
//TODO Compress? //TODO Compress?
//TODO transaction so we can rollback (`$db->beginTransaction()`)
$st = $this->db->prepare("INSERT INTO msqs (xml) VALUES (:xml)"); $st = $this->db->prepare("INSERT INTO msqs (xml) VALUES (:xml)");
$xml = file_get_contents($file['tmp_name']); $xml = file_get_contents($file['tmp_name']);
//Convert encoding to UTF-8 //Convert encoding to UTF-8
@ -87,11 +90,23 @@ class DB
$dt = new DateTime(); $dt = new DateTime();
$dt = $dt->format('Y-m-d H:i:s'); $dt = $dt->format('Y-m-d H:i:s');
DB::tryBind($st, ":uploaded", $dt); DB::tryBind($st, ":uploaded", $dt);
if ($st->execute()) $id = $this->db->lastInsertId(); if ($st->execute()) {
else $id = -1; $id = $this->db->lastInsertId();
} else {
error("Error inserting metadata");
if (DEBUG) {
print_r($st->errorInfo());
}
$id = -1;
}
$st->closeCursor(); $st->closeCursor();
} else {
error("Error inserting XML data");
if (DEBUG) {
print_r($st->errorInfo());
}
$id = -1;
} }
else $id = -1;
} }
catch (PDOException $e) catch (PDOException $e)
{ {
@ -626,6 +641,7 @@ class DB
if (!array_keys_exist($metadata, 'fileFormat', 'signature', 'firmware', 'author')) if (!array_keys_exist($metadata, 'fileFormat', 'signature', 'firmware', 'author'))
{ {
if (DEBUG) debug('Invalid MSQ metadata: ' . $metadata);
echo '<div class="warn">Incomplete MSQ metadata.</div>'; echo '<div class="warn">Incomplete MSQ metadata.</div>';
return false; return false;
} }
@ -708,6 +724,7 @@ class DB
{ {
if (DEBUG) if (DEBUG)
{ {
error("DB Error: " . $e->getMessage());
echo '<div class="error">Error executing database query:<br/>'; echo '<div class="error">Error executing database query:<br/>';
echo $e->getMessage(); echo $e->getMessage();
echo '</div>'; echo '</div>';
@ -722,7 +739,7 @@ class DB
*/ */
public function getXML($id) public function getXML($id)
{ {
if (DEBUG) debug('<div class="debug">Getting XML for id: ' . $id . '</div>'); if (DEBUG) debug('Getting XML for id: ' . $id);
if (!$this->connect()) return null; if (!$this->connect()) return null;
@ -732,14 +749,16 @@ class DB
{ {
$st = $this->db->prepare("SELECT xml FROM msqs INNER JOIN metadata ON metadata.msq = msqs.id WHERE metadata.id = :id LIMIT 1"); $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); DB::tryBind($st, ":id", $id);
if ($st->execute()) if ($st->execute() && $st->rowCount() === 1)
{ {
if (DEBUG) debug('<div class="debug">XML Found...</div>'); if (DEBUG) debug('XML Found.');
$result = $st->fetch(PDO::FETCH_ASSOC); $result = $st->fetch(PDO::FETCH_ASSOC);
$st->closeCursor(); $st->closeCursor();
$xml = $result['xml']; $xml = $result['xml'];
} else {
//TODO Send real 404
echo '<div class="error">404 MSQ not found.</div>';
} }
else echo '<div class="error">XML not found.</div>';
} }
catch (PDOException $e) catch (PDOException $e)
{ {

View File

@ -48,13 +48,20 @@ class MSQ
public function parseMSQ($xml, &$engine, &$metadata) public function parseMSQ($xml, &$engine, &$metadata)
{ {
$html = array(); $html = array();
if (DEBUG) debug('<div class="debug">Parsing MSQ...</div>'); if (DEBUG) debug('Parsing XML...');
$errorCount = 0; //Keep track of how many things go wrong. $errorCount = 0; //Keep track of how many things go wrong.
libxml_use_internal_errors(true);
$msq = simplexml_load_string($xml); $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'] = '<div class="error">Unable to parse MSQ.</div>';
} else if ($msq) {
$msqHeader = '<div class="info">'; $msqHeader = '<div class="info">';
$msqHeader .= "<div>Format Version: " . $msq->versionInfo['fileFormat'] . "</div>"; $msqHeader .= "<div>Format Version: " . $msq->versionInfo['fileFormat'] . "</div>";
$msqHeader .= "<div>MS Signature: " . $msq->versionInfo['signature'] . "</div>"; $msqHeader .= "<div>MS Signature: " . $msq->versionInfo['signature'] . "</div>";
@ -176,10 +183,6 @@ class MSQ
} }
} }
} }
else
{
$html['header'] = '<div class="error">Unable to parse tune.</div>';
}
return $html; return $html;
} }

View File

@ -154,20 +154,22 @@ class Msqur
$engine = array(); $engine = array();
$metadata = array(); $metadata = array();
$xml = $this->db->getXML($id); $xml = $this->db->getXML($id);
$groupedHtml = $msq->parseMSQ($xml, $engine, $metadata); if ($xml !== null) {
$this->db->updateMetadata($id, $metadata); $groupedHtml = $msq->parseMSQ($xml, $engine, $metadata);
$this->db->updateEngine($id, $engine); $this->db->updateMetadata($id, $metadata);
$this->db->updateEngine($id, $engine);
$html = "";
foreach($groupedHtml as $group => $v) $html = "";
{ foreach($groupedHtml as $group => $v)
//TODO Group name as fieldset legend or sth {
$html .= "<div class=\"group-$group\">"; //TODO Group name as fieldset legend or sth
$html .= $v; $html .= "<div class=\"group-$group\">";
$html .= '</div>'; $html .= $v;
$html .= '</div>';
}
$this->db->updateCache($id, $html);
} }
$this->db->updateCache($id, $html);
} }
} }
//TODO else show 404 //TODO else show 404