Exception.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\Core\HandlerError\Exceptions;
  3. /**
  4. * Base module Exception type
  5. *
  6. * @author Sławomir Miśkowicz <rafal.os@modulesgarden.com>
  7. */
  8. class Exception extends \Exception
  9. {
  10. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\Traits\ErrorCodesLibrary;
  11. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\Traits\Lang;
  12. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\Traits\IsDebugOn;
  13. use \ModulesGarden\Servers\ProxmoxCloudVps\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 \ModulesGarden\Servers\ProxmoxCloudVps\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. logModuleCall(
  52. 'proxmoxCloud',
  53. __FUNCTION__,
  54. $errorCode,
  55. 'Debug Error',
  56. $this
  57. );
  58. $this->errorCode = $this->genErrorCode(($errorCode ? : self::DEFAULT_ERROR_CODE));
  59. $this->setAdditionalData($additionalData);
  60. $this->setToTranslate($toTranslate);
  61. $this->setOriginalException($originalException);
  62. }
  63. /**
  64. * Returns an error code for the exception
  65. * @return type string
  66. */
  67. public function getMgCode()
  68. {
  69. return $this->errorCode->getCode();
  70. }
  71. /**
  72. * Returns an error token for the exception, an unique string based on exception occurence timestamp
  73. * @return type string
  74. */
  75. public function getMgToken()
  76. {
  77. return $this->errorCode->getToken();
  78. }
  79. /**
  80. * Returns a date for the exception occurence
  81. * @return type string
  82. */
  83. public function getMgTime()
  84. {
  85. return date("Y-m-d H:i:s", time());
  86. }
  87. /**
  88. * Returns a translated or raw error message
  89. * @param type bool $translate
  90. * @return type string
  91. */
  92. public function getMgMessage($translate = true)
  93. {
  94. if ($translate)
  95. {
  96. $this->loadLang();
  97. $message = $this->lang->absoluteTranslate(
  98. $this->errorCode === self::DEFAULT_ERROR_CODE ? 'errorMessage' : 'errorCodeMessage',
  99. $this->selectProperMessage());
  100. }
  101. else
  102. {
  103. $message = $this->selectProperMessage();
  104. }
  105. return $this->replaceMessageVars($message);
  106. }
  107. /**
  108. * Replaces provided vars in the message string
  109. *
  110. * @param type $message string
  111. */
  112. public function replaceMessageVars($message)
  113. {
  114. foreach ($this->toTranslate as $key => $value)
  115. {
  116. $message = str_replace(':' . $key . ':', $value, $message);
  117. }
  118. return $message;
  119. }
  120. /**
  121. * Returns an originall exception object if such was provided
  122. *
  123. * @return type \Exception
  124. */
  125. public function getOriginalException()
  126. {
  127. return $this->originalException;
  128. }
  129. /**
  130. * Returns an array of data to be displayed when exception occured
  131. *
  132. * @return type array
  133. */
  134. public function getDetailsToDisplay()
  135. {
  136. $errorDetails = [];
  137. if ($this->isDebugOn() && $this->isAdminLogedIn())
  138. {
  139. $errorDetails['errorCode'] = $this->getMgCode();
  140. $errorDetails['errorToken'] = $this->getMgToken();
  141. $errorDetails['errorTime'] = $this->getMgTime();
  142. }
  143. $errorDetails['errorMessage'] = $this->getMgMessage(true);
  144. return $errorDetails;
  145. }
  146. /**
  147. * Returns an array of data to be logged when exception occured
  148. *
  149. * @return type array
  150. */
  151. public function getDetailsToLog()
  152. {
  153. $errorDetails = [];
  154. $errorDetails['errorCode'] = $this->getMgCode();
  155. $errorDetails['errorToken'] = $this->getMgToken();
  156. $errorDetails['errorTime'] = $this->getMgTime();
  157. $errorDetails['errorMessage'] = $this->getMgMessage(false);
  158. $errorDetails['additionalData'] = $this->getAdditionalData();
  159. return $errorDetails;
  160. }
  161. /**
  162. * Select a proper message for the exepction
  163. * Priority:
  164. * 1 custom message
  165. * 2 original Exception message
  166. * 3 error code message
  167. *
  168. * @return type string
  169. */
  170. protected function selectProperMessage()
  171. {
  172. if (is_string($this->customMessage))
  173. {
  174. return $this->customMessage;
  175. }
  176. if ($this->originalException !== null)
  177. {
  178. return $this->originalException->getMessage();
  179. }
  180. return $this->errorCode->getMessage();
  181. }
  182. /**
  183. * Sets a $originalException param, so you can wrap other exception in this one,
  184. * in order to log and parse them automatically
  185. *
  186. * @param \Exception $originalException
  187. */
  188. public function setOriginalException($originalException)
  189. {
  190. if ($originalException instanceof \Exception)
  191. {
  192. $this->originalException = $originalException;
  193. parent::__construct($originalException->getMessage(), $originalException->getCode(), $originalException->getPrevious());
  194. }
  195. }
  196. /**
  197. *
  198. * @param type $data array
  199. * @return $this
  200. */
  201. public function setAdditionalData($data = [])
  202. {
  203. if (is_array($data))
  204. {
  205. $this->additionalData = $data;
  206. }
  207. return $this;
  208. }
  209. /**
  210. *
  211. * @return type array
  212. */
  213. public function getAdditionalData()
  214. {
  215. return $this->additionalData;
  216. }
  217. /**
  218. *
  219. * @param type $data array
  220. * @return $this
  221. */
  222. public function setToTranslate($data = [])
  223. {
  224. if (is_array($data))
  225. {
  226. $this->toTranslate = $data;
  227. }
  228. return $this;
  229. }
  230. /**
  231. *
  232. * @param type $message string
  233. * @return $this
  234. */
  235. public function setCustomMessage($message = null)
  236. {
  237. if (is_string($message) && $message !== '')
  238. {
  239. $this->customMessage = $message;
  240. }
  241. return $this;
  242. }
  243. /**
  244. * Check if the exception should be logged or not
  245. *
  246. * @return boolean
  247. */
  248. public function isLogable()
  249. {
  250. if ($this->errorCode->isLogable())
  251. {
  252. return true;
  253. }
  254. if ($this->isAdminLogedIn() && $this->isDebugOn())
  255. {
  256. return true;
  257. }
  258. return false;
  259. }
  260. /**
  261. * Check if the administrator user is logged in current session
  262. *
  263. * @return boolean
  264. */
  265. public function isAdminLogedIn()
  266. {
  267. $this->loadRequestObj();
  268. $adminId = $this->request->getSession('adminid');
  269. if (is_int($adminId) && $adminId > 0)
  270. {
  271. return true;
  272. }
  273. return false;
  274. }
  275. }