grommunio /
grommunio-web
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Defines a base exception class for all custom exceptions, so every exceptions that |
||
| 4 | * is thrown/caught by this application should extend this base class and make use of it. |
||
| 5 | * |
||
| 6 | * Some basic function of Exception class |
||
| 7 | * getMessage()- message of exception |
||
| 8 | * getCode() - code of exception |
||
| 9 | * getFile() - source filename |
||
| 10 | * getLine() - source line |
||
| 11 | * getTrace() - n array of the backtrace() |
||
| 12 | * getTraceAsString() - formatted string of trace |
||
| 13 | */ |
||
| 14 | class BaseException extends Exception |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Base name of the file, so we don't have to use static path of the file |
||
| 18 | */ |
||
| 19 | private $baseFile = null; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Flag to check if exception is already handled or not |
||
| 23 | */ |
||
| 24 | public $isHandled = false; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The exception message to show at client side. |
||
| 28 | */ |
||
| 29 | public $displayMessage = null; |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * Flag for allow to exception details message or not |
||
| 34 | */ |
||
| 35 | public $allowToShowDetailsMessage = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The exception title to show as a message box title at client side. |
||
| 39 | */ |
||
| 40 | public $title = null; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The notification type by which exception needs to be shown at client side. |
||
| 44 | */ |
||
| 45 | public $notificationType = ""; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Construct the exception |
||
| 49 | * |
||
| 50 | * @param string $errorMessage |
||
| 51 | * @param int $code |
||
| 52 | * @param Exception $previous |
||
| 53 | * @param string $displayMessage |
||
| 54 | * @return void |
||
| 55 | */ |
||
| 56 | public function __construct($errorMessage, $code = 0, Exception $previous = null, $displayMessage = null) |
||
| 57 | { |
||
| 58 | // assign display message |
||
| 59 | $this->displayMessage = $displayMessage; |
||
| 60 | |||
| 61 | parent::__construct($errorMessage, (int) $code, $previous); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return string returns file name and line number combined where exception occurred. |
||
| 66 | */ |
||
| 67 | public function getFileLine() |
||
| 68 | { |
||
| 69 | return $this->getBaseFile() . ':' . $this->getLine(); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return string returns message that should be sent to client to display |
||
| 74 | */ |
||
| 75 | public function getDisplayMessage() |
||
| 76 | { |
||
| 77 | if(!is_null($this->displayMessage)) { |
||
| 78 | return $this->displayMessage; |
||
| 79 | } |
||
| 80 | |||
| 81 | return $this->getMessage(); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Function sets display message of an exception that will be sent to the client side |
||
| 86 | * to show it to user. |
||
| 87 | * @param string $message display message. |
||
| 88 | */ |
||
| 89 | public function setDisplayMessage($message) |
||
| 90 | { |
||
| 91 | $this->displayMessage = $message; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Function sets title of an exception that will be sent to the client side |
||
| 96 | * to show it to user. |
||
| 97 | * @param string $title title of an exception. |
||
| 98 | */ |
||
| 99 | public function setTitle($title) |
||
| 100 | { |
||
| 101 | $this->title = $title; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return string returns title that should be sent to client to display as a message box |
||
| 106 | * title. |
||
| 107 | */ |
||
| 108 | public function getTitle() |
||
| 109 | { |
||
| 110 | return $this->title; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Function sets a flag in exception class to indicate that exception is already handled |
||
| 115 | * so if it is caught again in the top level of function stack then we have to silently |
||
| 116 | * ignore it. |
||
| 117 | */ |
||
| 118 | public function setHandled() |
||
| 119 | { |
||
| 120 | $this->isHandled = true; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return string returns base path of the file where exception occurred. |
||
| 125 | */ |
||
| 126 | public function getBaseFile() |
||
| 127 | { |
||
| 128 | if(is_null($this->baseFile)) { |
||
| 129 | $this->baseFile = basename(parent::getFile()); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $this->baseFile; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Name of the class of exception. |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function getName() |
||
| 141 | { |
||
| 142 | return get_class($this); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Function sets a type of notification by which exception needs to be shown at client side. |
||
| 147 | * @param string $notificationType type of notification to show an exception. |
||
| 148 | */ |
||
| 149 | public function setNotificationType($notificationType) |
||
| 150 | { |
||
| 151 | $this->notificationType = $notificationType; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return string a type of notification to show an exception. |
||
| 156 | */ |
||
| 157 | public function getNotifiactionType() |
||
| 158 | { |
||
| 159 | return $this->notificationType; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Function returns the JSON request as a string from the backtrace. |
||
| 164 | * |
||
| 165 | * @return string The JSON request as a string |
||
| 166 | */ |
||
| 167 | private function getJSONRequest() |
||
| 168 | { |
||
| 169 | $jsonrequest = false; |
||
| 170 | foreach ($this->getTrace() as $frame) { |
||
| 171 | if (in_array('JSONRequest', $frame)) { |
||
| 172 | // If there are multiple requests, we print each one on a new line |
||
| 173 | $jsonrequest = implode(PHP_EOL, $frame['args']); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | return $jsonrequest; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * If allowToShowDetailsMessage is set, function will return a detailed error message |
||
| 181 | * with the MAPIException code, e.g. 'MAPI_E_NO_ACCESS', the JSONRequest and the backtrace as string. |
||
| 182 | * |
||
| 183 | * @return string returns details error message. |
||
| 184 | */ |
||
| 185 | public function getDetailsMessage() |
||
| 186 | { |
||
| 187 | $request = $this->getJSONRequest(); |
||
| 188 | $message = 'MAPIException Code [' . get_mapi_error_name($this->getCode()) . ']' |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 189 | . PHP_EOL . PHP_EOL . 'MAPIException in ' . $this->getFile() . ':' . $this->getLine() |
||
| 190 | . PHP_EOL . PHP_EOL . 'Request:' |
||
| 191 | . PHP_EOL . PHP_EOL . $request === false ? _('Request is not available.') : $request |
||
| 192 | . PHP_EOL . PHP_EOL . 'Stack trace:' |
||
| 193 | . PHP_EOL . PHP_EOL . $this->getTraceAsString(); |
||
| 194 | |||
| 195 | return $this->allowToShowDetailsMessage ? $message : ''; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | ?> |
||
|
0 ignored issues
–
show
It is not recommended to use PHP's closing tag
?> in files other than templates.
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore. A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever. Loading history...
|
|||
| 199 |