Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions config.local.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
'ps_accounts.verify_account_tokens' => false,
'ps_accounts.accounts_vue_cdn_url' => 'http://prestashop8.docker.localhost/upload/psaccountsVue.umd.min.js',
// 'ps_accounts.accounts_cdn_url' => 'https://unpkg.com/@prestashopcorp/accounts-components@beta',
// 'ps_accounts.accounts_cdn_url' => 'http://127.0.0.1:3010/psaccountsVue.js',
'ps_accounts.accounts_cdn_url' => 'https://assets.prestashop3.com/accounts-components/1/psaccountsVue.js',
'ps_accounts.accounts_cdn_url' => 'http://127.0.0.1:3010/psaccountsVue.js',
//'ps_accounts.accounts_cdn_url' => 'https://assets.prestashop3.com/accounts-components/1/psaccountsVue.js',
'ps_accounts.cors_allowed_origins' => [
'https://integration-assets.prestashop3.com',
'https://preproduction-assets.prestashop3.com',
Expand Down
1 change: 1 addition & 0 deletions controllers/admin/AdminAjaxV2PsAccountsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ protected function handleError($e)
(string) json_encode([
'message' => $e->getMessage(),
'code' => $e->getErrorCode(),
'details' => $e->getDetails(),
])
);

Expand Down
23 changes: 15 additions & 8 deletions src/Http/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ public function toLegacy()
*/
public function getErrorMessageFromBody($key, $defaultMessage = '')
{
if (!isset($this->body[$key]) ||
!is_string($this->body[$key])) {
return $defaultMessage;
}

return $this->body[$key];
return $this->getValueFromBody($key, $defaultMessage, static function ($value) {return is_string($value); });
}

/**
Expand All @@ -88,10 +83,22 @@ public function getErrorMessageFromBody($key, $defaultMessage = '')
* @return string
*/
public function getErrorCodeFromBody($key, $defaultCode = '')
{
return $this->getValueFromBody($key, $defaultCode, static function ($value) {return is_string($value); });
}

/**
* @param string $key
* @param mixed $default
* @param callable $validator
*
* @return mixed
*/
public function getValueFromBody($key, $default = null, $validator = null)
{
if (!isset($this->body[$key]) ||
!is_string($this->body[$key])) {
return $defaultCode;
($validator && !$validator($this->body[$key]))) {
return $default;
}

return $this->body[$key];
Expand Down
98 changes: 98 additions & 0 deletions src/Service/Accounts/AccountsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,49 @@ class AccountsException extends \Exception
{
const ERROR_STORE_LEGACY_NOT_FOUND = 'store-identity/store-legacy-not-found';

/**
* @var Response
*/
protected $response;

/**
* @var string
*/
protected $errorCode;

/**
* @var string
*/
protected $reason;

/**
* @var Response|null
*/
protected $storeResponse;

/**
* @param Response $response
* @param string $defaultMessage
* @param string $defaultErrorCode
*/
public function __construct($response, $defaultMessage = '', $defaultErrorCode = '')
{
$this->response = $response;
$this->errorCode = $response->getErrorCodeFromBody('error', $defaultErrorCode);
$this->reason = $this->getReasonFromResponseBody();
$this->storeResponse = $this->getStoreResponseFromResponseBody();

parent::__construct($response->getErrorMessageFromBody('message', $defaultMessage));
}

/**
* @return Response
*/
public function getResponse()
{
return $this->response;
}

/**
* Get the error code.
*
Expand All @@ -52,4 +78,76 @@ public function getErrorCode()
{
return $this->errorCode;
}

/**
* @return string
*/
public function getReason()
{
return $this->reason;
}

/**
* @return Response|null
*/
public function getStoreResponse()
{
return $this->storeResponse;
}

/**
* @return string
*/
public function getDetails()
{
if ($storeResponse = $this->getStoreResponse()) {
return $storeResponse->statusCode . ' ' . $storeResponse->raw;
} elseif ($reason = $this->getReason()) {
return $reason;
}

return '';
}

/**
* @param string $key
*
* @return string
*/
protected function getReasonFromResponseBody($key = 'reason')
{
return $this->response->getValueFromBody(
$key,
null,
static function ($value) {
return is_string($value);
}
);
}

/**
* @param string $key
*
* @return Response|null
*/
protected function getStoreResponseFromResponseBody($key = 'response')
{
$response = $this->response->getValueFromBody(
$key,
null,
static function ($value) {
return is_array($value);
}
);

if ($response) {
return new Response(
isset($response['data']) ? $response['data'] : [],
isset($response['status']) ? $response['status'] : 0,
isset($response['headers']) ? $response['headers'] : []
);
}

return null;
}
}