Skip to content

Commit f193ccd

Browse files
authored
Better exception handling (#25)
* Handle exceptions better * cs
1 parent 5339e4f commit f193ccd

File tree

4 files changed

+11
-75
lines changed

4 files changed

+11
-75
lines changed

src/Api/Cart.php

+2-19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace FAPI\Sylius\Api;
1111

1212
use FAPI\Sylius\Exception;
13-
use FAPI\Sylius\Exception\Domain as DomainExceptions;
1413
use FAPI\Sylius\Exception\InvalidArgumentException;
1514
use FAPI\Sylius\Model\Cart\Cart as Model;
1615
use FAPI\Sylius\Model\Cart\CartItem;
@@ -75,15 +74,7 @@ public function create(string $customer, string $channel, string $localeCode, ar
7574

7675
// Use any valid status code here
7776
if (201 !== $response->getStatusCode()) {
78-
switch ($response->getStatusCode()) {
79-
case 400:
80-
throw new DomainExceptions\ValidationException();
81-
break;
82-
default:
83-
$this->handleErrors($response);
84-
85-
break;
86-
}
77+
$this->handleErrors($response);
8778
}
8879

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

121112
// Use any valid status code here
122113
if (201 !== $response->getStatusCode()) {
123-
switch ($response->getStatusCode()) {
124-
case 400:
125-
throw new DomainExceptions\ValidationException();
126-
break;
127-
default:
128-
$this->handleErrors($response);
129-
130-
break;
131-
}
114+
$this->handleErrors($response);
132115
}
133116

134117
return $this->hydrator->hydrate($response, CartItem::class);

src/Api/Checkout.php

+5-46
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace FAPI\Sylius\Api;
1111

1212
use FAPI\Sylius\Exception;
13-
use FAPI\Sylius\Exception\Domain as DomainExceptions;
1413
use FAPI\Sylius\Exception\InvalidArgumentException;
1514
use FAPI\Sylius\Model\Checkout\PaymentCollection;
1615
use FAPI\Sylius\Model\Checkout\ShipmentCollection;
@@ -64,15 +63,7 @@ public function updateAddress(int $cartId, array $shippingAddress, bool $differe
6463

6564
// Use any valid status code here
6665
if (204 !== $response->getStatusCode()) {
67-
switch ($response->getStatusCode()) {
68-
case 400:
69-
throw new DomainExceptions\ValidationException();
70-
break;
71-
default:
72-
$this->handleErrors($response);
73-
74-
break;
75-
}
66+
$this->handleErrors($response);
7667
}
7768
}
7869

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

9586
// Use any valid status code here
9687
if (204 !== $response->getStatusCode()) {
97-
switch ($response->getStatusCode()) {
98-
case 400:
99-
throw new DomainExceptions\ValidationException();
100-
break;
101-
default:
102-
$this->handleErrors($response);
103-
104-
break;
105-
}
88+
$this->handleErrors($response);
10689
}
10790
}
10891

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

125108
// Use any valid status code here
126109
if (204 !== $response->getStatusCode()) {
127-
switch ($response->getStatusCode()) {
128-
case 400:
129-
throw new DomainExceptions\ValidationException();
130-
break;
131-
default:
132-
$this->handleErrors($response);
133-
134-
break;
135-
}
110+
$this->handleErrors($response);
136111
}
137112
}
138113

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

155130
// Use any valid status code here
156131
if (200 !== $response->getStatusCode()) {
157-
switch ($response->getStatusCode()) {
158-
case 400:
159-
throw new DomainExceptions\ValidationException();
160-
break;
161-
default:
162-
$this->handleErrors($response);
163-
164-
break;
165-
}
132+
$this->handleErrors($response);
166133
}
167134

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

187154
// Use any valid status code here
188155
if (200 !== $response->getStatusCode()) {
189-
switch ($response->getStatusCode()) {
190-
case 400:
191-
throw new DomainExceptions\ValidationException();
192-
break;
193-
default:
194-
$this->handleErrors($response);
195-
196-
break;
197-
}
156+
$this->handleErrors($response);
198157
}
199158

200159
return $this->hydrator->hydrate($response, PaymentCollection::class);

src/Api/Customer.php

+1-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace FAPI\Sylius\Api;
1111

1212
use FAPI\Sylius\Exception;
13-
use FAPI\Sylius\Exception\Domain as DomainExceptions;
1413
use FAPI\Sylius\Exception\InvalidArgumentException;
1514
use FAPI\Sylius\Model\Customer\Customer as Model;
1615
use Psr\Http\Message\ResponseInterface;
@@ -57,15 +56,7 @@ public function create(string $email, string $firstName, string $lastName, strin
5756

5857
// Use any valid status code here
5958
if (201 !== $response->getStatusCode()) {
60-
switch ($response->getStatusCode()) {
61-
case 400:
62-
throw new DomainExceptions\ValidationException();
63-
break;
64-
default:
65-
$this->handleErrors($response);
66-
67-
break;
68-
}
59+
$this->handleErrors($response);
6960
}
7061

7162
return $this->hydrator->hydrate($response, Model::class);

src/Api/HttpApi.php

+3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ protected function httpDelete(string $path, array $params = [], array $requestHe
141141
*/
142142
protected function handleErrors(ResponseInterface $response)
143143
{
144+
$body = $response->getBody()->__toString();
144145
switch ($response->getStatusCode()) {
146+
case 400:
147+
throw new DomainExceptions\ValidationException($body);
145148
case 401:
146149
throw new DomainExceptions\UnauthorizedException();
147150
case 404:

0 commit comments

Comments
 (0)