BaseException::setShowDetailsMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 2
rs 10
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
	 * The exception message to show at client side.
59
	 *
60
	 * @var null|string
61
	 */
62
	public $displayMessage;
63
64
	/**
65
	 * Constructs the exception.
66
	 */
67
	public function __construct(string $errorMessage, int $code = 0, ?Throwable $previous = null, ?string $displayMessage = null) {
68
		parent::__construct($errorMessage, (int) $code, $previous);
69
		$this->displayMessage = $displayMessage;
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
		return $this->displayMessage ?? $this->getMessage();
84
	}
85
86
	/**
87
	 * Sets display message of an exception that will be sent to the client side
88
	 * to show it to user.
89
	 */
90
	public function setDisplayMessage(string $message): void {
91
		$this->displayMessage = $message . " (" . mapi_strerror($this->getCode()) . ")";
92
	}
93
94
	/**
95
	 * Sets the  title of an exception that will be sent to the client side
96
	 * to show it to user.
97
	 */
98
	public function setTitle(string $title): void {
99
		$this->title = $title;
100
	}
101
102
	/**
103
	 * Returns title that should be sent to client to display as a message box title.
104
	 */
105
	public function getTitle(): ?string {
106
		return $this->title;
107
	}
108
109
	/**
110
	 * Sets a flag in exception class to indicate that exception is already handled
111
	 * so if it is caught again in the top level of function stack
112
	 * then we have to silently ignore it.
113
	 */
114
	public function setHandled(): void {
115
		$this->isHandled = true;
116
	}
117
118
	/**
119
	 * Returns whether this exception has been handled.
120
	 */
121
	public function isHandled(): bool {
122
		return $this->isHandled;
123
	}
124
125
	/**
126
	 * Returns base path of the file where exception occurred.
127
	 */
128
	public function getBaseFile(): string {
129
		return $this->baseFile ??= basename(parent::getFile());
130
	}
131
132
	/**
133
	 * Returns the name of the class of exception.
134
	 */
135
	public function getName(): string {
136
		return static::class;
137
	}
138
139
	/**
140
	 * Sets a type of notification by which exception needs to be shown at client side.
141
	 */
142
	public function setNotificationType(string $notificationType): void {
143
		$this->notificationType = $notificationType;
144
	}
145
146
	/**
147
	 * Returns a type of notification to show an exception.
148
	 */
149
	public function getNotificationType(): string {
150
		return $this->notificationType;
151
	}
152
153
	/**
154
	 * Sets the flag to allow showing detailed exception messages.
155
	 */
156
	public function setShowDetailsMessage(): void {
157
		$this->allowToShowDetailsMessage = true;
158
	}
159
160
	/**
161
	 * Returns details of the error message if allowToShowDetailsMessage is set.
162
	 * Includes MAPI error code, exception message, JSON request, and backtrace.
163
	 *
164
	 * @return string returns details error message
165
	 */
166
	public function getDetailsMessage(): string {
167
		if (!$this->allowToShowDetailsMessage) {
168
			return '';
169
		}
170
171
		return 'MAPIException Code [' . get_mapi_error_name($this->getCode()) . ']' .
172
			PHP_EOL . PHP_EOL . 'Message: ' . $this->getMessage() .
173
			PHP_EOL . PHP_EOL . 'Error: ' . mapi_strerror($this->getCode()) .
174
			PHP_EOL . PHP_EOL . 'BaseException in ' . $this->getFile() . ':' . $this->getLine() .
175
			PHP_EOL . PHP_EOL . 'Stack trace:' .
176
			PHP_EOL . PHP_EOL . $this->getTraceAsString();
177
	}
178
}
179