class.baseexception.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /*
  3. * Copyright 2005 - 2016 Zarafa and its licensors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License, version 3,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Affero General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. ?>
  19. <?php
  20. /**
  21. * Defines a base exception class for all custom exceptions, so every exceptions that
  22. * is thrown/caught by this application should extend this base class and make use of it.
  23. *
  24. * Some basic function of Exception class
  25. * getMessage()- message of exception
  26. * getCode() - code of exception
  27. * getFile() - source filename
  28. * getLine() - source line
  29. * getTrace() - n array of the backtrace()
  30. * getTraceAsString() - formated string of trace
  31. */
  32. class BaseException extends Exception {
  33. /**
  34. * Base name of the file, so we don't have to use static path of the file
  35. */
  36. private $baseFile = null;
  37. /**
  38. * Flag to check if exception is already handled or not
  39. */
  40. public $isHandled = false;
  41. /**
  42. * The exception message to show at client side.
  43. */
  44. public $displayMessage = null;
  45. /**
  46. * Construct the exception
  47. *
  48. * @param string $errorMessage
  49. * @param int $code
  50. * @param Exception $previous
  51. * @param string $displayMessage
  52. * @return void
  53. */
  54. public function __construct($errorMessage, $code = 0, Exception $previous = null, $displayMessage = null) {
  55. // assign display message
  56. $this->displayMessage = $displayMessage;
  57. parent::__construct($errorMessage, (int) $code, $previous);
  58. }
  59. /**
  60. * @return string returns file name and line number combined where exception occurred.
  61. */
  62. public function getFileLine()
  63. {
  64. return $this->getBaseFile() . ':' . $this->getLine();
  65. }
  66. /**
  67. * @return string returns message that should be sent to client to display
  68. */
  69. public function getDisplayMessage()
  70. {
  71. if(!is_null($this->displayMessage)) {
  72. return $this->displayMessage;
  73. }
  74. return $this->getMessage();
  75. }
  76. /**
  77. * Function sets display message of an exception that will be sent to the client side
  78. * to show it to user.
  79. * @param string $message display message.
  80. */
  81. public function setDisplayMessage($message)
  82. {
  83. $this->displayMessage = $message;
  84. }
  85. /**
  86. * Function sets a flag in exception class to indicate that exception is already handled
  87. * so if it is caught again in the top level of function stack then we have to silently
  88. * ignore it.
  89. */
  90. public function setHandled()
  91. {
  92. $this->isHandled = true;
  93. }
  94. /**
  95. * @return string returns base path of the file where exception occurred.
  96. */
  97. public function getBaseFile()
  98. {
  99. if(is_null($this->baseFile)) {
  100. $this->baseFile = basename(parent::getFile());
  101. }
  102. return $this->baseFile;
  103. }
  104. /**
  105. * Function will check for PHP version if it is greater than 5.3 then we can use its default implementation
  106. * otherwise we have to use our own implementation of chanining functionality.
  107. *
  108. * @return Exception returns previous exception
  109. */
  110. public function _getPrevious()
  111. {
  112. if (version_compare(PHP_VERSION, '5.3.0', '<'))
  113. return $this->_previous;
  114. else
  115. return parent::getPrevious();
  116. }
  117. /**
  118. * String representation of the exception, also handles previous exception.
  119. *
  120. * @return string
  121. */
  122. public function __toString()
  123. {
  124. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  125. if (($e = $this->getPrevious()) !== null) {
  126. return $e->__toString()
  127. . "\n\nNext "
  128. . parent::__toString();
  129. }
  130. }
  131. return parent::__toString();
  132. }
  133. /**
  134. * Name of the class of exception.
  135. *
  136. * @return string
  137. */
  138. public function getName()
  139. {
  140. return get_class($this);
  141. }
  142. // @TODO getTrace and getTraceAsString
  143. }
  144. ?>