Skip to content

Commit

Permalink
Better exception handling (#25)
Browse files Browse the repository at this point in the history
* Handle exceptions better

* cs
  • Loading branch information
Nyholm authored Nov 23, 2018
1 parent 5339e4f commit f193ccd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 75 deletions.
21 changes: 2 additions & 19 deletions src/Api/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace FAPI\Sylius\Api;

use FAPI\Sylius\Exception;
use FAPI\Sylius\Exception\Domain as DomainExceptions;
use FAPI\Sylius\Exception\InvalidArgumentException;
use FAPI\Sylius\Model\Cart\Cart as Model;
use FAPI\Sylius\Model\Cart\CartItem;
Expand Down Expand Up @@ -75,15 +74,7 @@ public function create(string $customer, string $channel, string $localeCode, ar

// Use any valid status code here
if (201 !== $response->getStatusCode()) {
switch ($response->getStatusCode()) {
case 400:
throw new DomainExceptions\ValidationException();
break;
default:
$this->handleErrors($response);

break;
}
$this->handleErrors($response);
}

return $this->hydrator->hydrate($response, Model::class);
Expand Down Expand Up @@ -120,15 +111,7 @@ public function addItem(int $cartId, string $variant, int $quantity)

// Use any valid status code here
if (201 !== $response->getStatusCode()) {
switch ($response->getStatusCode()) {
case 400:
throw new DomainExceptions\ValidationException();
break;
default:
$this->handleErrors($response);

break;
}
$this->handleErrors($response);
}

return $this->hydrator->hydrate($response, CartItem::class);
Expand Down
51 changes: 5 additions & 46 deletions src/Api/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace FAPI\Sylius\Api;

use FAPI\Sylius\Exception;
use FAPI\Sylius\Exception\Domain as DomainExceptions;
use FAPI\Sylius\Exception\InvalidArgumentException;
use FAPI\Sylius\Model\Checkout\PaymentCollection;
use FAPI\Sylius\Model\Checkout\ShipmentCollection;
Expand Down Expand Up @@ -64,15 +63,7 @@ public function updateAddress(int $cartId, array $shippingAddress, bool $differe

// Use any valid status code here
if (204 !== $response->getStatusCode()) {
switch ($response->getStatusCode()) {
case 400:
throw new DomainExceptions\ValidationException();
break;
default:
$this->handleErrors($response);

break;
}
$this->handleErrors($response);
}
}

Expand All @@ -94,15 +85,7 @@ public function updatePaymentMethod(int $cartId, array $params = [])

// Use any valid status code here
if (204 !== $response->getStatusCode()) {
switch ($response->getStatusCode()) {
case 400:
throw new DomainExceptions\ValidationException();
break;
default:
$this->handleErrors($response);

break;
}
$this->handleErrors($response);
}
}

Expand All @@ -124,15 +107,7 @@ public function complete(int $cartId)

// Use any valid status code here
if (204 !== $response->getStatusCode()) {
switch ($response->getStatusCode()) {
case 400:
throw new DomainExceptions\ValidationException();
break;
default:
$this->handleErrors($response);

break;
}
$this->handleErrors($response);
}
}

Expand All @@ -154,15 +129,7 @@ public function getShippingMethods(int $cartId)

// Use any valid status code here
if (200 !== $response->getStatusCode()) {
switch ($response->getStatusCode()) {
case 400:
throw new DomainExceptions\ValidationException();
break;
default:
$this->handleErrors($response);

break;
}
$this->handleErrors($response);
}

return $this->hydrator->hydrate($response, ShipmentCollection::class);
Expand All @@ -186,15 +153,7 @@ public function getPaymentMethods(int $cartId)

// Use any valid status code here
if (200 !== $response->getStatusCode()) {
switch ($response->getStatusCode()) {
case 400:
throw new DomainExceptions\ValidationException();
break;
default:
$this->handleErrors($response);

break;
}
$this->handleErrors($response);
}

return $this->hydrator->hydrate($response, PaymentCollection::class);
Expand Down
11 changes: 1 addition & 10 deletions src/Api/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace FAPI\Sylius\Api;

use FAPI\Sylius\Exception;
use FAPI\Sylius\Exception\Domain as DomainExceptions;
use FAPI\Sylius\Exception\InvalidArgumentException;
use FAPI\Sylius\Model\Customer\Customer as Model;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -57,15 +56,7 @@ public function create(string $email, string $firstName, string $lastName, strin

// Use any valid status code here
if (201 !== $response->getStatusCode()) {
switch ($response->getStatusCode()) {
case 400:
throw new DomainExceptions\ValidationException();
break;
default:
$this->handleErrors($response);

break;
}
$this->handleErrors($response);
}

return $this->hydrator->hydrate($response, Model::class);
Expand Down
3 changes: 3 additions & 0 deletions src/Api/HttpApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ protected function httpDelete(string $path, array $params = [], array $requestHe
*/
protected function handleErrors(ResponseInterface $response)
{
$body = $response->getBody()->__toString();
switch ($response->getStatusCode()) {
case 400:
throw new DomainExceptions\ValidationException($body);
case 401:
throw new DomainExceptions\UnauthorizedException();
case 404:
Expand Down

0 comments on commit f193ccd

Please sign in to comment.