1 | <?php |
||
2 | /* |
||
3 | * SPDX-License-Identifier: AGPL-3.0-only |
||
4 | * SPDX-FileCopyrightText: Copyright 2005-2016 Zarafa Deutschland GmbH |
||
5 | * SPDX-FileCopyrightText: Copyright 2020-2024 grommunio GmbH |
||
6 | */ |
||
7 | |||
8 | /** |
||
9 | * Defines a base exception class for all custom exceptions, so every exceptions that |
||
10 | * is thrown/caught by this application should extend this base class and make use of it. |
||
11 | * |
||
12 | * Some basic function of Exception class |
||
13 | * getMessage()- message of exception |
||
14 | * getCode() - code of exception |
||
15 | * getFile() - source filename |
||
16 | * getLine() - source line |
||
17 | * getTrace() - n array of the backtrace() |
||
18 | * getTraceAsString() - formatted string of trace |
||
19 | */ |
||
20 | class BaseException extends Exception { |
||
21 | /** |
||
22 | * Base name of the file, so we don't have to use static path of the file. |
||
23 | * |
||
24 | * @var null|string |
||
25 | */ |
||
26 | private $baseFile; |
||
27 | |||
28 | /** |
||
29 | * Flag to check if exception is already handled or not. |
||
30 | * |
||
31 | * @var bool |
||
32 | */ |
||
33 | public $isHandled = false; |
||
34 | |||
35 | /** |
||
36 | * The exception message to show at client side. |
||
37 | * |
||
38 | * @var null|string |
||
39 | */ |
||
40 | public $displayMessage; |
||
41 | |||
42 | /** |
||
43 | * Flag for allow to exception details message or not. |
||
44 | * |
||
45 | * @var false |
||
46 | */ |
||
47 | public $allowToShowDetailsMessage = false; |
||
48 | |||
49 | /** |
||
50 | * The exception title to show as a message box title at client side. |
||
51 | * |
||
52 | * @var null|string |
||
53 | */ |
||
54 | public $title; |
||
55 | |||
56 | /** |
||
57 | * The notification type by which exception needs to be shown at client side. |
||
58 | */ |
||
59 | public $notificationType = ""; |
||
60 | |||
61 | /** |
||
62 | * Construct the exception. |
||
63 | * |
||
64 | * @param string $errorMessage |
||
65 | * @param int $code |
||
66 | * @param Throwable $previous |
||
67 | * @param string $displayMessage |
||
68 | */ |
||
69 | public function __construct($errorMessage, $code = 0, $previous = null, $displayMessage = null) { |
||
70 | // assign display message |
||
71 | $this->displayMessage = $displayMessage; |
||
72 | |||
73 | parent::__construct($errorMessage, (int) $code, $previous); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return string returns file name and line number combined where exception occurred |
||
78 | */ |
||
79 | public function getFileLine(): string { |
||
80 | return $this->getBaseFile() . ':' . $this->getLine(); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return string returns message that should be sent to client to display |
||
85 | */ |
||
86 | public function getDisplayMessage() { |
||
87 | if (!is_null($this->displayMessage)) { |
||
88 | return $this->displayMessage; |
||
89 | } |
||
90 | |||
91 | return $this->getMessage(); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Function sets display message of an exception that will be sent to the client side |
||
96 | * to show it to user. |
||
97 | * |
||
98 | * @param string $message display message |
||
99 | */ |
||
100 | public function setDisplayMessage($message): void { |
||
101 | $this->displayMessage = $message . " (" . mapi_strerror($this->getCode()) . ")"; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Function sets title of an exception that will be sent to the client side |
||
106 | * to show it to user. |
||
107 | * |
||
108 | * @param string $title title of an exception |
||
109 | */ |
||
110 | public function setTitle($title): void { |
||
111 | $this->title = $title; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @return null|string returns title that should be sent to client to display as a message box title |
||
116 | */ |
||
117 | public function getTitle() { |
||
118 | return $this->title; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Function sets a flag in exception class to indicate that exception is already handled |
||
123 | * so if it is caught again in the top level of function stack then we have to silently |
||
124 | * ignore it. |
||
125 | */ |
||
126 | public function setHandled(): void { |
||
127 | $this->isHandled = true; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return string returns base path of the file where exception occurred |
||
132 | */ |
||
133 | public function getBaseFile() { |
||
134 | if (is_null($this->baseFile)) { |
||
135 | $this->baseFile = basename(parent::getFile()); |
||
136 | } |
||
137 | |||
138 | return $this->baseFile; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Name of the class of exception. |
||
143 | * |
||
144 | * @return get-class-of<$this, BaseException&static> |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
145 | */ |
||
146 | public function getName(): string { |
||
147 | return get_class($this); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Function sets a type of notification by which exception needs to be shown at client side. |
||
152 | * |
||
153 | * @param string $notificationType type of notification to show an exception |
||
154 | */ |
||
155 | public function setNotificationType($notificationType) { |
||
156 | $this->notificationType = $notificationType; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @return string a type of notification to show an exception |
||
161 | */ |
||
162 | public function getNotificationType() { |
||
163 | return $this->notificationType; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * It will return details error message if allowToShowDetailsMessage is set. |
||
168 | * |
||
169 | * @return string returns details error message |
||
170 | */ |
||
171 | public function getDetailsMessage() { |
||
172 | return $this->allowToShowDetailsMessage ? $this->__toString() : ''; |
||
173 | } |
||
174 | |||
175 | // @TODO getTrace and getTraceAsString |
||
176 | } |
||
177 |