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