Browse Source

Reduce the number of database exception types

microsub
J. King 7 years ago
parent
commit
fa247b34f6
  1. 26
      lib/AbstractException.php
  2. 2
      lib/Db/ExceptionInput.php
  3. 2
      lib/Db/ExceptionStartup.php
  4. 2
      lib/Db/ExceptionTimeout.php
  5. 26
      lib/Db/SQLite3/Driver.php
  6. 26
      locale/en.php

26
lib/AbstractException.php

@ -14,22 +14,22 @@ abstract class AbstractException extends \Exception {
"Lang/Exception.fileCorrupt" => 10104,
"Lang/Exception.stringMissing" => 10105,
"Lang/Exception.stringInvalid" => 10106,
"Db/ExceptionStartup.extMissing" => 10201,
"Db/ExceptionStartup.fileMissing" => 10202,
"Db/ExceptionStartup.fileUnusable" => 10203,
"Db/ExceptionStartup.fileUnreadable" => 10204,
"Db/ExceptionStartup.fileUnwritable" => 10205,
"Db/ExceptionStartup.fileUncreatable" => 10206,
"Db/ExceptionStartup.fileCorrupt" => 10207,
"Db/Exception.extMissing" => 10201,
"Db/Exception.fileMissing" => 10202,
"Db/Exception.fileUnusable" => 10203,
"Db/Exception.fileUnreadable" => 10204,
"Db/Exception.fileUnwritable" => 10205,
"Db/Exception.fileUncreatable" => 10206,
"Db/Exception.fileCorrupt" => 10207,
"Db/Exception.updateTooNew" => 10211,
"Db/Exception.updateFileMissing" => 10212,
"Db/Exception.updateFileUnusable" => 10213,
"Db/Exception.updateFileUnreadable" => 10214,
"Db/Exception.updateManual" => 10215,
"Db/Exception.updateManualOnly" => 10216,
"Db/Exception.paramTypeInvalid" => 10401,
"Db/Exception.paramTypeUnknown" => 10402,
"Db/Exception.paramTypeMissing" => 10403,
"Db/ExceptionUpdate.tooNew" => 10211,
"Db/ExceptionUpdate.fileMissing" => 10212,
"Db/ExceptionUpdate.fileUnusable" => 10213,
"Db/ExceptionUpdate.fileUnreadable" => 10214,
"Db/ExceptionUpdate.manual" => 10215,
"Db/ExceptionUpdate.manualOnly" => 10216,
"Conf/Exception.fileMissing" => 10302,
"Conf/Exception.fileUnusable" => 10303,
"Conf/Exception.fileUnreadable" => 10304,

2
lib/Db/ExceptionInput.php

@ -2,5 +2,5 @@
declare(strict_types=1);
namespace JKingWeb\NewsSync\Db;
class ExceptionInput extends Exception {
class ExceptionInput extends \JKingWeb\NewsSync\AbstractException {
}

2
lib/Db/ExceptionStartup.php

@ -2,5 +2,5 @@
declare(strict_types=1);
namespace JKingWeb\NewsSync\Db;
class ExceptionInput extends Exception {
class ExceptionStartup extends Exception {
}

2
lib/Db/ExceptionTimeout.php

@ -2,5 +2,5 @@
declare(strict_types=1);
namespace JKingWeb\NewsSync\Db;
class ExceptionTimeout extends Exception {
class ExceptionTimeout extends \JKingWeb\NewsSync\AbstractException {
}

26
lib/Db/SQLite3/Driver.php

@ -3,8 +3,6 @@ declare(strict_types=1);
namespace JKingWeb\NewsSync\Db\SQLite3;
use JKingWeb\NewsSync\Lang;
use JKingWeb\NewsSync\Db\Exception;
use JKingWeb\NewsSync\Db\ExceptionStartup;
use JKingWeb\NewsSync\Db\ExceptionUpdate;
use JKingWeb\NewsSync\Db\ExceptionInput;
use JKingWeb\NewsSync\Db\ExceptionTimeout;
@ -20,7 +18,7 @@ class Driver extends \JKingWeb\NewsSync\Db\AbstractDriver {
public function __construct(\JKingWeb\NewsSync\RuntimeData $data, bool $install = false) {
// check to make sure required extension is loaded
if(!class_exists("SQLite3")) throw new ExceptionStartup("extMissing", self::driverName());
if(!class_exists("SQLite3")) throw new Exception("extMissing", self::driverName());
$this->data = $data;
$file = $data->conf->dbSQLite3File;
// if the file exists (or we're initializing the database), try to open it and set initial options
@ -32,14 +30,14 @@ class Driver extends \JKingWeb\NewsSync\Db\AbstractDriver {
} catch(\Throwable $e) {
// if opening the database doesn't work, check various pre-conditions to find out what the problem might be
if(!file_exists($file)) {
if($install && !is_writable(dirname($file))) throw new ExceptionStartup("fileUncreatable", dirname($file));
throw new ExceptionStartup("fileMissing", $file);
if($install && !is_writable(dirname($file))) throw new Exception("fileUncreatable", dirname($file));
throw new Exception("fileMissing", $file);
}
if(!is_readable($file) && !is_writable($file)) throw new ExceptionStartup("fileUnusable", $file);
if(!is_readable($file)) throw new ExceptionStartup("fileUnreadable", $file);
if(!is_writable($file)) throw new ExceptionStartup("fileUnwritable", $file);
if(!is_readable($file) && !is_writable($file)) throw new Exception("fileUnusable", $file);
if(!is_readable($file)) throw new Exception("fileUnreadable", $file);
if(!is_writable($file)) throw new Exception("fileUnwritable", $file);
// otherwise the database is probably corrupt
throw new ExceptionStartup("fileCorrupt", $mainfile);
throw new Exception("fileCorrupt", $mainfile);
}
}
@ -59,8 +57,8 @@ class Driver extends \JKingWeb\NewsSync\Db\AbstractDriver {
public function schemaUpdate(int $to): bool {
$ver = $this->schemaVersion();
if(!$this->data->conf->dbSQLite3AutoUpd) throw new ExceptionUpdate("manual", ['version' => $ver, 'driver_name' => $this->driverName()]);
if($ver >= $to) throw new ExceptionUpdate("tooNew", ['difference' => ($ver - $to), 'current' => $ver, 'target' => $to, 'driver_name' => $this->driverName()]);
if(!$this->data->conf->dbSQLite3AutoUpd) throw new Exception("updateManual", ['version' => $ver, 'driver_name' => $this->driverName()]);
if($ver >= $to) throw new Exception("updateTooNew", ['difference' => ($ver - $to), 'current' => $ver, 'target' => $to, 'driver_name' => $this->driverName()]);
$sep = \DIRECTORY_SEPARATOR;
$path = \JKingWeb\NewsSync\BASE."sql".$sep."SQLite3".$sep;
$this->lock();
@ -69,10 +67,10 @@ class Driver extends \JKingWeb\NewsSync\Db\AbstractDriver {
$this->begin();
try {
$file = $path.$a.".sql";
if(!file_exists($file)) throw new ExceptionUpdate("fileMissing", ['file' => $file, 'driver_name' => $this->driverName()]);
if(!is_readable($file)) throw new ExceptionUpdate("fileUnreadable", ['file' => $file, 'driver_name' => $this->driverName()]);
if(!file_exists($file)) throw new Exception("updateFileMissing", ['file' => $file, 'driver_name' => $this->driverName()]);
if(!is_readable($file)) throw new Exception("updateFileUnreadable", ['file' => $file, 'driver_name' => $this->driverName()]);
$sql = @file_get_contents($file);
if($sql===false) throw new ExceptionUpdate("fileUnusable", ['file' => $file, 'driver_name' => $this->driverName()]);
if($sql===false) throw new Exception("updateFileUnusable", ['file' => $file, 'driver_name' => $this->driverName()]);
$this->exec($sql);
} catch(\Throwable $e) {
// undo any partial changes from the failed update

26
locale/en.php

@ -21,31 +21,31 @@ return [
'Exception.JKingWeb/NewsSync/Conf/Exception.fileUnwritable' => 'Insufficient permissions to overwrite configuration file "{0}"',
'Exception.JKingWeb/NewsSync/Conf/Exception.fileCorrupt' => 'Configuration file "{0}" is corrupt or does not conform to expected format',
'Exception.JKingWeb/NewsSync/Db/ExceptionStartup.extMissing' => 'Required PHP extension for driver "{0}" not installed',
'Exception.JKingWeb/NewsSync/Db/ExceptionStartup.fileMissing' => 'Database file "{0}" does not exist',
'Exception.JKingWeb/NewsSync/Db/ExceptionStartup.fileUnreadable' => 'Insufficient permissions to open database file "{0}" for reading',
'Exception.JKingWeb/NewsSync/Db/ExceptionStartup.fileUnwritable' => 'Insufficient permissions to open database file "{0}" for writing',
'Exception.JKingWeb/NewsSync/Db/ExceptionStartup.fileUnusable' => 'Insufficient permissions to open database file "{0}" for reading or writing',
'Exception.JKingWeb/NewsSync/Db/ExceptionStartup.fileUncreatable' => 'Insufficient permissions to create new database file "{0}"',
'Exception.JKingWeb/NewsSync/Db/ExceptionStartup.fileCorrupt' => 'Database file "{0}" is corrupt or not a valid database',
'Exception.JKingWeb/NewsSync/Db/Exception.extMissing' => 'Required PHP extension for driver "{0}" not installed',
'Exception.JKingWeb/NewsSync/Db/Exception.fileMissing' => 'Database file "{0}" does not exist',
'Exception.JKingWeb/NewsSync/Db/Exception.fileUnreadable' => 'Insufficient permissions to open database file "{0}" for reading',
'Exception.JKingWeb/NewsSync/Db/Exception.fileUnwritable' => 'Insufficient permissions to open database file "{0}" for writing',
'Exception.JKingWeb/NewsSync/Db/Exception.fileUnusable' => 'Insufficient permissions to open database file "{0}" for reading or writing',
'Exception.JKingWeb/NewsSync/Db/Exception.fileUncreatable' => 'Insufficient permissions to create new database file "{0}"',
'Exception.JKingWeb/NewsSync/Db/Exception.fileCorrupt' => 'Database file "{0}" is corrupt or not a valid database',
'Exception.JKingWeb/NewsSync/Db/Exception.paramTypeInvalid' => 'Prepared statement parameter type "{0}" is invalid',
'Exception.JKingWeb/NewsSync/Db/Exception.paramTypeUnknown' => 'Prepared statement parameter type "{0}" is valid, but not implemented',
'Exception.JKingWeb/NewsSync/Db/Exception.paramTypeMissing' => 'Prepared statement parameter type for parameter #{0} was not specified',
'Exception.JKingWeb/NewsSync/Db/ExceptionUpdate.manual' =>
'Exception.JKingWeb/NewsSync/Db/Exception.updateManual' =>
'{from_version, select,
0 {{driver_name} database is configured for manual updates and is not initialized; please populate the database with the base schema}
other {{driver_name} database is configured for manual updates; please update from schema version {current} to version {target}}
}',
'Exception.JKingWeb/NewsSync/Db/ExceptionUpdate.manualOnly' =>
'Exception.JKingWeb/NewsSync/Db/Exception.updateManualOnly' =>
'{from_version, select,
0 {{driver_name} database must be updated manually and is not initialized; please populate the database with the base schema}
other {{driver_name} database must be updated manually; please update from schema version {current} to version {target}}
}',
'Exception.JKingWeb/NewsSync/Db/ExceptionUpdate.fileMissing' => 'Automatic updating of the {driver_name} database failed due to instructions for updating from version {current} not being available',
'Exception.JKingWeb/NewsSync/Db/ExceptionUpdate.fileUnreadable' => 'Automatic updating of the {driver_name} database failed due to insufficient permissions to read instructions for updating from version {current}',
'Exception.JKingWeb/NewsSync/Db/ExceptionUpdate.fileUnusable' => 'Automatic updating of the {driver_name} database failed due to an error reading instructions for updating from version {current}',
'Exception.JKingWeb/NewsSync/Db/ExceptionUpdate.tooNew' =>
'Exception.JKingWeb/NewsSync/Db/Exception.updateFileMissing' => 'Automatic updating of the {driver_name} database failed due to instructions for updating from version {current} not being available',
'Exception.JKingWeb/NewsSync/Db/Exception.updateFileUnreadable' => 'Automatic updating of the {driver_name} database failed due to insufficient permissions to read instructions for updating from version {current}',
'Exception.JKingWeb/NewsSync/Db/Exception.updateFileUnusable' => 'Automatic updating of the {driver_name} database failed due to an error reading instructions for updating from version {current}',
'Exception.JKingWeb/NewsSync/Db/Exception.updateTooNew' =>
'{difference, select,
0 {Automatic updating of the {driver_name} database failed because it is already up to date with the requested version, {target}}
other {Automatic updating of the {driver_name} database failed because its version, {current}, is newer than the requested version, {target}}

Loading…
Cancel
Save