Exception.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions;
  3. /**
  4. * Base module Exception type
  5. *
  6. * @autor ThurData <info@thrudata.ch>
  7. */
  8. class Exception extends \Exception
  9. {
  10. use \ThurData\Servers\KerioEmail\Core\Traits\ErrorCodesLibrary;
  11. use \ThurData\Servers\KerioEmail\Core\Traits\Lang;
  12. use \ThurData\Servers\KerioEmail\Core\Traits\IsDebugOn;
  13. use \ThurData\Servers\KerioEmail\Core\UI\Traits\RequestObjectHandler;
  14. /**
  15. * A default error code number, selected when no code number provided
  16. */
  17. const DEFAULT_ERROR_CODE = 'CORE_ERR_000001';
  18. /**
  19. * An error code object
  20. *
  21. * @var type \ThurData\Servers\KerioEmail\Core\HandlerError\ErrorCodes\ErrorCode
  22. */
  23. protected $errorCode = null;
  24. /**
  25. * Every Exception which can be caught as \Exception
  26. * @var type \Exception
  27. */
  28. protected $originalException = null;
  29. /**
  30. * An array of additionall data that will be logged with the Exception in order to help debug
  31. * @var type array
  32. */
  33. protected $additionalData = [];
  34. /**
  35. * An array of strings to be replaced in translate process, eg. for message:
  36. * "An error :xyz: occured" in order to replace key ':xyz:' with a '123' set this
  37. * param to: ['xyz' => '123']
  38. *
  39. * @var type array
  40. */
  41. protected $toTranslate = [];
  42. /**
  43. * This is a way to replace standard ErrorCode message, use it when no original exception
  44. * is present and the ErrorCode message, needs to be replaced, eg. API string error responses
  45. *
  46. * @var type string
  47. */
  48. protected $customMessage = null;
  49. public function __construct($errorCode = null, $additionalData = null, $toTranslate = null, $originalException = null)
  50. {
  51. $this->errorCode = $this->genErrorCode(($errorCode ? : self::DEFAULT_ERROR_CODE));
  52. $this->setAdditionalData($additionalData);
  53. $this->setToTranslate($toTranslate);
  54. $this->setOriginalException($originalException);
  55. }
  56. /**
  57. * Returns an error code for the exception
  58. * @return type string
  59. */
  60. public function getMgCode()
  61. {
  62. return $this->errorCode->getCode();
  63. }
  64. /**
  65. * Returns an error token for the exception, an unique string based on exception occurence timestamp
  66. * @return type string
  67. */
  68. public function getMgToken()
  69. {
  70. return $this->errorCode->getToken();
  71. }
  72. /**
  73. * Returns a date for the exception occurence
  74. * @return type string
  75. */
  76. public function getMgTime()
  77. {
  78. return date("Y-m-d H:i:s", time());
  79. }
  80. /**
  81. * Returns a translated or raw error message
  82. * @param type bool $translate
  83. * @return type string
  84. */
  85. public function getMgMessage($translate = true)
  86. {
  87. if ($translate)
  88. {
  89. $this->loadLang();
  90. $message = $this->lang->absoluteTranslate(
  91. $this->errorCode === self::DEFAULT_ERROR_CODE ? 'errorMessage' : 'errorCodeMessage',
  92. $this->selectProperMessage());
  93. }
  94. else
  95. {
  96. $message = $this->selectProperMessage();
  97. }
  98. return $this->replaceMessageVars($message);
  99. }
  100. /**
  101. * Replaces provided vars in the message string
  102. *
  103. * @param type $message string
  104. */
  105. public function replaceMessageVars($message)
  106. {
  107. foreach ($this->toTranslate as $key => $value)
  108. {
  109. $message = str_replace(':' . $key . ':', $value, $message);
  110. }
  111. return $message;
  112. }
  113. /**
  114. * Returns an originall exception object if such was provided
  115. *
  116. * @return type \Exception
  117. */
  118. public function getOriginalException()
  119. {
  120. return $this->originalException;
  121. }
  122. /**
  123. * Returns an array of data to be displayed when exception occured
  124. *
  125. * @return type array
  126. */
  127. public function getDetailsToDisplay()
  128. {
  129. $errorDetails = [];
  130. if ($this->isDebugOn() && $this->isAdminLogedIn())
  131. {
  132. $errorDetails['errorCode'] = $this->getMgCode();
  133. $errorDetails['errorToken'] = $this->getMgToken();
  134. $errorDetails['errorTime'] = $this->getMgTime();
  135. }
  136. $errorDetails['errorMessage'] = $this->getMgMessage(true);
  137. return $errorDetails;
  138. }
  139. /**
  140. * Returns an array of data to be logged when exception occured
  141. *
  142. * @return type array
  143. */
  144. public function getDetailsToLog()
  145. {
  146. $errorDetails = [];
  147. $errorDetails['errorCode'] = $this->getMgCode();
  148. $errorDetails['errorToken'] = $this->getMgToken();
  149. $errorDetails['errorTime'] = $this->getMgTime();
  150. $errorDetails['errorMessage'] = $this->getMgMessage(false);
  151. $errorDetails['additionalData'] = $this->getAdditionalData();
  152. return $errorDetails;
  153. }
  154. /**
  155. * Select a proper message for the exepction
  156. * Priority:
  157. * 1 custom message
  158. * 2 original Exception message
  159. * 3 error code message
  160. *
  161. * @return type string
  162. */
  163. protected function selectProperMessage()
  164. {
  165. if (is_string($this->customMessage))
  166. {
  167. return $this->customMessage;
  168. }
  169. if ($this->originalException !== null)
  170. {
  171. return $this->originalException->getMessage();
  172. }
  173. return $this->errorCode->getMessage();
  174. }
  175. /**
  176. * Sets a $originalException param, so you can wrap other exception in this one,
  177. * in order to log and parse them automatically
  178. *
  179. * @param \Exception $originalException
  180. */
  181. public function setOriginalException($originalException)
  182. {
  183. if ($originalException instanceof \Exception)
  184. {
  185. $this->originalException = $originalException;
  186. parent::__construct($originalException->getMessage(), $originalException->getCode(), $originalException->getPrevious());
  187. }
  188. }
  189. /**
  190. *
  191. * @param type $data array
  192. * @return $this
  193. */
  194. public function setAdditionalData($data = [])
  195. {
  196. if (is_array($data))
  197. {
  198. $this->additionalData = $data;
  199. }
  200. return $this;
  201. }
  202. /**
  203. *
  204. * @return type array
  205. */
  206. public function getAdditionalData()
  207. {
  208. return $this->additionalData;
  209. }
  210. /**
  211. *
  212. * @param type $data array
  213. * @return $this
  214. */
  215. public function setToTranslate($data = [])
  216. {
  217. if (is_array($data))
  218. {
  219. $this->toTranslate = $data;
  220. }
  221. return $this;
  222. }
  223. /**
  224. *
  225. * @param type $message string
  226. * @return $this
  227. */
  228. public function setCustomMessage($message = null)
  229. {
  230. if (is_string($message) && $message !== '')
  231. {
  232. $this->customMessage = $message;
  233. }
  234. return $this;
  235. }
  236. /**
  237. * Check if the exception should be logged or not
  238. *
  239. * @return boolean
  240. */
  241. public function isLogable()
  242. {
  243. if ($this->errorCode->isLogable())
  244. {
  245. return true;
  246. }
  247. if ($this->isAdminLogedIn() && $this->isDebugOn())
  248. {
  249. return true;
  250. }
  251. return false;
  252. }
  253. /**
  254. * Check if the administrator user is logged in current session
  255. *
  256. * @return boolean
  257. */
  258. public function isAdminLogedIn()
  259. {
  260. $this->loadRequestObj();
  261. $adminId = $this->request->getSession('adminid');
  262. if (is_int($adminId) && $adminId > 0)
  263. {
  264. return true;
  265. }
  266. return false;
  267. }
  268. }