Skip to content

Commit

Permalink
Handle some db errors better and improve error logging #27
Browse files Browse the repository at this point in the history
  • Loading branch information
nearwood committed Oct 31, 2019
1 parent 0a1be8f commit 0214790
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
39 changes: 29 additions & 10 deletions src/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<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);
//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 '<div class="error">Error connecting to database.</div>';
$this->dbError($e);
$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);
}

Expand All @@ -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
Expand All @@ -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)
{
Expand Down Expand Up @@ -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 '<div class="warn">Incomplete MSQ metadata.</div>';
return false;
}
Expand Down Expand Up @@ -708,6 +724,7 @@ private function dbError($e)
{
if (DEBUG)
{
error("DB Error: " . $e->getMessage());
echo '<div class="error">Error executing database query:<br/>';
echo $e->getMessage();
echo '</div>';
Expand All @@ -722,7 +739,7 @@ private function dbError($e)
*/
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;

Expand All @@ -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('<div class="debug">XML Found...</div>');
if (DEBUG) debug('XML Found.');
$result = $st->fetch(PDO::FETCH_ASSOC);
$st->closeCursor();
$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)
{
Expand Down
19 changes: 11 additions & 8 deletions src/msq.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,20 @@ private function msqConstant($constant, $value, $help)
public function parseMSQ($xml, &$engine, &$metadata)
{
$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.

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'] = '<div class="error">Unable to parse MSQ.</div>';
} else if ($msq) {
$msqHeader = '<div class="info">';
$msqHeader .= "<div>Format Version: " . $msq->versionInfo['fileFormat'] . "</div>";
$msqHeader .= "<div>MS Signature: " . $msq->versionInfo['signature'] . "</div>";
Expand Down Expand Up @@ -176,10 +183,6 @@ public function parseMSQ($xml, &$engine, &$metadata)
}
}
}
else
{
$html['header'] = '<div class="error">Unable to parse tune.</div>';
}

return $html;
}
Expand Down
28 changes: 15 additions & 13 deletions src/msqur.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= "<div class=\"group-$group\">";
$html .= $v;
$html .= '</div>';
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 .= "<div class=\"group-$group\">";
$html .= $v;
$html .= '</div>';
}

$this->db->updateCache($id, $html);
}

$this->db->updateCache($id, $html);
}
}
//TODO else show 404
Expand Down

0 comments on commit 0214790

Please sign in to comment.