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