Skip to content

Commit c1e8621

Browse files
committed
Two-level error system : Rename
Renames the code and message from internal Error store to errorCode and errorMessage. Also adds detailedErrorCode and detailedErrorMessage to the error store See: #33217
1 parent 0404c14 commit c1e8621

File tree

7 files changed

+260
-60
lines changed

7 files changed

+260
-60
lines changed

src/PureMachine/Bundle/SDKBundle/Exception/Exception.php

Lines changed: 137 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,24 @@
55

66
class Exception extends \Exception
77
{
8+
9+
/**
10+
* @var ExceptionStore
11+
*/
812
private $exceptionStore;
913

1014
const DEFAULT_ERROR_CODE = 'GENERIC_001';
11-
1215
const GENERIC_001 = 'GENERIC_001';
1316
const GENERIC_001_MESSAGE = 'Unknown error';
1417

18+
/**
19+
* Exception constructor.
20+
*
21+
* @param string $detailedMessage
22+
* @param null $code
23+
* @param \Exception|null $previous
24+
* @param ExceptionStore|null $exceptionStore
25+
*/
1526
public function __construct($detailedMessage = "", $code = null, \Exception $previous = null,
1627
ExceptionStore $exceptionStore=null)
1728
{
@@ -28,6 +39,16 @@ public function __construct($detailedMessage = "", $code = null, \Exception $pre
2839
$this->setup($code, $message, null, $detailedMessage, $exceptionStore);
2940
}
3041

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+
*/
3152
protected function setup($code, $message, $merchantMessage, $debugMessage,
3253
ExceptionStore $exceptionStore=null)
3354
{
@@ -36,9 +57,9 @@ protected function setup($code, $message, $merchantMessage, $debugMessage,
3657
} else {
3758

3859
$this->exceptionStore = new ExceptionStore();
39-
$this->exceptionStore->setMessage($message);
60+
$this->exceptionStore->setErrorMessage($message);
4061
$this->exceptionStore->setDetailledMessage($debugMessage);
41-
$this->exceptionStore->setCode($code);
62+
$this->exceptionStore->setErrorCode($code);
4263
$this->exceptionStore->setExceptionClass(get_class($this));
4364
$t = explode('\n', $this->getTraceAsString());
4465
$this->exceptionStore->setStack($t);
@@ -77,24 +98,95 @@ protected static function searchForFileAndLineCalledFromStack(array $stack)
7798
return null;
7899
}
79100

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+
*/
80140
public function getStore()
81141
{
82142
return $this->exceptionStore;
83143
}
84144

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)
86153
{
87-
return $this->exceptionStore = $e;
154+
$this->exceptionStore = $e;
155+
return $this;
88156
}
89157

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)
91164
{
92-
$this->exceptionStore->addMessage($key, $value);
165+
$this->exceptionStore->setErrorMessage($value);
93166
}
94167

168+
/**
169+
* Returns the errorCode on the internal exception store
170+
*
171+
* @return string
172+
*/
95173
public function getErrorCode()
96174
{
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);
98190
}
99191

100192
public function setMerchantDetails($merchantMessage)
@@ -129,31 +221,46 @@ public function setMetadata($value)
129221
}
130222

131223
/**
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
134227
*/
135-
public static function buildExceptionStore(\Exception $e)
228+
public function getDetailedErrorMessage()
136229
{
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+
}
143232

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+
}
156242

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;
158265
}
159266
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace PureMachine\Bundle\SDKBundle\Exception;
4+
5+
/**
6+
* Interface ExceptionElementInterface
7+
*
8+
* Implementing this interface, tags the object with the
9+
* next inherited method (should be implemented with direct
10+
* method definition or with __call hook)
11+
*
12+
* getErrorMessage()
13+
* getErrorCode()
14+
* getDetailedErrorMessage()
15+
* getDetailedErrorCode()
16+
* setErrorMessage()
17+
* setErrorCode()
18+
* setDetailedErrorMessage()
19+
* setDetailedErrorCode()
20+
*
21+
*
22+
* @package PureMachine\Bundle\SDKBundle\Exception
23+
*/
24+
interface ExceptionElementInterface
25+
{
26+
27+
}

src/PureMachine/Bundle/SDKBundle/Exception/WebServiceException.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static function raiseIfError($answer, $displayStack=false)
3434

