1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* In this file the exception class '\UK\CoreException' is defined. |
4
|
|
|
* |
5
|
|
|
* @author UniKado <[email protected]> |
6
|
|
|
* @copyright (c) 2016, UniKado |
7
|
|
|
* @package UK |
8
|
|
|
* @since 2016-06-24 |
9
|
|
|
* @subpackage Errors |
10
|
|
|
* @version 0.1.0 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
declare( strict_types = 1 ); |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
namespace UK; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* <b>ATTENTION!</b> Never throw or use this exception inside your code! |
22
|
|
|
* |
23
|
|
|
* @since v0.1.0 |
24
|
|
|
*/ |
25
|
|
|
class CoreException extends \Exception |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
// <editor-fold desc="// = = = = P U B L I C S T A T I C F I E L D S = = = = = = = = = = = = = = = = = = ="> |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* If this value is set to true Notices will trigger an exception |
33
|
|
|
* |
34
|
|
|
* @type bool |
35
|
|
|
*/ |
36
|
|
|
public static $debug = false; |
37
|
|
|
|
38
|
|
|
// </editor-fold> |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
// <editor-fold desc="// = = = = P U B L I C C O N S T R U C T O R = = = = = = = = = = = = = = = = = = = = ="> |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Init's a new instance. |
45
|
|
|
* |
46
|
|
|
* @param string $message The error message |
47
|
|
|
* @param int|string $code The optional error code. (defaults to \E_ERROR) |
48
|
|
|
* @param \Exception $previous Optional previous exception. |
49
|
|
|
*/ |
50
|
3 |
|
public function __construct( string $message, $code = \E_ERROR, \Exception $previous = null ) |
51
|
|
|
{ |
52
|
|
|
|
53
|
|
|
// Call the parent constructor (\Exception) |
54
|
3 |
|
parent::__construct( |
55
|
|
|
$message, |
56
|
|
|
$code, |
57
|
|
|
$previous |
58
|
|
|
); |
59
|
|
|
|
60
|
3 |
|
} |
61
|
|
|
|
62
|
|
|
// </editor-fold> |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
// <editor-fold desc="// = = = = P U B L I C M E T H O D S = = = = = = = = = = = = = = = = = = = = = = = = ="> |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Extends the origin getMessage method, so also previous messages are include, if defined. |
69
|
|
|
* |
70
|
|
|
* @param bool $appendPreviousByNewline If a prev. Exception is defined append it by a new line? (' ' other) |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
3 |
|
public function getErrorMessage( bool $appendPreviousByNewline = false ) : string |
74
|
|
|
{ |
75
|
|
|
|
76
|
|
|
// Getting a optional previous exception |
77
|
3 |
|
$prev = $this->getPrevious(); |
78
|
|
|
|
79
|
3 |
|
if ( \is_null( $prev ) ) |
80
|
|
|
{ |
81
|
|
|
// If no previous exception is used |
82
|
3 |
|
return \sprintf( |
83
|
3 |
|
'%s(%d): %s', |
84
|
3 |
|
static::GetCodeName( $this->getCode() ), |
85
|
3 |
|
$this->getCode(), |
86
|
3 |
|
$this->getMessage() |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Define the separator between current and previous exception. |
91
|
1 |
|
$separator = $appendPreviousByNewline ? "\n" : ' '; |
92
|
|
|
|
93
|
1 |
|
if ( ( $prev instanceof CoreException ) ) |
94
|
|
|
{ |
95
|
1 |
|
return \sprintf( |
96
|
1 |
|
'%s(%d): %s%s%s', |
97
|
1 |
|
static::GetCodeName( $this->getCode() ), |
98
|
1 |
|
$this->getCode(), |
99
|
1 |
|
$this->getMessage(), |
100
|
|
|
$separator, |
101
|
1 |
|
$prev->getErrorMessage( $appendPreviousByNewline ) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return \sprintf( |
106
|
|
|
'%s(%d): %s%s%s', |
107
|
|
|
static::GetCodeName( $this->getCode() ), |
108
|
|
|
$this->getCode(), |
109
|
|
|
$this->getMessage(), |
110
|
|
|
$separator, |
111
|
|
|
$prev->getMessage() |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Overrides __toString, to return a more detailed string, when object is casted to a string. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
3 |
|
public final function __toString() |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
|
124
|
3 |
|
return $this->toCustomString(); |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Allows the definition of the sub exception level if there is a parent exception that contains this exception. |
130
|
|
|
* |
131
|
|
|
* @param int $subExceptionLevel |
132
|
|
|
* @param string $indentSpaces Spaces to use for a single indention level. |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
3 |
|
public function toCustomString( int $subExceptionLevel = 0, string $indentSpaces = ' ' ) : string |
136
|
|
|
{ |
137
|
|
|
|
138
|
|
|
// Concatenate the base error message from usable elements |
139
|
3 |
|
$msg = \sprintf( |
140
|
3 |
|
"%s%s in %s[%d]. %s", |
141
|
|
|
\str_repeat( $indentSpaces, $subExceptionLevel ), |
142
|
|
|
\get_class( $this ), |
143
|
3 |
|
$this->file, |
144
|
3 |
|
$this->line, |
145
|
3 |
|
\str_replace( "\n", "\n" . \str_repeat( $indentSpaces, $subExceptionLevel ), $this->message ) |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
// getting a may defined previous exception |
149
|
3 |
|
$previous = $this->getPrevious(); |
150
|
|
|
|
151
|
|
|
// if no previous exception is defined return the current generated message |
152
|
3 |
|
if ( \is_null( $previous ) || ! ( $previous instanceof \Exception ) ) |
153
|
|
|
{ |
154
|
3 |
|
return $msg; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// If previous message is a framework internal exception |
158
|
1 |
|
if ( $previous instanceof CoreException ) |
159
|
|
|
{ |
160
|
|
|
|
161
|
|
|
// Simple cast the exception to a string and append it with rewrite the indention |
162
|
1 |
|
$msg .= "\n" . $previous->toCustomString( $subExceptionLevel + 1, $indentSpaces ); |
163
|
|
|
|
164
|
|
|
// And return the message |
165
|
1 |
|
return $msg; |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// Else its a normal PHP exception |
170
|
|
|
|
171
|
|
|
// Concatenate the previous error message from usable elements |
172
|
|
|
$msg .= \sprintf( |
173
|
|
|
"\n%s%s %s in %s[%d]\n %s", |
174
|
|
|
\str_repeat( $indentSpaces, $subExceptionLevel + 1 ), |
175
|
|
|
\get_class( $previous ), |
176
|
|
|
static::GetCodeName( $previous->getCode() ), |
177
|
|
|
$previous->file, |
178
|
|
|
$previous->line, |
179
|
|
|
\str_replace( "\n", "\n" . \str_repeat( $indentSpaces, $subExceptionLevel + 1 ), $previous->message ) |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
// And return the message |
183
|
|
|
return $msg; |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// </editor-fold> |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
// <editor-fold desc="// = = = = P U B L I C S T A T I C M E T H O D S = = = = = = = = = = = = = = = = = ="> |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Returns a string, representing the defined error code. |
194
|
|
|
* |
195
|
|
|
* @param int|string $code e.g.: \E_USER_ERROR |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
3 |
|
public static function GetCodeName( $code ) : string |
199
|
|
|
{ |
200
|
|
|
|
201
|
|
|
switch ( $code ) |
202
|
|
|
{ |
203
|
|
|
|
204
|
3 |
|
case \E_ERROR: |
205
|
1 |
|
case \E_USER_ERROR: |
206
|
2 |
|
return 'ERROR'; |
207
|
|
|
|
208
|
1 |
|
case \E_WARNING: |
209
|
1 |
|
case \E_USER_WARNING: |
210
|
|
|
return 'WARNING'; |
211
|
|
|
|
212
|
1 |
|
case \E_DEPRECATED: |
213
|
1 |
|
case \E_USER_DEPRECATED: |
214
|
|
|
return 'DEPRECATED'; |
215
|
|
|
|
216
|
1 |
|
case \E_NOTICE: |
217
|
|
|
case \E_USER_NOTICE: |
218
|
1 |
|
return 'NOTICE'; |
219
|
|
|
|
220
|
|
|
case \E_PARSE: |
221
|
|
|
return 'PARSE'; |
222
|
|
|
case \E_RECOVERABLE_ERROR: |
223
|
|
|
return 'RECOVERABLE ERROR'; |
224
|
|
|
|
225
|
|
|
case \E_STRICT: |
226
|
|
|
return 'STRICT'; |
227
|
|
|
|
228
|
|
|
default: |
229
|
|
|
if ( \is_string( $code ) ) |
230
|
|
|
{ |
231
|
|
|
return $code; |
232
|
|
|
} |
233
|
|
|
return 'OTHER'; |
234
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
// </editor-fold> |
240
|
|
|
|
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|