From fa247b34f60bc6ae768a6552cffbeedf9e531f0b Mon Sep 17 00:00:00 2001 From: "J. King" Date: Wed, 8 Mar 2017 08:19:42 -0500 Subject: [PATCH] Reduce the number of database exception types --- lib/AbstractException.php | 26 +++++++++++++------------- lib/Db/ExceptionInput.php | 2 +- lib/Db/ExceptionStartup.php | 2 +- lib/Db/ExceptionTimeout.php | 2 +- lib/Db/SQLite3/Driver.php | 26 ++++++++++++-------------- locale/en.php | 26 +++++++++++++------------- 6 files changed, 41 insertions(+), 43 deletions(-) diff --git a/lib/AbstractException.php b/lib/AbstractException.php index bb2dfb4..04bb2c0 100644 --- a/lib/AbstractException.php +++ b/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, diff --git a/lib/Db/ExceptionInput.php b/lib/Db/ExceptionInput.php index 7aca7a2..2a2825d 100644 --- a/lib/Db/ExceptionInput.php +++ b/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 { } \ No newline at end of file diff --git a/lib/Db/ExceptionStartup.php b/lib/Db/ExceptionStartup.php index 7aca7a2..db51cb5 100644 --- a/lib/Db/ExceptionStartup.php +++ b/lib/Db/ExceptionStartup.php @@ -2,5 +2,5 @@ declare(strict_types=1); namespace JKingWeb\NewsSync\Db; -class ExceptionInput extends Exception { +class ExceptionStartup extends Exception { } \ No newline at end of file diff --git a/lib/Db/ExceptionTimeout.php b/lib/Db/ExceptionTimeout.php index 450ef67..7d4aa6c 100644 --- a/lib/Db/ExceptionTimeout.php +++ b/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 { } \ No newline at end of file diff --git a/lib/Db/SQLite3/Driver.php b/lib/Db/SQLite3/Driver.php index ee33e6f..826e288 100644 --- a/lib/Db/SQLite3/Driver.php +++ b/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 diff --git a/locale/en.php b/locale/en.php index 3ef9c6c..4e13834 100644 --- a/locale/en.php +++ b/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}}