Skip to content

Commit

Permalink
fix: exceptions on cdek server unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
vermorag committed Nov 27, 2024
1 parent 61646cf commit cbac468
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Actions/CalculateDeliveryAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __invoke(array $package, int $instanceID, bool $addTariffsToOffi
$this->method = ShippingMethod::factory($instanceID);
$api = new CdekApi($instanceID);

if (empty($this->method->city_code) || !$api->checkAuth()) {
if (empty($this->method->city_code) || $api->authGetError() !== null) {
return [];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Blocks/AdminOrderBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static function registerMetaBoxes(string $_post_type, $post): void

add_action('admin_enqueue_scripts', [__CLASS__, 'registerOrderScripts']);

if (!(new CdekApi($shipping->getInstanceId()))->checkAuth()) {
if ((new CdekApi($shipping->getInstanceId()))->authGetError() !== null) {
add_meta_box(
Config::ORDER_META_BOX_KEY,
Loader::getPluginName(),
Expand Down
7 changes: 6 additions & 1 deletion src/Blocks/CheckoutMapBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema;
use Cdek\CdekApi;
use Cdek\Config;
use Cdek\Contracts\ExceptionContract;
use Cdek\Helpers\CheckoutHelper;
use Cdek\Helpers\UI;
use Cdek\Model\Order;
Expand Down Expand Up @@ -54,7 +55,11 @@ public static function extend_cart_data(): array

$api = new CdekApi;

$city = $api->cityCodeGet($cityInput, $postcodeInput);
try {
$city = $api->cityCodeGet($cityInput, $postcodeInput);
} catch (ExceptionContract $e) {
$city = null;
}

return [
'inputs' => [
Expand Down
8 changes: 4 additions & 4 deletions src/CdekApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ public function calculateList(array $deliveryParam): array
)->json();
}

public function checkAuth(): bool
public function authGetError(): ?string
{
try {
$this->tokenStorage->getToken();
$this->fetchToken();

return true;
return null;
} catch (LegacyAuthException $e) {
return false;
return $e->getData()['code'] ?? $e->getData()['error'] ?? 'unknown';
}
}

Expand Down
27 changes: 20 additions & 7 deletions src/ShippingMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,26 @@ public function __isset(string $key): bool

final public function admin_options(): void
{
if (!(new CdekApi)->checkAuth()) {
WC_Admin_Settings::add_error(
esc_html__(
'Error receiving token from CDEK API. Make sure the integration keys are correct',
'cdekdelivery',
),
);
$error = (new CdekApi)->authGetError();
if ($error !== null) {
if ($error === 'invalid_client') {
WC_Admin_Settings::add_error(
esc_html__(
'Error receiving token from CDEK API. Make sure the integration keys are correct',
'cdekdelivery',
),
);
} else {
WC_Admin_Settings::add_error(
sprintf(
esc_html__(
'Error receiving token from CDEK API. Contact plugin support. Error code: %s',
'cdekdelivery',
),
$error,
),
);
}
WC_Admin_Settings::show_messages();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Transport/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static function tryGetRequesterIp(): ?string
}

if (!headers_sent()) {
header("X-Requester-IP: $ip");
header("X-Origin-IP: $ip");
}

return $ip;
Expand Down

0 comments on commit cbac468

Please sign in to comment.