Exception.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * @package TeamSpeak3
  20. * @author Sven 'ScP' Paulsen
  21. * @copyright Copyright (c) Planet TeamSpeak. All rights reserved.
  22. */
  23. /**
  24. * @class TeamSpeak3_Exception
  25. * @brief Enhanced exception class for TeamSpeak3 objects.
  26. */
  27. class TeamSpeak3_Exception extends Exception
  28. {
  29. /**
  30. * Stores the original error code.
  31. *
  32. * @var integer
  33. */
  34. protected $raw_code = 0x00;
  35. /**
  36. * Stores the original error message.
  37. *
  38. * @var string
  39. */
  40. protected $raw_mesg = null;
  41. /**
  42. * Stores custom error messages.
  43. *
  44. * @var array
  45. */
  46. protected static $messages = array();
  47. /**
  48. * The TeamSpeak3_Exception constructor.
  49. *
  50. * @param string $mesg
  51. * @param integer $code
  52. * @return TeamSpeak3_Exception
  53. */
  54. public function __construct($mesg, $code = 0x00)
  55. {
  56. parent::__construct($mesg, $code);
  57. $this->raw_code = $code;
  58. $this->raw_mesg = $mesg;
  59. if(array_key_exists((int) $code, self::$messages))
  60. {
  61. $this->message = $this->prepareCustomMessage(self::$messages[intval($code)]);
  62. }
  63. TeamSpeak3_Helper_Signal::getInstance()->emit("errorException", $this);
  64. }
  65. /**
  66. * Prepares a custom error message by replacing pre-defined signs with given values.
  67. *
  68. * @param TeamSpeak3_Helper_String $mesg
  69. * @return TeamSpeak3_Helper_String
  70. */
  71. protected function prepareCustomMessage(TeamSpeak3_Helper_String $mesg)
  72. {
  73. $args = array(
  74. "code" => $this->getCode(),
  75. "mesg" => $this->getMessage(),
  76. "line" => $this->getLine(),
  77. "file" => $this->getFile(),
  78. );
  79. return $mesg->arg($args)->toString();
  80. }
  81. /**
  82. * Registers a custom error message to $code.
  83. *
  84. * @param integer $code
  85. * @param string $mesg
  86. * @throws TeamSpeak3_Exception
  87. * @return void
  88. */
  89. public static function registerCustomMessage($code, $mesg)
  90. {
  91. if(array_key_exists((int) $code, self::$messages))
  92. {
  93. throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " is already registered");
  94. }
  95. if(!is_string($mesg))
  96. {
  97. throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " must be a string");
  98. }
  99. self::$messages[(int) $code] = new TeamSpeak3_Helper_String($mesg);
  100. }
  101. /**
  102. * Unregisters a custom error message from $code.
  103. *
  104. * @param integer $code
  105. * @throws TeamSpeak3_Exception
  106. * @return void
  107. */
  108. public static function unregisterCustomMessage($code)
  109. {
  110. if(!array_key_exists((int) $code, self::$messages))
  111. {
  112. throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " is not registered");
  113. }
  114. unset(self::$messages[(int) $code]);
  115. }
  116. /**
  117. * Returns the original error code.
  118. *
  119. * @return integer
  120. */
  121. public function getRawCode()
  122. {
  123. return $this->raw_code;
  124. }
  125. /**
  126. * Returns the original error message.
  127. *
  128. * @return integer
  129. */
  130. public function getRawMessage()
  131. {
  132. return $this->raw_mesg;
  133. }
  134. /**
  135. * Returns the class from which the exception was thrown.
  136. *
  137. * @return string
  138. */
  139. public function getSender()
  140. {
  141. $trace = $this->getTrace();
  142. return (isset($trace[0]["class"])) ? $trace[0]["class"] : "{main}";
  143. }
  144. }