5
5
6
6
class Exception extends \Exception
7
7
{
8
+
9
+ /**
10
+ * @var ExceptionStore
11
+ */
8
12
private $ exceptionStore ;
9
13
10
14
const DEFAULT_ERROR_CODE = 'GENERIC_001 ' ;
11
-
12
15
const GENERIC_001 = 'GENERIC_001 ' ;
13
16
const GENERIC_001_MESSAGE = 'Unknown error ' ;
14
17
18
+ /**
19
+ * Exception constructor.
20
+ *
21
+ * @param string $detailedMessage
22
+ * @param null $code
23
+ * @param \Exception|null $previous
24
+ * @param ExceptionStore|null $exceptionStore
25
+ */
15
26
public function __construct ($ detailedMessage = "" , $ code = null , \Exception $ previous = null ,
16
27
ExceptionStore $ exceptionStore =null )
17
28
{
@@ -28,6 +39,16 @@ public function __construct($detailedMessage = "", $code = null, \Exception $pre
28
39
$ this ->setup ($ code , $ message , null , $ detailedMessage , $ exceptionStore );
29
40
}
30
41
42
+ /**
43
+ * Setups a new internal exception store on the exception class
44
+ * This exception store is built from the arguments of this method call
45
+ *
46
+ * @param $code
47
+ * @param $message
48
+ * @param $merchantMessage
49
+ * @param $debugMessage
50
+ * @param ExceptionStore|null $exceptionStore
51
+ */
31
52
protected function setup ($ code , $ message , $ merchantMessage , $ debugMessage ,
32
53
ExceptionStore $ exceptionStore =null )
33
54
{
@@ -36,9 +57,9 @@ protected function setup($code, $message, $merchantMessage, $debugMessage,
36
57
} else {
37
58
38
59
$ this ->exceptionStore = new ExceptionStore ();
39
- $ this ->exceptionStore ->setMessage ($ message );
60
+ $ this ->exceptionStore ->setErrorMessage ($ message );
40
61
$ this ->exceptionStore ->setDetailledMessage ($ debugMessage );
41
- $ this ->exceptionStore ->setCode ($ code );
62
+ $ this ->exceptionStore ->setErrorCode ($ code );
42
63
$ this ->exceptionStore ->setExceptionClass (get_class ($ this ));
43
64
$ t = explode ('\n ' , $ this ->getTraceAsString ());
44
65
$ this ->exceptionStore ->setStack ($ t );
@@ -77,24 +98,95 @@ protected static function searchForFileAndLineCalledFromStack(array $stack)
77
98
return null ;
78
99
}
79
100
101
+ /**
102
+ * Build a exceptionStore from a PHP Exception
103
+ * @param \Exception $e
104
+ * @return ExceptionStore
105
+ */
106
+ public static function buildExceptionStore (\Exception $ e )
107
+ {
108
+ $ exceptionStore = new ExceptionStore ();
109
+ $ exceptionStore ->setErrorMessage ($ e ->getMessage ());
110
+ $ exceptionStore ->setErrorCode ($ e ->getCode ());
111
+ $ exceptionStore ->setExceptionClass (get_class ($ e ));
112
+ $ t = explode ('\n ' , $ e ->getTraceAsString ());
113
+ $ exceptionStore ->setStack ($ t );
114
+
115
+ $ stack = $ e ->getTrace ();
116
+ if (count ($ stack ) > 0 ) {
117
+ //Setting default unknown values
118
+ $ exceptionStore ->setFile ("unknown " );
119
+ $ exceptionStore ->setLine (0 );
120
+ //Searching for a valid stackItem
121
+ $ stackItem = static ::searchForFileAndLineCalledFromStack ($ stack );
122
+ if (!is_null ($ stackItem )) {
123
+ $ exceptionStore ->setFile (basename ($ stackItem ['file ' ],'.php ' ));
124
+ $ exceptionStore ->setLine ($ stackItem ['line ' ]);
125
+ }
126
+ }
127
+
128
+ return $ exceptionStore ;
129
+ }
130
+
131
+ /*
132
+ * Getters and setters
133
+ */
134
+
135
+ /**
136
+ * Returns the exception store
137
+ *
138
+ * @return ExceptionStore
139
+ */
80
140
public function getStore ()
81
141
{
82
142
return $ this ->exceptionStore ;
83
143
}
84
144
85
- public function setStore ($ e )
145
+ /**
146
+ * Injects the exception store on the exception
147
+ * instance
148
+ *
149
+ * @param ExceptionStore $e
150
+ * @return Exception
151
+ */
152
+ public function setStore (ExceptionStore $ e )
86
153
{
87
- return $ this ->exceptionStore = $ e ;
154
+ $ this ->exceptionStore = $ e ;
155
+ return $ this ;
88
156
}
89
157
90
- public function addMessage ($ key , $ value )
158
+ /**
159
+ * Sets the message on the internal exception store
160
+ *
161
+ * @param $value
162
+ */
163
+ public function setErrorMessage ($ value )
91
164
{
92
- $ this ->exceptionStore ->addMessage ( $ key , $ value );
165
+ $ this ->exceptionStore ->setErrorMessage ( $ value );
93
166
}
94
167
168
+ /**
169
+ * Returns the errorCode on the internal exception store
170
+ *
171
+ * @return string
172
+ */
95
173
public function getErrorCode ()
96
174
{
97
- return $ this ->exceptionStore ->getCode ();
175
+ return $ this ->exceptionStore ->getErrorCode ();
176
+ }
177
+
178
+ /**
179
+ * Concat the sent element message on the errorMessage
180
+ * string.
181
+ *
182
+ * @param $message
183
+ */
184
+ public function addErrorMessage ($ message )
185
+ {
186
+ if (!$ this ->exceptionStore ->getErrorMessage ()) {
187
+ $ this ->exceptionStore ->setErrorMessage ('' );
188
+ }
189
+ return $ this ->exceptionStore ->setErrorMessage ($ this ->exceptionStore ->getErrorMessage ().', ' . $ message );
98
190
}
99
191
100
192
public function setMerchantDetails ($ merchantMessage )
@@ -129,31 +221,46 @@ public function setMetadata($value)
129
221
}
130
222
131
223
/**
132
- * Build a exceptionStore from a PHP Exception
133
- * @param \Exception $e
224
+ * Returns the detailed error message from the internal exception store
225
+ *
226
+ * @return mixed
134
227
*/
135
- public static function buildExceptionStore ( \ Exception $ e )
228
+ public function getDetailedErrorMessage ( )
136
229
{
137
- $ exceptionStore = new ExceptionStore ();
138
- $ exceptionStore ->setMessage ($ e ->getMessage ());
139
- $ exceptionStore ->setCode ($ e ->getCode ());
140
- $ exceptionStore ->setExceptionClass (get_class ($ e ));
141
- $ t = explode ('\n ' , $ e ->getTraceAsString ());
142
- $ exceptionStore ->setStack ($ t );
230
+ return $ this ->exceptionStore ->getDetailedErrorMessage ();
231
+ }
143
232
144
- $ stack = $ e ->getTrace ();
145
- if (count ($ stack ) > 0 ) {
146
- //Setting default unknown values
147
- $ exceptionStore ->setFile ("unknown " );
148
- $ exceptionStore ->setLine (0 );
149
- //Searching for a valid stackItem
150
- $ stackItem = static ::searchForFileAndLineCalledFromStack ($ stack );
151
- if (!is_null ($ stackItem )) {
152
- $ exceptionStore ->setFile (basename ($ stackItem ['file ' ],'.php ' ));
153
- $ exceptionStore ->setLine ($ stackItem ['line ' ]);
154
- }
155
- }
233
+ /**
234
+ * Returns the detailed error code from the internal exception store
235
+ *
236
+ * @return mixed
237
+ */
238
+ public function getDetailedErrorCode ()
239
+ {
240
+ return $ this ->exceptionStore ->getDetailedErrorCode ();
241
+ }
156
242
157
- return $ exceptionStore ;
243
+ /**
244
+ * Sets the detailed error message of the internal exception store
245
+ *
246
+ * @param $value
247
+ * @return Exception
248
+ */
249
+ public function setDetailedErrorMessage ($ value )
250
+ {
251
+ $ this ->exceptionStore ->setDetailedErrorMessage ($ value );
252
+ return $ this ;
253
+ }
254
+
255
+ /**
256
+ * Sets the detailed error code of the internal exception store
257
+ *
258
+ * @param $value
259
+ * @return Exception
260
+ */
261
+ public function setDetailedErrorCode ($ value )
262
+ {
263
+ $ this ->exceptionStore ->setDetailedErrorCode ($ value );
264
+ return $ this ;
158
265
}
159
266
}
0 commit comments