Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing exception class not found #912

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions libs/ts3_lib/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,146 @@
* @copyright Copyright (c) Planet TeamSpeak. All rights reserved.
*/

/**
* @class TeamSpeak3_Exception
* @brief Enhanced exception class for TeamSpeak3 objects.
*/
class TeamSpeak3_Exception extends Exception
{
/**
* Stores the original error code.
*
* @var integer
*/
protected $raw_code = 0x00;

/**
* Stores the original error message.
*
* @var string
*/
protected $raw_mesg = null;

/**
* Stores custom error messages.
*
* @var array
*/
protected static $messages = array();

/**
* The TeamSpeak3_Exception constructor.
*
* @param string $mesg
* @param integer $code
* @return TeamSpeak3_Exception
*/
public function __construct($mesg, $code = 0x00)
{
parent::__construct($mesg, $code);

$this->raw_code = $code;
$this->raw_mesg = $mesg;

if(array_key_exists((int) $code, self::$messages))
{
$this->message = $this->prepareCustomMessage(self::$messages[intval($code)]);
}

TeamSpeak3_Helper_Signal::getInstance()->emit("errorException", $this);
}

/**
* Prepares a custom error message by replacing pre-defined signs with given values.
*
* @param TeamSpeak3_Helper_String $mesg
* @return TeamSpeak3_Helper_String
*/
protected function prepareCustomMessage(TeamSpeak3_Helper_String $mesg)
{
$args = array(
"code" => $this->getCode(),
"mesg" => $this->getMessage(),
"line" => $this->getLine(),
"file" => $this->getFile(),
);

return $mesg->arg($args)->toString();
}

/**
* Registers a custom error message to $code.
*
* @param integer $code
* @param string $mesg
* @throws TeamSpeak3_Exception
* @return void
*/
public static function registerCustomMessage($code, $mesg)
{
if(array_key_exists((int) $code, self::$messages))
{
throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " is already registered");
}

if(!is_string($mesg))
{
throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " must be a string");
}

self::$messages[(int) $code] = new TeamSpeak3_Helper_String($mesg);
}

/**
* Unregisters a custom error message from $code.
*
* @param integer $code
* @throws TeamSpeak3_Exception
* @return void
*/
public static function unregisterCustomMessage($code)
{
if(!array_key_exists((int) $code, self::$messages))
{
throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " is not registered");
}

unset(self::$messages[(int) $code]);
}

/**
* Returns the original error code.
*
* @return integer
*/
public function getRawCode()
{
return $this->raw_code;
}

/**
* Returns the original error message.
*
* @return integer
*/
public function getRawMessage()
{
return $this->raw_mesg;
}

/**
* Returns the class from which the exception was thrown.
*
* @return string
*/
public function getSender()
{
$trace = $this->getTrace();

return (isset($trace[0]["class"])) ? $trace[0]["class"] : "{main}";
}
}

/**
* @class TeamSpeak3_Helper_Exception
* @brief Enhanced exception class for TeamSpeak3_Helper_* objects.
Expand Down