| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <?php
- namespace ModulesGarden\Servers\ZimbraEmail\Core\HandlerError\Exceptions;
- /**
- * Base module Exception type
- *
- * @author Sławomir Miśkowicz <rafal.os@modulesgarden.com>
- */
- class Exception extends \Exception
- {
- use \ModulesGarden\Servers\ZimbraEmail\Core\Traits\ErrorCodesLibrary;
- use \ModulesGarden\Servers\ZimbraEmail\Core\Traits\Lang;
- use \ModulesGarden\Servers\ZimbraEmail\Core\Traits\IsDebugOn;
- use \ModulesGarden\Servers\ZimbraEmail\Core\UI\Traits\RequestObjectHandler;
-
- /**
- * A default error code number, selected when no code number provided
- */
- const DEFAULT_ERROR_CODE = 'CORE_ERR_000001';
- /**
- * An error code object
- *
- * @var type \ModulesGarden\Servers\ZimbraEmail\Core\HandlerError\ErrorCodes\ErrorCode
- */
- protected $errorCode = null;
- /**
- * Every Exception which can be caught as \Exception
- * @var type \Exception
- */
- protected $originalException = null;
- /**
- * An array of additionall data that will be logged with the Exception in order to help debug
- * @var type array
- */
- protected $additionalData = [];
- /**
- * An array of strings to be replaced in translate process, eg. for message:
- * "An error :xyz: occured" in order to replace key ':xyz:' with a '123' set this
- * param to: ['xyz' => '123']
- *
- * @var type array
- */
- protected $toTranslate = [];
- /**
- * This is a way to replace standard ErrorCode message, use it when no original exception
- * is present and the ErrorCode message, needs to be replaced, eg. API string error responses
- *
- * @var type string
- */
- protected $customMessage = null;
- public function __construct($errorCode = null, $additionalData = null, $toTranslate = null, $originalException = null)
- {
- $this->errorCode = $this->genErrorCode(($errorCode ? : self::DEFAULT_ERROR_CODE));
- $this->setAdditionalData($additionalData);
- $this->setToTranslate($toTranslate);
- $this->setOriginalException($originalException);
- }
- /**
- * Returns an error code for the exception
- * @return type string
- */
- public function getMgCode()
- {
- return $this->errorCode->getCode();
- }
- /**
- * Returns an error token for the exception, an unique string based on exception occurence timestamp
- * @return type string
- */
- public function getMgToken()
- {
- return $this->errorCode->getToken();
- }
- /**
- * Returns a date for the exception occurence
- * @return type string
- */
- public function getMgTime()
- {
- return date("Y-m-d H:i:s", time());
- }
- /**
- * Returns a translated or raw error message
- * @param type bool $translate
- * @return type string
- */
- public function getMgMessage($translate = true)
- {
- if ($translate)
- {
- $this->loadLang();
- $message = $this->lang->absoluteTranslate(
- $this->errorCode === self::DEFAULT_ERROR_CODE ? 'errorMessage' : 'errorCodeMessage',
- $this->selectProperMessage());
- }
- else
- {
- $message = $this->selectProperMessage();
- }
- return $this->replaceMessageVars($message);
- }
- /**
- * Replaces provided vars in the message string
- *
- * @param type $message string
- */
- public function replaceMessageVars($message)
- {
- foreach ($this->toTranslate as $key => $value)
- {
- $message = str_replace(':' . $key . ':', $value, $message);
- }
- return $message;
- }
- /**
- * Returns an originall exception object if such was provided
- *
- * @return type \Exception
- */
- public function getOriginalException()
- {
- return $this->originalException;
- }
- /**
- * Returns an array of data to be displayed when exception occured
- *
- * @return type array
- */
- public function getDetailsToDisplay()
- {
- $errorDetails = [];
- if ($this->isDebugOn() && $this->isAdminLogedIn())
- {
- $errorDetails['errorCode'] = $this->getMgCode();
- $errorDetails['errorToken'] = $this->getMgToken();
- $errorDetails['errorTime'] = $this->getMgTime();
- }
- $errorDetails['errorMessage'] = $this->getMgMessage(true);
- return $errorDetails;
- }
- /**
- * Returns an array of data to be logged when exception occured
- *
- * @return type array
- */
- public function getDetailsToLog()
- {
- $errorDetails = [];
- $errorDetails['errorCode'] = $this->getMgCode();
- $errorDetails['errorToken'] = $this->getMgToken();
- $errorDetails['errorTime'] = $this->getMgTime();
- $errorDetails['errorMessage'] = $this->getMgMessage(false);
- $errorDetails['additionalData'] = $this->getAdditionalData();
-
- return $errorDetails;
- }
-
- /**
- * Select a proper message for the exepction
- * Priority:
- * 1 custom message
- * 2 original Exception message
- * 3 error code message
- *
- * @return type string
- */
- protected function selectProperMessage()
- {
- if (is_string($this->customMessage))
- {
- return $this->customMessage;
- }
-
- if ($this->originalException !== null)
- {
- return $this->originalException->getMessage();
- }
-
- return $this->errorCode->getMessage();
- }
-
- /**
- * Sets a $originalException param, so you can wrap other exception in this one,
- * in order to log and parse them automatically
- *
- * @param \Exception $originalException
- */
- public function setOriginalException($originalException)
- {
- if ($originalException instanceof \Exception)
- {
- $this->originalException = $originalException;
-
- parent::__construct($originalException->getMessage(), $originalException->getCode(), $originalException->getPrevious());
- }
- }
-
- /**
- *
- * @param type $data array
- * @return $this
- */
- public function setAdditionalData($data = [])
- {
- if (is_array($data))
- {
- $this->additionalData = $data;
- }
-
- return $this;
- }
-
- /**
- *
- * @return type array
- */
- public function getAdditionalData()
- {
- return $this->additionalData;
- }
-
- /**
- *
- * @param type $data array
- * @return $this
- */
- public function setToTranslate($data = [])
- {
- if (is_array($data))
- {
- $this->toTranslate = $data;
- }
-
- return $this;
- }
-
- /**
- *
- * @param type $message string
- * @return $this
- */
- public function setCustomMessage($message = null)
- {
- if (is_string($message) && $message !== '')
- {
- $this->customMessage = $message;
- }
-
- return $this;
- }
-
- /**
- * Check if the exception should be logged or not
- *
- * @return boolean
- */
- public function isLogable()
- {
- if ($this->errorCode->isLogable())
- {
- return true;
- }
- if ($this->isAdminLogedIn() && $this->isDebugOn())
- {
- return true;
- }
-
- return false;
- }
-
- /**
- * Check if the administrator user is logged in current session
- *
- * @return boolean
- */
- public function isAdminLogedIn()
- {
- $this->loadRequestObj();
- $adminId = $this->request->getSession('adminid');
- if (is_int($adminId) && $adminId > 0)
- {
- return true;
- }
-
- return false;
- }
- }
|