From 36cdd39ef1c2bd899ca69c564bd5252e83c2f789 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gr=C3=A9gory=20Planchat?=
Date: Tue, 25 Apr 2023 18:06:15 +0200
Subject: [PATCH 01/10] Error handling: Exploded the base exception into
several more specialized exception classes
---
PSWebServiceLibrary.php | 346 ++++++++++++++++++++++++++++++++-----
examples/Create.php | 12 +-
examples/CustomersList.php | 4 +-
examples/Delete.php | 10 +-
examples/Retrieve.php | 6 +-
examples/Update.php | 12 +-
6 files changed, 322 insertions(+), 68 deletions(-)
diff --git a/PSWebServiceLibrary.php b/PSWebServiceLibrary.php
index 90ca603..27fdd08 100644
--- a/PSWebServiceLibrary.php
+++ b/PSWebServiceLibrary.php
@@ -75,9 +75,7 @@ class PrestaShopWebservice
function __construct($url, $key, $debug = true)
{
if (!extension_loaded('curl')) {
- throw new PrestaShopWebserviceException(
- 'Please activate the PHP extension \'curl\' to allow use of PrestaShop webservice library'
- );
+ throw PrestaShopWebserviceMissingPreconditionException::missingExtension('curl');
}
$this->url = $url;
$this->key = $key;
@@ -92,50 +90,66 @@ function __construct($url, $key, $debug = true)
* 'response' => CURL response
*
*
- * @param array $request Response elements of CURL request
+ * @param array{status_code: string, response: string} $request Response elements of CURL request
*
* @throws PrestaShopWebserviceException if HTTP status code is not 200 or 201
*/
protected function checkStatusCode($request)
{
- switch ($request['status_code']) {
- case 200:
- case 201:
- break;
+ $this->assertStatusCode($request['status_code']);
+ }
+
+ private function assertStatusCode($statusCode)
+ {
+ switch ($statusCode) {
+ case 100:
+ throw PrestaShopWebserviceStatusException::shouldNotReceive100ContinueStatus();
+ case 101:
+ throw PrestaShopWebserviceStatusException::shouldNotReceive101SwitchingProtocolsStatus();
+ case 102:
+ throw PrestaShopWebserviceStatusException::shouldNotReceive102ProcessingStatus();
+ case 103:
+ throw PrestaShopWebserviceStatusException::shouldNotReceive103EarlyHintsStatus();
case 204:
- $error_message = 'No content';
- break;
+ throw new PrestaShopWebserviceNoContentException();
+ case 300:
+ throw PrestaShopWebserviceStatusException::shouldNotReceive300MultipleChoicesStatus();
+ case 301:
+ throw PrestaShopWebserviceStatusException::shouldNotReceive301MovedPermanentlyStatus();
+ case 302:
+ throw PrestaShopWebserviceStatusException::shouldNotReceive302FoundStatus();
+ case 303:
+ throw PrestaShopWebserviceStatusException::shouldNotReceive303SeeOtherStatus();
+ case 304:
+ throw PrestaShopWebserviceStatusException::shouldNotReceive304NotModifiedStatus();
+ case 307:
+ throw PrestaShopWebserviceStatusException::shouldNotReceive307TemporaryRedirectStatus();
+ case 308:
+ throw PrestaShopWebserviceStatusException::shouldNotReceive308PermanentRedirectStatus();
case 400:
- $error_message = 'Bad Request';
- break;
+ throw new PrestaShopWebserviceBadRequestException();
case 401:
- $error_message = 'Unauthorized';
- break;
+ throw new PrestaShopWebserviceUnauthorizedException();
+ case 403:
+ throw new PrestaShopWebserviceForbiddenException();
case 404:
- $error_message = 'Not Found';
- break;
+ throw new PrestaShopWebserviceNotFoundException();
case 405:
- $error_message = 'Method Not Allowed';
- break;
- case 500:
- $error_message = 'Internal Server Error';
- break;
- default:
- throw new PrestaShopWebserviceException(
- 'This call to PrestaShop Web Services returned an unexpected HTTP status of:' . $request['status_code']
- );
+ throw new PrestaShopWebserviceMethodNotAllowedException();
+ case 429:
+ throw new PrestaShopWebserviceTooManyRequestsException();
+ }
+ if ($statusCode >= 100 && $statusCode < 200
+ || $statusCode >= 202 && $statusCode < 300
+ || $statusCode >= 300 && $statusCode < 500
+ || $statusCode >= 600
+ || $statusCode < 100
+ ) {
+ throw new PrestaShopWebserviceClientException('This call to PrestaShop Web Services responded with a non-standard code or a code this client could not handle properly. This can come from your web server or reverse proxy configuration.', $statusCode);
}
- if (!empty($error_message)) {
- $response = $this->parseXML($request['response']);
- $errors = $response->children()->children();
- if ($errors && count($errors) > 0) {
- foreach ($errors as $error) {
- $error_message.= ' - (Code ' . $error->code . '): ' . $error->message;
- }
- }
- $error_label = 'This call to PrestaShop Web Services failed and returned an HTTP status of %d. That means: %s.';
- throw new PrestaShopWebserviceException(sprintf($error_label, $request['status_code'], $error_message));
+ if ($statusCode >= 500 && $statusCode < 600) {
+ throw new PrestaShopWebserviceServerException('This call to PrestaShop Web Services responded with a status code indicating it is not available or under a too heavy load to process your request.', $statusCode);
}
}
@@ -194,7 +208,7 @@ protected function executeRequest($url, $curl_params = array())
$index = strpos($response, "\r\n\r\n");
if ($index === false && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD') {
- throw new PrestaShopWebserviceException('Bad HTTP response ' . $response . curl_error($session));
+ throw new PrestaShopWebserviceServerException('Bad HTTP response ' . $response . curl_error($session));
}
$header = substr($response, 0, $index);
@@ -217,7 +231,7 @@ protected function executeRequest($url, $curl_params = array())
version_compare(PrestaShopWebservice::psCompatibleVersionsMin, $headerArray['PSWS-Version']) == 1 ||
version_compare(PrestaShopWebservice::psCompatibleVersionsMax, $headerArray['PSWS-Version']) == -1
) {
- throw new PrestaShopWebserviceException(
+ throw new PrestaShopWebserviceMissingPreconditionException(
'This library is not compatible with this version of PrestaShop. Please upgrade/downgrade this library'
);
}
@@ -229,7 +243,7 @@ protected function executeRequest($url, $curl_params = array())
}
$status_code = curl_getinfo($session, CURLINFO_HTTP_CODE);
if ($status_code === 0) {
- throw new PrestaShopWebserviceException('CURL Error: ' . curl_error($session));
+ throw new PrestaShopWebserviceServerException('CURL Error: ' . curl_error($session));
}
curl_close($session);
if ($this->debug) {
@@ -278,11 +292,11 @@ protected function parseXML($response)
if (libxml_get_errors()) {
$msg = var_export(libxml_get_errors(), true);
libxml_clear_errors();
- throw new PrestaShopWebserviceException('HTTP XML response is not parsable: ' . $msg);
+ throw new PrestaShopWebserviceServerException('HTTP XML response is not parsable: ' . $msg);
}
return $xml;
} else {
- throw new PrestaShopWebserviceException('HTTP response is empty');
+ throw new PrestaShopWebserviceServerException('HTTP response is empty');
}
}
@@ -312,7 +326,7 @@ public function add($options)
$url .= '&id_group_shop=' . $options['id_group_shop'];
}
} else {
- throw new PrestaShopWebserviceException('Bad parameters given');
+ throw PrestaShopWebserviceBadParametersException::badParameters();
}
$request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $xml));
@@ -374,7 +388,7 @@ public function get($options)
$url .= '?' . http_build_query($url_params);
}
} else {
- throw new PrestaShopWebserviceException('Bad parameters given');
+ throw PrestaShopWebserviceBadParametersException::badParameters();
}
$request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'GET'));
@@ -415,7 +429,7 @@ public function head($options)
$url .= '?' . http_build_query($url_params);
}
} else {
- throw new PrestaShopWebserviceException('Bad parameters given');
+ throw PrestaShopWebserviceBadParametersException::badParameters();
}
$request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'HEAD', CURLOPT_NOBODY => true));
$this->checkStatusCode($request);// check the response validity
@@ -451,7 +465,7 @@ public function edit($options)
$url .= '&id_group_shop=' . $options['id_group_shop'];
}
} else {
- throw new PrestaShopWebserviceException('Bad parameters given');
+ throw PrestaShopWebserviceBadParametersException::badParameters();
}
$request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_POSTFIELDS => $xml));
@@ -495,7 +509,7 @@ public function delete($options)
? $this->url . '/api/' . $options['resource'] . '/?id=[' . implode(',', $options['id']) . ']'
: $this->url . '/api/' . $options['resource'] . '/' . $options['id'];
} else {
- throw new PrestaShopWebserviceException('Bad parameters given');
+ throw PrestaShopWebserviceBadParametersException::badParameters();
}
if (isset($options['id_shop'])) {
@@ -512,9 +526,249 @@ public function delete($options)
}
+// Polyfill for PHP 5
+if (!interface_exists('Throwable')) {
+ interface Throwable
+ {
+ }
+}
+
/**
* @package PrestaShopWebservice
*/
-class PrestaShopWebserviceException extends Exception
+interface PrestaShopWebserviceException extends \Throwable
{
}
+
+/**
+ * @package PrestaShopWebservice
+ */
+class PrestaShopWebserviceBadParametersException extends \RuntimeException implements PrestaShopWebserviceException {
+ public static function badParameters($response, $previous = null)
+ {
+ return new self('Bad parameters given', $previous);
+ }
+}
+
+/**
+ * @package PrestaShopWebservice
+ */
+class PrestaShopWebserviceMissingPreconditionException extends \BadFunctionCallException implements PrestaShopWebserviceException {
+ public static function missingExtension($extension, $previous = null)
+ {
+ return new self(
+ strtr(
+ 'Please activate the PHP extension \'%extension%\' to allow use of PrestaShop webservice library',
+ array(
+ '%extension%' => $extension,
+ )
+ ),
+ $previous
+ );
+ }
+}
+
+/**
+ * @package PrestaShopWebservice
+ */
+class PrestaShopWebserviceBadRequestException extends \RuntimeException implements PrestaShopWebserviceException {
+ public function __construct($previous = null)
+ {
+ parent::__construct('Bad Request', 400, $previous);
+ }
+}
+
+/**
+ * @package PrestaShopWebservice
+ */
+class PrestaShopWebserviceUnauthorizedException extends \RuntimeException implements PrestaShopWebserviceException {
+ public function __construct($previous = null)
+ {
+ parent::__construct('Unauthorized', 401, $previous);
+ }
+}
+
+/**
+ * @package PrestaShopWebservice
+ */
+class PrestaShopWebserviceForbiddenException extends \RuntimeException implements PrestaShopWebserviceException {
+ public function __construct($previous = null)
+ {
+ parent::__construct('Forbidden', 403, $previous);
+ }
+}
+
+/**
+ * @package PrestaShopWebservice
+ */
+class PrestaShopWebserviceNotFoundException extends \RuntimeException implements PrestaShopWebserviceException {
+ public function __construct($previous = null)
+ {
+ parent::__construct('Not Found', 404, $previous);
+ }
+}
+
+/**
+ * @package PrestaShopWebservice
+ */
+class PrestaShopWebserviceMethodNotAllowedException extends \RuntimeException implements PrestaShopWebserviceException {
+ public function __construct($previous = null)
+ {
+ parent::__construct('Method Not Allowed', 405, $previous);
+ }
+}
+
+/**
+ * @package PrestaShopWebservice
+ */
+class PrestaShopWebserviceTooManyRequestsException extends \RuntimeException implements PrestaShopWebserviceException {
+ public function __construct($previous = null)
+ {
+ parent::__construct('Too Many Requests', 429, $previous);
+ }
+}
+
+/**
+ * @package PrestaShopWebservice
+ */
+class PrestaShopWebserviceNoContentException extends \RuntimeException implements PrestaShopWebserviceException {
+ public function __construct($previous = null)
+ {
+ parent::__construct('No Content', 204, $previous);
+ }
+}
+
+/**
+ * @package PrestaShopWebservice
+ */
+class PrestaShopWebserviceStatusException extends \RuntimeException implements PrestaShopWebserviceException {
+ /**
+ * @param int $code
+ * @param string $status
+ * @param ?\Throwable $previous
+ * @return self
+ */
+ private static function shouldNotReceiveStatus($code, $status, $previous = null)
+ {
+ return new self(
+ strtr(
+ 'This call to PrestaShop Web Services responded with status `%code% %status%`, this client was not designed to process this kind of response. This can come from your web server or reverse proxy configuration.',
+ array(
+ '%code%' => $code,
+ '%status%' => $status,
+ )
+ ),
+ $code,
+ $previous
+ );
+ }
+
+ /**
+ * @param ?\Throwable $previous
+ * @return self
+ */
+ public static function shouldNotReceive100ContinueStatus($previous = null)
+ {
+ return self::shouldNotReceiveStatus(100, 'Continue', $previous);
+ }
+
+ /**
+ * @param ?\Throwable $previous
+ * @return self
+ */
+ public static function shouldNotReceive101SwitchingProtocolsStatus($previous = null)
+ {
+ return self::shouldNotReceiveStatus(101, 'Switching Protocols', $previous);
+ }
+
+ /**
+ * @param ?\Throwable $previous
+ * @return self
+ */
+ public static function shouldNotReceive102ProcessingStatus($previous = null)
+ {
+ return self::shouldNotReceiveStatus(102, 'Processing', $previous);
+ }
+
+ /**
+ * @param ?\Throwable $previous
+ * @return self
+ */
+ public static function shouldNotReceive103EarlyHintsStatus($previous = null)
+ {
+ return self::shouldNotReceiveStatus(103, 'Early Hints', $previous);
+ }
+
+ /**
+ * @param ?\Throwable $previous
+ * @return self
+ */
+ public static function shouldNotReceive300MultipleChoicesStatus($previous = null)
+ {
+ return self::shouldNotReceiveStatus(300, 'Multiple Choices', $previous);
+ }
+
+ /**
+ * @param ?\Throwable $previous
+ * @return self
+ */
+ public static function shouldNotReceive301MovedPermanentlyStatus($previous = null)
+ {
+ return self::shouldNotReceiveStatus(301, 'Moved Permanently', $previous);
+ }
+
+ /**
+ * @param ?\Throwable $previous
+ * @return self
+ */
+ public static function shouldNotReceive302FoundStatus($previous = null)
+ {
+ return self::shouldNotReceiveStatus(302, 'Found', $previous);
+ }
+
+ /**
+ * @param ?\Throwable $previous
+ * @return self
+ */
+ public static function shouldNotReceive303SeeOtherStatus($previous = null)
+ {
+ return self::shouldNotReceiveStatus(303, 'See Other', $previous);
+ }
+
+ /**
+ * @param ?\Throwable $previous
+ * @return self
+ */
+ public static function shouldNotReceive304NotModifiedStatus($previous = null)
+ {
+ return self::shouldNotReceiveStatus(304, 'Not Modified', $previous);
+ }
+
+ /**
+ * @param ?\Throwable $previous
+ * @return self
+ */
+ public static function shouldNotReceive307TemporaryRedirectStatus($previous = null)
+ {
+ return self::shouldNotReceiveStatus(307, 'Temporary Redirect', $previous);
+ }
+
+ /**
+ * @param ?\Throwable $previous
+ * @return self
+ */
+ public static function shouldNotReceive308PermanentRedirectStatus($previous = null)
+ {
+ return self::shouldNotReceiveStatus(308, 'Permanent Redirect', $previous);
+ }
+}
+
+/**
+ * @package PrestaShopWebservice
+ */
+class PrestaShopWebserviceClientException extends \RuntimeException implements PrestaShopWebserviceException {}
+
+/**
+ * @package PrestaShopWebservice
+ */
+class PrestaShopWebserviceServerException extends \RuntimeException implements PrestaShopWebserviceException {}
diff --git a/examples/Create.php b/examples/Create.php
index 1ebf4d8..73eaf64 100644
--- a/examples/Create.php
+++ b/examples/Create.php
@@ -44,13 +44,13 @@
$xml = $webService->get($opt);
$resources = $xml->children()->children();
}
-catch (PrestaShopWebserviceException $e)
+catch (PrestaShopWebserviceException $exception)
{
// Here we are dealing with errors
- $trace = $e->getTrace();
+ $trace = $exception->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
- else echo 'Other error '.$e->getMessage();
+ else echo 'Other error '.$exception->getMessage();
}
if (count($_POST) > 0)
@@ -70,13 +70,13 @@
echo "Successfully added.";
}
}
- catch (PrestaShopWebserviceException $ex)
+ catch (PrestaShopWebserviceException $exception)
{
// Here we are dealing with errors
- $trace = $ex->getTrace();
+ $trace = $exception->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
- else echo 'Other error '.$ex->getMessage();
+ else echo 'Other error '.$exception->getMessage();
}
}
diff --git a/examples/CustomersList.php b/examples/CustomersList.php
index 241eeb0..3d33e34 100644
--- a/examples/CustomersList.php
+++ b/examples/CustomersList.php
@@ -47,10 +47,10 @@
// Here we get the elements from children of customers markup "customer"
$resources = $xml->customers->children();
}
-catch (PrestaShopWebserviceException $e)
+catch (PrestaShopWebserviceException $exception)
{
// Here we are dealing with errors
- $trace = $e->getTrace();
+ $trace = $exception->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
else echo 'Other error';
diff --git a/examples/Delete.php b/examples/Delete.php
index 07171a1..0f521e3 100644
--- a/examples/Delete.php
+++ b/examples/Delete.php
@@ -50,13 +50,13 @@
// If there's an error we throw an exception
echo 'Successfully deleted ! ';
}
- catch (PrestaShopWebserviceException $e)
+ catch (PrestaShopWebserviceException $exception)
{
// Here we are dealing with errors
- $trace = $e->getTrace();
+ $trace = $exception->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
- else echo 'Other error '.$e->getMessage();
+ else echo 'Other error '.$exception->getMessage();
}
}
else
@@ -69,10 +69,10 @@
$xml = $webService->get($opt);
$resources = $xml->children()->children();
}
- catch (PrestaShopWebserviceException $e)
+ catch (PrestaShopWebserviceException $exception)
{
// Here we are dealing with errors
- $trace = $e->getTrace();
+ $trace = $exception->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
else echo 'Other error';
diff --git a/examples/Retrieve.php b/examples/Retrieve.php
index 8771220..00ad2a3 100644
--- a/examples/Retrieve.php
+++ b/examples/Retrieve.php
@@ -49,13 +49,13 @@
// Here we get the elements from children of customer markup which is children of prestashop root markup
$resources = $xml->children()->children();
}
-catch (PrestaShopWebserviceException $e)
+catch (PrestaShopWebserviceException $exception)
{
// Here we are dealing with errors
- $trace = $e->getTrace();
+ $trace = $exception->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
- else echo 'Other error '.$e->getMessage();
+ else echo 'Other error '.$exception->getMessage();
}
// We set the Title
diff --git a/examples/Update.php b/examples/Update.php
index ddb4ac7..ab7914d 100644
--- a/examples/Update.php
+++ b/examples/Update.php
@@ -45,13 +45,13 @@
// Here we get the elements from children of customer markup which is children of prestashop root markup
$resources = $xml->children()->children();
}
-catch (PrestaShopWebserviceException $e)
+catch (PrestaShopWebserviceException $exception)
{
// Here we are dealing with errors
- $trace = $e->getTrace();
+ $trace = $exception->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
- else echo 'Other error '.$e->getMessage();
+ else echo 'Other error '.$exception->getMessage();
}
// Second : We update the data and send it to the web service
@@ -72,13 +72,13 @@
// if WebService don't throw an exception the action worked well and we don't show the following message
echo "Successfully updated.";
}
- catch (PrestaShopWebserviceException $ex)
+ catch (PrestaShopWebserviceException $exception)
{
// Here we are dealing with errors
- $trace = $ex->getTrace();
+ $trace = $exception->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
- else echo 'Other error '.$ex->getMessage();
+ else echo 'Other error '.$exception->getMessage();
}
}
From dbc3cf6eeb8faaedfed300a3082ea7ffc7b8b231 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gr=C3=A9gory=20Planchat?=
Date: Wed, 26 Apr 2023 11:40:35 +0200
Subject: [PATCH 02/10] Error handling: Removed an unnecessary and unused
parameter inside an exception constructor
---
PSWebServiceLibrary.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/PSWebServiceLibrary.php b/PSWebServiceLibrary.php
index 27fdd08..22ff061 100644
--- a/PSWebServiceLibrary.php
+++ b/PSWebServiceLibrary.php
@@ -544,7 +544,7 @@ interface PrestaShopWebserviceException extends \Throwable
* @package PrestaShopWebservice
*/
class PrestaShopWebserviceBadParametersException extends \RuntimeException implements PrestaShopWebserviceException {
- public static function badParameters($response, $previous = null)
+ public static function badParameters($previous = null)
{
return new self('Bad parameters given', $previous);
}
From 4f78866cb49c04e7e131a1f5ddb8fcc4531f66d8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gr=C3=A9gory=20Planchat?=
Date: Wed, 26 Apr 2023 11:46:35 +0200
Subject: [PATCH 03/10] Error handling: Updated the code examples, removing the
complex trace analysis to determine the error type
---
examples/Create.php | 33 +++++++++++++++++----------------
examples/CustomersList.php | 16 ++++++++--------
examples/Delete.php | 36 ++++++++++++++++++------------------
examples/Retrieve.php | 16 ++++++++--------
examples/Update.php | 34 +++++++++++++++++-----------------
5 files changed, 68 insertions(+), 67 deletions(-)
diff --git a/examples/Create.php b/examples/Create.php
index 73eaf64..a7dd5f2 100644
--- a/examples/Create.php
+++ b/examples/Create.php
@@ -43,14 +43,15 @@
else
$xml = $webService->get($opt);
$resources = $xml->children()->children();
-}
-catch (PrestaShopWebserviceException $exception)
-{
- // Here we are dealing with errors
- $trace = $exception->getTrace();
- if ($trace[0]['args'][0] == 404) echo 'Bad ID';
- else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
- else echo 'Other error '.$exception->getMessage();
+
+} catch (PrestaShopWebserviceNotFoundException $exception) {
+ echo 'Bad ID';
+} catch (PrestaShopWebserviceUnauthorizedException $exception) {
+ echo 'Bad auth key';
+} catch (PrestaShopWebserviceForbiddenException $exception) {
+ echo 'Not logged in';
+} catch (PrestaShopWebserviceException $exception) {
+ echo 'Other error '.$exception->getMessage();
}
if (count($_POST) > 0)
@@ -69,14 +70,14 @@
$xml = $webService->add($opt);
echo "Successfully added.";
}
- }
- catch (PrestaShopWebserviceException $exception)
- {
- // Here we are dealing with errors
- $trace = $exception->getTrace();
- if ($trace[0]['args'][0] == 404) echo 'Bad ID';
- else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
- else echo 'Other error '.$exception->getMessage();
+ } catch (PrestaShopWebserviceNotFoundException $exception) {
+ echo 'Bad ID';
+ } catch (PrestaShopWebserviceUnauthorizedException $exception) {
+ echo 'Bad auth key';
+ } catch (PrestaShopWebserviceForbiddenException $exception) {
+ echo 'Not logged in';
+ } catch (PrestaShopWebserviceException $exception) {
+ echo 'Other error '.$exception->getMessage();
}
}
diff --git a/examples/CustomersList.php b/examples/CustomersList.php
index 3d33e34..799565a 100644
--- a/examples/CustomersList.php
+++ b/examples/CustomersList.php
@@ -46,14 +46,14 @@
// Here we get the elements from children of customers markup "customer"
$resources = $xml->customers->children();
-}
-catch (PrestaShopWebserviceException $exception)
-{
- // Here we are dealing with errors
- $trace = $exception->getTrace();
- if ($trace[0]['args'][0] == 404) echo 'Bad ID';
- else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
- else echo 'Other error';
+} catch (PrestaShopWebserviceNotFoundException $exception) {
+ echo 'Bad ID';
+} catch (PrestaShopWebserviceUnauthorizedException $exception) {
+ echo 'Bad auth key';
+} catch (PrestaShopWebserviceForbiddenException $exception) {
+ echo 'Not logged in';
+} catch (PrestaShopWebserviceException $exception) {
+ echo 'Other error '.$exception->getMessage();
}
// We set the Title
diff --git a/examples/Delete.php b/examples/Delete.php
index 0f521e3..cfb1a32 100644
--- a/examples/Delete.php
+++ b/examples/Delete.php
@@ -49,15 +49,15 @@
$webService->delete(array('resource' => 'customers', 'id' => intval($_GET['DeleteID'])));
// If there's an error we throw an exception
echo 'Successfully deleted ! ';
- }
- catch (PrestaShopWebserviceException $exception)
- {
- // Here we are dealing with errors
- $trace = $exception->getTrace();
- if ($trace[0]['args'][0] == 404) echo 'Bad ID';
- else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
- else echo 'Other error '.$exception->getMessage();
- }
+ } catch (PrestaShopWebserviceNotFoundException $exception) {
+ echo 'Bad ID';
+ } catch (PrestaShopWebserviceUnauthorizedException $exception) {
+ echo 'Bad auth key';
+ } catch (PrestaShopWebserviceForbiddenException $exception) {
+ echo 'Not logged in';
+ } catch (PrestaShopWebserviceException $exception) {
+ echo 'Other error '.$exception->getMessage();
+ }
}
else
{
@@ -68,15 +68,15 @@
$opt = array('resource' => 'customers');
$xml = $webService->get($opt);
$resources = $xml->children()->children();
- }
- catch (PrestaShopWebserviceException $exception)
- {
- // Here we are dealing with errors
- $trace = $exception->getTrace();
- if ($trace[0]['args'][0] == 404) echo 'Bad ID';
- else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
- else echo 'Other error';
- }
+ } catch (PrestaShopWebserviceNotFoundException $exception) {
+ echo 'Bad ID';
+ } catch (PrestaShopWebserviceUnauthorizedException $exception) {
+ echo 'Bad auth key';
+ } catch (PrestaShopWebserviceForbiddenException $exception) {
+ echo 'Not logged in';
+ } catch (PrestaShopWebserviceException $exception) {
+ echo 'Other error '.$exception->getMessage();
+ }
echo 'Customers List ';
echo '';
diff --git a/examples/Retrieve.php b/examples/Retrieve.php
index 00ad2a3..b64d726 100644
--- a/examples/Retrieve.php
+++ b/examples/Retrieve.php
@@ -48,14 +48,14 @@
// Here we get the elements from children of customer markup which is children of prestashop root markup
$resources = $xml->children()->children();
-}
-catch (PrestaShopWebserviceException $exception)
-{
- // Here we are dealing with errors
- $trace = $exception->getTrace();
- if ($trace[0]['args'][0] == 404) echo 'Bad ID';
- else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
- else echo 'Other error '.$exception->getMessage();
+} catch (PrestaShopWebserviceNotFoundException $exception) {
+ echo 'Bad ID';
+} catch (PrestaShopWebserviceUnauthorizedException $exception) {
+ echo 'Bad auth key';
+} catch (PrestaShopWebserviceForbiddenException $exception) {
+ echo 'Not logged in';
+} catch (PrestaShopWebserviceException $exception) {
+ echo 'Other error '.$exception->getMessage();
}
// We set the Title
diff --git a/examples/Update.php b/examples/Update.php
index ab7914d..3f32b13 100644
--- a/examples/Update.php
+++ b/examples/Update.php
@@ -44,14 +44,14 @@
// Here we get the elements from children of customer markup which is children of prestashop root markup
$resources = $xml->children()->children();
-}
-catch (PrestaShopWebserviceException $exception)
-{
- // Here we are dealing with errors
- $trace = $exception->getTrace();
- if ($trace[0]['args'][0] == 404) echo 'Bad ID';
- else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
- else echo 'Other error '.$exception->getMessage();
+} catch (PrestaShopWebserviceNotFoundException $exception) {
+ echo 'Bad ID';
+} catch (PrestaShopWebserviceUnauthorizedException $exception) {
+ echo 'Bad auth key';
+} catch (PrestaShopWebserviceForbiddenException $exception) {
+ echo 'Not logged in';
+} catch (PrestaShopWebserviceException $exception) {
+ echo 'Other error '.$exception->getMessage();
}
// Second : We update the data and send it to the web service
@@ -71,15 +71,15 @@
$xml = $webService->edit($opt);
// if WebService don't throw an exception the action worked well and we don't show the following message
echo "Successfully updated.";
- }
- catch (PrestaShopWebserviceException $exception)
- {
- // Here we are dealing with errors
- $trace = $exception->getTrace();
- if ($trace[0]['args'][0] == 404) echo 'Bad ID';
- else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
- else echo 'Other error '.$exception->getMessage();
- }
+ } catch (PrestaShopWebserviceNotFoundException $exception) {
+ echo 'Bad ID';
+ } catch (PrestaShopWebserviceUnauthorizedException $exception) {
+ echo 'Bad auth key';
+ } catch (PrestaShopWebserviceForbiddenException $exception) {
+ echo 'Not logged in';
+ } catch (PrestaShopWebserviceException $exception) {
+ echo 'Other error '.$exception->getMessage();
+ }
}
// UI
From 7bdad868715c46c4e7cc1c6fc402cba33d5f1465 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gr=C3=A9gory=20Planchat?=
Date: Wed, 26 Apr 2023 11:48:47 +0200
Subject: [PATCH 04/10] Error handling: Replaced tabs with spaces in the
examples
---
examples/Create.php | 82 +++++++++++++++++++-------------------
examples/CustomersList.php | 36 ++++++++---------
examples/Delete.php | 70 ++++++++++++++++----------------
examples/Retrieve.php | 74 +++++++++++++++++-----------------
examples/Update.php | 80 ++++++++++++++++++-------------------
5 files changed, 171 insertions(+), 171 deletions(-)
diff --git a/examples/Create.php b/examples/Create.php
index a7dd5f2..2dbad74 100644
--- a/examples/Create.php
+++ b/examples/Create.php
@@ -36,13 +36,13 @@
// Here we use the WebService to get the schema of "customers" resource
try
{
- $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
- $opt = array('resource' => 'customers');
- if (isset($_GET['Create']))
- $xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/customers?schema=blank'));
- else
- $xml = $webService->get($opt);
- $resources = $xml->children()->children();
+ $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
+ $opt = array('resource' => 'customers');
+ if (isset($_GET['Create']))
+ $xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/customers?schema=blank'));
+ else
+ $xml = $webService->get($opt);
+ $resources = $xml->children()->children();
} catch (PrestaShopWebserviceNotFoundException $exception) {
echo 'Bad ID';
@@ -57,28 +57,28 @@
if (count($_POST) > 0)
{
// Here we have XML before update, lets update XML
- foreach ($resources as $nodeKey => $node)
- {
- $resources->$nodeKey = $_POST[$nodeKey];
- }
- try
- {
- $opt = array('resource' => 'customers');
- if ($_GET['Create'] == 'Creating')
- {
- $opt['postXml'] = $xml->asXML();
- $xml = $webService->add($opt);
- echo "Successfully added.";
- }
- } catch (PrestaShopWebserviceNotFoundException $exception) {
+ foreach ($resources as $nodeKey => $node)
+ {
+ $resources->$nodeKey = $_POST[$nodeKey];
+ }
+ try
+ {
+ $opt = array('resource' => 'customers');
+ if ($_GET['Create'] == 'Creating')
+ {
+ $opt['postXml'] = $xml->asXML();
+ $xml = $webService->add($opt);
+ echo "Successfully added.";
+ }
+ } catch (PrestaShopWebserviceNotFoundException $exception) {
echo 'Bad ID';
- } catch (PrestaShopWebserviceUnauthorizedException $exception) {
+ } catch (PrestaShopWebserviceUnauthorizedException $exception) {
echo 'Bad auth key';
} catch (PrestaShopWebserviceForbiddenException $exception) {
echo 'Not logged in';
} catch (PrestaShopWebserviceException $exception) {
- echo 'Other error '.$exception->getMessage();
- }
+ echo 'Other error '.$exception->getMessage();
+ }
}
// We set the Title
@@ -89,12 +89,12 @@
// We set a link to go back to list if we are in creation
if (isset($_GET['Create']))
- echo 'Return to the list ';
+ echo 'Return to the list ';
if (!isset($_GET['Create']))
- echo ' ';
+ echo ' ';
else
- echo '';
+ echo ' ';
?>
diff --git a/examples/CustomersList.php b/examples/CustomersList.php
index 799565a..1f9f1fe 100644
--- a/examples/CustomersList.php
+++ b/examples/CustomersList.php
@@ -28,24 +28,24 @@
*/
// Here we define constants /!\ You need to replace this parameters
-define('DEBUG', true); // Debug mode
-define('PS_SHOP_PATH', 'http://www.myshop.com/'); // Root path of your PrestaShop store
-define('PS_WS_AUTH_KEY', 'ZQ88PRJX5VWQHCWE4EE7SQ7HPNX00RAJ'); // Auth key (Get it in your Back Office)
+define('DEBUG', true); // Debug mode
+define('PS_SHOP_PATH', 'http://www.myshop.com/'); // Root path of your PrestaShop store
+define('PS_WS_AUTH_KEY', 'ZQ88PRJX5VWQHCWE4EE7SQ7HPNX00RAJ'); // Auth key (Get it in your Back Office)
require_once('../PSWebServiceLibrary.php');
// Here we make the WebService Call
try
{
- $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
-
- // Here we set the option array for the Webservice : we want customers resources
- $opt['resource'] = 'customers';
-
- // Call
- $xml = $webService->get($opt);
+ $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
+
+ // Here we set the option array for the Webservice : we want customers resources
+ $opt['resource'] = 'customers';
+
+ // Call
+ $xml = $webService->get($opt);
- // Here we get the elements from children of customers markup "customer"
- $resources = $xml->customers->children();
+ // Here we get the elements from children of customers markup "customer"
+ $resources = $xml->customers->children();
} catch (PrestaShopWebserviceNotFoundException $exception) {
echo 'Bad ID';
} catch (PrestaShopWebserviceUnauthorizedException $exception) {
@@ -63,12 +63,12 @@
// if $resources is set we can lists element in it otherwise do nothing cause there's an error
if (isset($resources))
{
- echo 'Id ';
- foreach ($resources as $resource)
- {
- // Iterates on the found IDs
- echo ''.$resource->attributes().' ';
- }
+ echo 'Id ';
+ foreach ($resources as $resource)
+ {
+ // Iterates on the found IDs
+ echo ''.$resource->attributes().' ';
+ }
}
echo '
';
?>
diff --git a/examples/Delete.php b/examples/Delete.php
index cfb1a32..0b516f8 100644
--- a/examples/Delete.php
+++ b/examples/Delete.php
@@ -35,20 +35,20 @@
if (isset($_GET['DeleteID']))
{
- //Deletion
+ //Deletion
- echo 'Customers Deletion ';
+ echo 'Customers Deletion ';
- // We set a link to go back to list
- echo 'Return to the list ';
+ // We set a link to go back to list
+ echo 'Return to the list ';
- try
- {
- $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
- // Call for a deletion, we specify the resource name and the id of the resource in order to delete the item
- $webService->delete(array('resource' => 'customers', 'id' => intval($_GET['DeleteID'])));
- // If there's an error we throw an exception
- echo 'Successfully deleted ! ';
+ try
+ {
+ $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
+ // Call for a deletion, we specify the resource name and the id of the resource in order to delete the item
+ $webService->delete(array('resource' => 'customers', 'id' => intval($_GET['DeleteID'])));
+ // If there's an error we throw an exception
+ echo 'Successfully deleted ! ';
} catch (PrestaShopWebserviceNotFoundException $exception) {
echo 'Bad ID';
} catch (PrestaShopWebserviceUnauthorizedException $exception) {
@@ -61,13 +61,13 @@
}
else
{
- // Else get customers list
- try
- {
- $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
- $opt = array('resource' => 'customers');
- $xml = $webService->get($opt);
- $resources = $xml->children()->children();
+ // Else get customers list
+ try
+ {
+ $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
+ $opt = array('resource' => 'customers');
+ $xml = $webService->get($opt);
+ $resources = $xml->children()->children();
} catch (PrestaShopWebserviceNotFoundException $exception) {
echo 'Bad ID';
} catch (PrestaShopWebserviceUnauthorizedException $exception) {
@@ -78,24 +78,24 @@
echo 'Other error '.$exception->getMessage();
}
- echo 'Customers List ';
- echo '';
- if (isset($resources))
- {
- echo '';
- if (!isset($DeletionID))
- {
- echo 'Id More ';
+ echo 'Customers List ';
+ echo '';
+ if (isset($resources))
+ {
+ echo '';
+ if (!isset($DeletionID))
+ {
+ echo 'Id More ';
- foreach ($resources as $resource)
- {
- echo ''.$resource->attributes().' '.
- 'Delete '.
- ' ';
- }
- }
- echo '
';
- }
+ foreach ($resources as $resource)
+ {
+ echo ''.$resource->attributes().' '.
+ 'Delete '.
+ ' ';
+ }
+ }
+ echo '
';
+ }
}
?>