From c8274eca749b9aebf8f261adfacdebb6c102429b Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sat, 11 Feb 2017 14:56:02 -0500 Subject: [PATCH] Moved base exception code to abstract class This allows differentiating specific and general exceptions in tests; a library-wide trap would have to trap AbstractException --- lib/AbstractException.php | 60 +++++++++++++++++++++++++++++++++++++++ lib/Conf/Exception.php | 2 +- lib/Db/Exception.php | 2 +- lib/Exception.php | 55 +---------------------------------- lib/ExceptionFatal.php | 2 +- lib/Lang/Exception.php | 2 +- lib/User/Exception.php | 2 +- 7 files changed, 66 insertions(+), 59 deletions(-) create mode 100644 lib/AbstractException.php diff --git a/lib/AbstractException.php b/lib/AbstractException.php new file mode 100644 index 0000000..b580774 --- /dev/null +++ b/lib/AbstractException.php @@ -0,0 +1,60 @@ + -1, + "Exception.invalid" => 1, // this exception MUST NOT have a message string defined + "Exception.unknown" => 10000, + "Lang/Exception.defaultFileMissing" => 10101, + "Lang/Exception.fileMissing" => 10102, + "Lang/Exception.fileUnreadable" => 10103, + "Lang/Exception.fileCorrupt" => 10104, + "Lang/Exception.stringMissing" => 10105, + "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/Update/Exception.tooNew" => 10211, + "Db/Update/Exception.fileMissing" => 10212, + "Db/Update/Exception.fileUnusable" => 10213, + "Db/Update/Exception.fileUnreadable" => 10214, + "Db/Update/Exception.manual" => 10215, + "Db/Update/Exception.manualOnly" => 10216, + "Conf/Exception.fileMissing" => 10302, + "Conf/Exception.fileUnusable" => 10303, + "Conf/Exception.fileUnreadable" => 10304, + "Conf/Exception.fileUnwritable" => 10305, + "Conf/Exception.fileUncreatable" => 10306, + "Conf/Exception.fileCorrupt" => 10307, + "User/Exception.functionNotImplemented" => 10401, + "User/Exception.doesNotExist" => 10402, + "User/Exception.alreadyExists" => 10403, + "User/Exception.authMissing" => 10411, + "User/Exception.authFailed" => 10412, + "User/Exception.notAuthorized" => 10421, + ]; + + public function __construct(string $msgID = "", $vars = null, \Throwable $e = null) { + if($msgID=="") { + $msg = "Exception.unknown"; + $code = 10000; + } else { + $class = get_called_class(); + $codeID = str_replace("\\", "/", str_replace(NS_BASE, "", $class)).".$msgID"; + if(!array_key_exists($codeID, self::CODES)) { + throw new Exception("uncoded"); + } else { + $code = self::CODES[$codeID]; + $msg = "Exception.".str_replace("\\", "/", $class).".$msgID"; + } + $msg = Lang::msg($msg, $vars); + } + parent::__construct($msg, $code, $e); + } +} \ No newline at end of file diff --git a/lib/Conf/Exception.php b/lib/Conf/Exception.php index d9a6b18..99bbd4c 100644 --- a/lib/Conf/Exception.php +++ b/lib/Conf/Exception.php @@ -2,5 +2,5 @@ declare(strict_types=1); namespace JKingWeb\NewsSync\Conf; -class Exception extends \JKingWeb\NewsSync\Exception { +class Exception extends \JKingWeb\NewsSync\AbstractException { } \ No newline at end of file diff --git a/lib/Db/Exception.php b/lib/Db/Exception.php index bf4dea1..1f42021 100644 --- a/lib/Db/Exception.php +++ b/lib/Db/Exception.php @@ -2,5 +2,5 @@ declare(strict_types=1); namespace JKingWeb\NewsSync\Db; -class Exception extends \JKingWeb\NewsSync\Exception { +class Exception extends \JKingWeb\NewsSync\AbstractException { } \ No newline at end of file diff --git a/lib/Exception.php b/lib/Exception.php index 17dec58..e7043f0 100644 --- a/lib/Exception.php +++ b/lib/Exception.php @@ -2,58 +2,5 @@ declare(strict_types=1); namespace JKingWeb\NewsSync; -class Exception extends \Exception { - - const CODES = [ - "Exception.uncoded" => -1, - "Exception.invalid" => 1, // this exception MUST NOT have a message string defined - "Exception.unknown" => 10000, - "Lang/Exception.defaultFileMissing" => 10101, - "Lang/Exception.fileMissing" => 10102, - "Lang/Exception.fileUnreadable" => 10103, - "Lang/Exception.fileCorrupt" => 10104, - "Lang/Exception.stringMissing" => 10105, - "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/Update/Exception.tooNew" => 10211, - "Db/Update/Exception.fileMissing" => 10212, - "Db/Update/Exception.fileUnusable" => 10213, - "Db/Update/Exception.fileUnreadable" => 10214, - "Db/Update/Exception.manual" => 10215, - "Db/Update/Exception.manualOnly" => 10216, - "Conf/Exception.fileMissing" => 10302, - "Conf/Exception.fileUnusable" => 10303, - "Conf/Exception.fileUnreadable" => 10304, - "Conf/Exception.fileUnwritable" => 10305, - "Conf/Exception.fileUncreatable" => 10306, - "Conf/Exception.fileCorrupt" => 10307, - "User/Exception.functionNotImplemented" => 10401, - "User/Exception.doesNotExist" => 10402, - "User/Exception.alreadyExists" => 10403, - "User/Exception.authMissing" => 10411, - "User/Exception.authFailed" => 10412, - "User/Exception.notAuthorized" => 10421, - ]; - - public function __construct(string $msgID = "", $vars = null, \Throwable $e = null) { - if($msgID=="") { - $msg = "Exception.unknown"; - $code = 10000; - } else { - $codeID = str_replace("\\", "/", str_replace(NS_BASE, "", get_called_class())).".$msgID"; - if(!array_key_exists($codeID,self::CODES)) { - throw new self("uncoded"); - } else { - $code = self::CODES[$codeID]; - $msg = "Exception.".str_replace("\\","/",get_called_class()).".$msgID"; - } - $msg = Lang::msg($msg, $vars); - } - parent::__construct($msg, $code, $e); - } +class Exception extends AbstractException { } \ No newline at end of file diff --git a/lib/ExceptionFatal.php b/lib/ExceptionFatal.php index 40f7202..fcb7a96 100644 --- a/lib/ExceptionFatal.php +++ b/lib/ExceptionFatal.php @@ -2,7 +2,7 @@ declare(strict_types=1); namespace JKingWeb\NewsSync; -class ExceptionFatal extends Exception { +class ExceptionFatal extends AbstractException { public function __construct($msg = "", $code = 0, $e = null) { \Exception::__construct($msg, $code, $e); } diff --git a/lib/Lang/Exception.php b/lib/Lang/Exception.php index 88291d4..e280322 100644 --- a/lib/Lang/Exception.php +++ b/lib/Lang/Exception.php @@ -2,7 +2,7 @@ declare(strict_types=1); namespace JKingWeb\NewsSync\Lang; -class Exception extends \JKingWeb\NewsSync\Exception { +class Exception extends \JKingWeb\NewsSync\AbstractException { static $test = false; // used during PHPUnit testing only function __construct(string $msgID = "", $vars = null, \Throwable $e = null) { diff --git a/lib/User/Exception.php b/lib/User/Exception.php index ed4c7e5..a1632d5 100644 --- a/lib/User/Exception.php +++ b/lib/User/Exception.php @@ -2,5 +2,5 @@ declare(strict_types=1); namespace JKingWeb\NewsSync\User; -class Exception extends \JKingWeb\NewsSync\Exception { +class Exception extends \JKingWeb\NewsSync\AbstractException { } \ No newline at end of file