|
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
|
|
|
* Construct the exception |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $errorMessage |
|
46
|
|
|
* @param int $code |
|
47
|
|
|
* @param Exception $previous |
|
48
|
|
|
* @param string $displayMessage |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct($errorMessage, $code = 0, Exception $previous = null, $displayMessage = null) |
|
52
|
|
|
{ |
|
53
|
|
|
// assign display message |
|
54
|
|
|
$this->displayMessage = $displayMessage; |
|
55
|
|
|
|
|
56
|
|
|
parent::__construct($errorMessage, (int) $code, $previous); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string returns file name and line number combined where exception occurred. |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getFileLine() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->getBaseFile() . ':' . $this->getLine(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string returns message that should be sent to client to display |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getDisplayMessage() |
|
71
|
|
|
{ |
|
72
|
|
|
if(!is_null($this->displayMessage)) { |
|
73
|
|
|
return $this->displayMessage; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this->getMessage(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Function sets display message of an exception that will be sent to the client side |
|
81
|
|
|
* to show it to user. |
|
82
|
|
|
* @param string $message display message. |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setDisplayMessage($message) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->displayMessage = $message . " (" . mapi_strerror($this->getCode()) . ")"; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Function sets title of an exception that will be sent to the client side |
|
91
|
|
|
* to show it to user. |
|
92
|
|
|
* @param string $title title of an exception. |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setTitle($title) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->title = $title; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return string returns title that should be sent to client to display as a message box |
|
101
|
|
|
* title. |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getTitle() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->title; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Function sets a flag in exception class to indicate that exception is already handled |
|
110
|
|
|
* so if it is caught again in the top level of function stack then we have to silently |
|
111
|
|
|
* ignore it. |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setHandled() |
|
114
|
|
|
{ |
|
115
|
|
|
$this->isHandled = true; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return string returns base path of the file where exception occurred. |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getBaseFile() |
|
122
|
|
|
{ |
|
123
|
|
|
if(is_null($this->baseFile)) { |
|
124
|
|
|
$this->baseFile = basename(parent::getFile()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $this->baseFile; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Name of the class of exception. |
|
132
|
|
|
* |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getName() |
|
136
|
|
|
{ |
|
137
|
|
|
return get_class($this); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* It will return details error message if allowToShowDetailsMessage is set. |
|
142
|
|
|
* |
|
143
|
|
|
* @return string returns details error message. |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getDetailsMessage() |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->allowToShowDetailsMessage ? $this->__toString() : ''; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
// @TODO getTrace and getTraceAsString |
|
151
|
|
|
} |
|
152
|
|
|
?> |
|
|
|
|
|
|
153
|
|
|
|
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.