3535
if ($answer instanceof ErrorResponse) {
3636

37-
$message = $answer->getAnswer()->getCode() .": ". $answer->getAnswer()->getMessage() ." \n";
37+
$message = $answer->getAnswer()->getErrorCode() .": ". $answer->getAnswer()->getErrorMessage() ." \n";
3838

3939
if ($answer->getAnswer()->isStoreProperty('detailledMessage')) {
4040
$message .= $answer->getAnswer()->getDetailledMessage();
@@ -95,7 +95,7 @@ public static function raiseIfError($answer, $displayStack=false)
9595
if (class_exists($class)) {
9696
$ex = null;
9797
try {
98-
$ex = new $class($message, $answer->getAnswer()->getCode(), null);
98+
$ex = new $class($message, $answer->getAnswer()->getErrorCode(), null);
9999
if ($ex instanceof Exception) {
100100
$ex->setStore($answer->getAnswer());
101101
}
@@ -109,8 +109,8 @@ public static function raiseIfError($answer, $displayStack=false)
109109
}
110110

111111
$e = new WebServiceException($message);
112-
$e->getStore()->setMessage($answer->getAnswer()->getMessage());
113-
$e->getStore()->setCode($answer->getAnswer()->getCode());
112+
$e->getStore()->setErrorMessage($answer->getAnswer()->getErrorMessage());
113+
$e->getStore()->setErrorCode($answer->getAnswer()->getErrorCode());
114114
throw $e;
115115
}
116116
}

src/PureMachine/Bundle/SDKBundle/Service/HttpHelper.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ public function httpRequest($url, $data=null, $method='POST',
242242

243243

244244
$e = $this->createException($message, $exception_code);
245-
$e->addMessage('output', $output);
246-
$e->addMessage('called URL', $url);
247-
$e->addMessage('data sent:', $data);
245+
$e->addErrorMessage('output', $output);
246+
$e->addErrorMessage('called URL', $url);
247+
$e->addErrorMessage('data sent:', $data);
248248
$this->triggerHttpRequestEvent($data, $output, $url, $method, $statusCode, $duration);
249249
throw $e;
250250
}
@@ -253,9 +253,9 @@ public function httpRequest($url, $data=null, $method='POST',
253253
$e = $this->createException("HTTP exception: error " . $statusCode ." for ". $url
254254
." . Page or service not found.",
255255
HTTPException::HTTP_404);
256-
$e->addMessage('output', $output);
257-
$e->addMessage('called URL', $url);
258-
$e->addMessage('data sent:', $data);
256+
$e->addErrorMessage('output', $output);
257+
$e->addErrorMessage('called URL', $url);
258+
$e->addErrorMessage('data sent:', $data);
259259
$this->triggerHttpRequestEvent($data, $output, $url, $method, $statusCode, $duration);
260260
throw $e;
261261
}
@@ -264,29 +264,29 @@ public function httpRequest($url, $data=null, $method='POST',
264264
$e = $this->createException("HTTP exception: error " . $statusCode ." for ". $url
265265
." . Invalid credentials.",
266266
HTTPException::HTTP_401);
267-
$e->addMessage('output', $output);
268-
$e->addMessage('called URL', $url);
269-
$e->addMessage('data sent:', $data);
267+
$e->addErrorMessage('output', $output);
268+
$e->addErrorMessage('called URL', $url);
269+
$e->addErrorMessage('data sent:', $data);
270270
$this->triggerHttpRequestEvent($data, $output, $url, $method, $statusCode, $duration);
271271
throw $e;
272272
}
273273

274274
if ($this->proxy && $statusCode == 100) {
275275
$errorMessage = "HTTP Timeout (100)for " . $url;
276276
$e = $this->createException($errorMessage, HTTPException::HTTP_100);
277-
$e->addMessage('output', $output);
278-
$e->addMessage('called URL', $url);
279-
$e->addMessage('data sent:', $data);
277+
$e->addErrorMessage('output', $output);
278+
$e->addErrorMessage('called URL', $url);
279+
$e->addErrorMessage('data sent:', $data);
280280
$this->triggerHttpRequestEvent($data, $output, $url, $method, $statusCode, $duration);
281281
throw $e;
282282
}
283283

284284
if ($statusCode != 200) {
285285
$errorMessage = "HTTP exception: error " . $statusCode . " for $url";
286286
$e = $this->createException($errorMessage, HTTPException::HTTP_500);
287-
$e->addMessage('output', $output);
288-
$e->addMessage('called URL', $url);
289-
$e->addMessage('data sent:', $data);
287+
$e->addErrorMessage('output', $output);
288+
$e->addErrorMessage('called URL', $url);
289+
$e->addErrorMessage('data sent:', $data);
290290
$this->triggerHttpRequestEvent($data, $output, $url, $method, $statusCode, $duration);
291291
throw $e;
292292
}

src/PureMachine/Bundle/SDKBundle/Service/WebServiceClient.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PureMachine\Bundle\SDKBundle\Store\WebService\DebugErrorResponse;
1919
use PureMachine\Bundle\SDKBundle\Store\Base\StoreHelper;
2020
use Symfony\Component\Validator\Validation;
21+
use PureMachine\Bundle\SDKBundle\Exception\ExceptionElementInterface;
2122

2223
/**
2324
* Needed because there is an annotation Inheritance bug in PHP 5.3.3 (centOS version)
@@ -424,7 +425,7 @@ public function buildErrorResponse($webServiceName, $version, \Exception $except
424425
//Translate the exception if possible
425426
if($this->isSymfony() && $this->symfonyContainer) {
426427
$translatedMessage = $this->translateException($data);
427-
if(!is_null($translatedMessage)) $data->setMessage($translatedMessage);
428+
if(!is_null($translatedMessage)) $data->setErrorMessage($translatedMessage);
428429
}
429430

430431
if ($serialize) $data = $data->serialize();
@@ -458,8 +459,15 @@ protected function translateException($store)
458459
if(!$this->isSymfony() || !$this->symfonyContainer) return null;
459460
$translator = $this->getSymfonyTranslator();
460461

461-
$translatedMessage = $translator->trans($store->getCode(), array(), 'messages', $translator->getLocale());
462-
if($translatedMessage!=$store->getCode()) return $translatedMessage;
462+
$errorCode = null;
463+
if ($store instanceof ExceptionElementInterface) {
464+
$errorCode = $store->getErrorCode();
465+
} else {
466+
$errorCode = $store->getCode();
467+
}
468+
469+
$translatedMessage = $translator->trans($errorCode, array(), 'messages', $translator->getLocale());
470+
if($translatedMessage!=$errorCode) return $translatedMessage;
463471

464472
return null;
465473
}

0 commit comments

Comments
 (0)