Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two-level error system : Rename #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 137 additions & 30 deletions src/PureMachine/Bundle/SDKBundle/Exception/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@

class Exception extends \Exception
{

/**
* @var ExceptionStore
*/
private $exceptionStore;

const DEFAULT_ERROR_CODE = 'GENERIC_001';

const GENERIC_001 = 'GENERIC_001';
const GENERIC_001_MESSAGE = 'Unknown error';

/**
* Exception constructor.
*
* @param string $detailedMessage
* @param null $code
* @param \Exception|null $previous
* @param ExceptionStore|null $exceptionStore
*/
public function __construct($detailedMessage = "", $code = null, \Exception $previous = null,
ExceptionStore $exceptionStore=null)
{
Expand All @@ -28,6 +39,16 @@ public function __construct($detailedMessage = "", $code = null, \Exception $pre
$this->setup($code, $message, null, $detailedMessage, $exceptionStore);
}

/**
* Setups a new internal exception store on the exception class
* This exception store is built from the arguments of this method call
*
* @param $code
* @param $message
* @param $merchantMessage
* @param $debugMessage
* @param ExceptionStore|null $exceptionStore
*/
protected function setup($code, $message, $merchantMessage, $debugMessage,
ExceptionStore $exceptionStore=null)
{
Expand All @@ -36,9 +57,9 @@ protected function setup($code, $message, $merchantMessage, $debugMessage,
} else {

$this->exceptionStore = new ExceptionStore();
$this->exceptionStore->setMessage($message);
$this->exceptionStore->setErrorMessage($message);
$this->exceptionStore->setDetailledMessage($debugMessage);
$this->exceptionStore->setCode($code);
$this->exceptionStore->setErrorCode($code);
$this->exceptionStore->setExceptionClass(get_class($this));
$t = explode('\n', $this->getTraceAsString());
$this->exceptionStore->setStack($t);
Expand Down Expand Up @@ -77,24 +98,95 @@ protected static function searchForFileAndLineCalledFromStack(array $stack)
return null;
}

/**
* Build a exceptionStore from a PHP Exception
* @param \Exception $e
* @return ExceptionStore
*/
public static function buildExceptionStore(\Exception $e)
{
$exceptionStore = new ExceptionStore();
$exceptionStore->setErrorMessage($e->getMessage());
$exceptionStore->setErrorCode($e->getCode());
$exceptionStore->setExceptionClass(get_class($e));
$t = explode('\n', $e->getTraceAsString());
$exceptionStore->setStack($t);

$stack = $e->getTrace();
if (count($stack) > 0) {
//Setting default unknown values
$exceptionStore->setFile("unknown");
$exceptionStore->setLine(0);
//Searching for a valid stackItem
$stackItem = static::searchForFileAndLineCalledFromStack($stack);
if (!is_null($stackItem)) {
$exceptionStore->setFile(basename($stackItem['file'],'.php'));
$exceptionStore->setLine($stackItem['line']);
}
}

return $exceptionStore;
}

/*
* Getters and setters
*/

/**
* Returns the exception store
*
* @return ExceptionStore
*/
public function getStore()
{
return $this->exceptionStore;
}

public function setStore($e)
/**
* Injects the exception store on the exception
* instance
*
* @param ExceptionStore $e
* @return Exception
*/
public function setStore(ExceptionStore $e)
{
return $this->exceptionStore = $e;
$this->exceptionStore = $e;
return $this;
}

public function addMessage($key, $value)
/**
* Sets the message on the internal exception store
*
* @param $value
*/
public function setErrorMessage($value)
{
$this->exceptionStore->addMessage($key, $value);
$this->exceptionStore->setErrorMessage($value);
}

/**
* Returns the errorCode on the internal exception store
*
* @return string
*/
public function getErrorCode()
{
return $this->exceptionStore->getCode();
return $this->exceptionStore->getErrorCode();
}

/**
* Concat the sent element message on the errorMessage
* string.
*
* @param $message
*/
public function addErrorMessage($message)
{
if (!$this->exceptionStore->getErrorMessage()) {
$this->exceptionStore->setErrorMessage('');
}
return $this->exceptionStore->setErrorMessage($this->exceptionStore->getErrorMessage().', ' . $message);
}

public function setMerchantDetails($merchantMessage)
Expand Down Expand Up @@ -129,31 +221,46 @@ public function setMetadata($value)
}

/**
* Build a exceptionStore from a PHP Exception
* @param \Exception $e
* Returns the detailed error message from the internal exception store
*
* @return mixed
*/
public static function buildExceptionStore(\Exception $e)
public function getDetailedErrorMessage()
{
$exceptionStore = new ExceptionStore();
$exceptionStore->setMessage($e->getMessage());
$exceptionStore->setCode($e->getCode());
$exceptionStore->setExceptionClass(get_class($e));
$t = explode('\n', $e->getTraceAsString());
$exceptionStore->setStack($t);
return $this->exceptionStore->getDetailedErrorMessage();
}

$stack = $e->getTrace();
if (count($stack) > 0) {
//Setting default unknown values
$exceptionStore->setFile("unknown");
$exceptionStore->setLine(0);
//Searching for a valid stackItem
$stackItem = static::searchForFileAndLineCalledFromStack($stack);
if (!is_null($stackItem)) {
$exceptionStore->setFile(basename($stackItem['file'],'.php'));
$exceptionStore->setLine($stackItem['line']);
}
}
/**
* Returns the detailed error code from the internal exception store
*
* @return mixed
*/
public function getDetailedErrorCode()
{
return $this->exceptionStore->getDetailedErrorCode();
}

return $exceptionStore;
/**
* Sets the detailed error message of the internal exception store
*
* @param $value
* @return Exception
*/
public function setDetailedErrorMessage($value)
{
$this->exceptionStore->setDetailedErrorMessage($value);
return $this;
}

/**
* Sets the detailed error code of the internal exception store
*
* @param $value
* @return Exception
*/
public function setDetailedErrorCode($value)
{
$this->exceptionStore->setDetailedErrorCode($value);
return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace PureMachine\Bundle\SDKBundle\Exception;

/**
* Interface ExceptionElementInterface
*
* Implementing this interface, tags the object with the
* next inherited method (should be implemented with direct
* method definition or with __call hook)
*
* getErrorMessage()
* getErrorCode()
* getDetailedErrorMessage()
* getDetailedErrorCode()
* setErrorMessage()
* setErrorCode()
* setDetailedErrorMessage()
* setDetailedErrorCode()
*
*
* @package PureMachine\Bundle\SDKBundle\Exception
*/
interface ExceptionElementInterface
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function raiseIfError($answer, $displayStack=false)

if ($answer instanceof ErrorResponse) {

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

if ($answer->getAnswer()->isStoreProperty('detailledMessage')) {
$message .= $answer->getAnswer()->getDetailledMessage();
Expand Down Expand Up @@ -95,7 +95,7 @@ public static function raiseIfError($answer, $displayStack=false)
if (class_exists($class)) {
$ex = null;
try {
$ex = new $class($message, $answer->getAnswer()->getCode(), null);
$ex = new $class($message, $answer->getAnswer()->getErrorCode(), null);
if ($ex instanceof Exception) {
$ex->setStore($answer->getAnswer());
}
Expand All @@ -109,8 +109,8 @@ public static function raiseIfError($answer, $displayStack=false)
}

$e = new WebServiceException($message);
$e->getStore()->setMessage($answer->getAnswer()->getMessage());
$e->getStore()->setCode($answer->getAnswer()->getCode());
$e->getStore()->setErrorMessage($answer->getAnswer()->getErrorMessage());
$e->getStore()->setErrorCode($answer->getAnswer()->getErrorCode());
throw $e;
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/PureMachine/Bundle/SDKBundle/Service/HttpHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ public function httpRequest($url, $data=null, $method='POST',


$e = $this->createException($message, $exception_code);
$e->addMessage('output', $output);
$e->addMessage('called URL', $url);
$e->addMessage('data sent:', $data);
$e->addErrorMessage('output', $output);
$e->addErrorMessage('called URL', $url);
$e->addErrorMessage('data sent:', $data);
$this->triggerHttpRequestEvent($data, $output, $url, $method, $statusCode, $duration);
throw $e;
}
Expand All @@ -253,9 +253,9 @@ public function httpRequest($url, $data=null, $method='POST',
$e = $this->createException("HTTP exception: error " . $statusCode ." for ". $url
." . Page or service not found.",
HTTPException::HTTP_404);
$e->addMessage('output', $output);
$e->addMessage('called URL', $url);
$e->addMessage('data sent:', $data);
$e->addErrorMessage('output', $output);
$e->addErrorMessage('called URL', $url);
$e->addErrorMessage('data sent:', $data);
$this->triggerHttpRequestEvent($data, $output, $url, $method, $statusCode, $duration);
throw $e;
}
Expand All @@ -264,29 +264,29 @@ public function httpRequest($url, $data=null, $method='POST',
$e = $this->createException("HTTP exception: error " . $statusCode ." for ". $url
." . Invalid credentials.",
HTTPException::HTTP_401);
$e->addMessage('output', $output);
$e->addMessage('called URL', $url);
$e->addMessage('data sent:', $data);
$e->addErrorMessage('output', $output);
$e->addErrorMessage('called URL', $url);
$e->addErrorMessage('data sent:', $data);
$this->triggerHttpRequestEvent($data, $output, $url, $method, $statusCode, $duration);
throw $e;
}

if ($this->proxy && $statusCode == 100) {
$errorMessage = "HTTP Timeout (100)for " . $url;
$e = $this->createException($errorMessage, HTTPException::HTTP_100);
$e->addMessage('output', $output);
$e->addMessage('called URL', $url);
$e->addMessage('data sent:', $data);
$e->addErrorMessage('output', $output);
$e->addErrorMessage('called URL', $url);
$e->addErrorMessage('data sent:', $data);
$this->triggerHttpRequestEvent($data, $output, $url, $method, $statusCode, $duration);
throw $e;
}

if ($statusCode != 200) {
$errorMessage = "HTTP exception: error " . $statusCode . " for $url";
$e = $this->createException($errorMessage, HTTPException::HTTP_500);
$e->addMessage('output', $output);
$e->addMessage('called URL', $url);
$e->addMessage('data sent:', $data);
$e->addErrorMessage('output', $output);
$e->addErrorMessage('called URL', $url);
$e->addErrorMessage('data sent:', $data);
$this->triggerHttpRequestEvent($data, $output, $url, $method, $statusCode, $duration);
throw $e;
}
Expand Down
14 changes: 11 additions & 3 deletions src/PureMachine/Bundle/SDKBundle/Service/WebServiceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PureMachine\Bundle\SDKBundle\Store\WebService\DebugErrorResponse;
use PureMachine\Bundle\SDKBundle\Store\Base\StoreHelper;
use Symfony\Component\Validator\Validation;
use PureMachine\Bundle\SDKBundle\Exception\ExceptionElementInterface;

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

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

$translatedMessage = $translator->trans($store->getCode(), array(), 'messages', $translator->getLocale());
if($translatedMessage!=$store->getCode()) return $translatedMessage;
$errorCode = null;
if ($store instanceof ExceptionElementInterface) {
$errorCode = $store->getErrorCode();
} else {
$errorCode = $store->getCode();
}

$translatedMessage = $translator->trans($errorCode, array(), 'messages', $translator->getLocale());
if($translatedMessage!=$errorCode) return $translatedMessage;

return null;
}
Expand Down
Loading