Skip to content

Commit b4297e7

Browse files
author
Pantea Marius-ciclistu
committed
Fix http error code on guzzle exception
1 parent 604fd1e commit b4297e7

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/RequestBuilder.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use GuzzleHttp\Client;
77
use GuzzleHttp\Exception\GuzzleException;
8+
use GuzzleHttp\Exception\RequestException;
89
use GuzzleHttp\RequestOptions;
910
use Psr\Http\Message\ResponseInterface;
1011

@@ -55,6 +56,11 @@ public function create(string $resource, array $content): string
5556
{
5657
try {
5758
$response = $this->client->post(\trim($resource, '/'), ['json' => $content]);
59+
} catch (RequestException $e) {
60+
throw new Exception(
61+
(string)\json_encode(['message' => $e->getMessage()]),
62+
$e->getResponse() instanceof ResponseInterface ? $e->getResponse()->getStatusCode() : $e->getCode()
63+
);
5864
} catch (GuzzleException $e) {
5965
throw new Exception((string)\json_encode(['message' => $e->getMessage()]), $e->getCode());
6066
}
@@ -87,6 +93,11 @@ public function get(
8793
'withRelationsCount' => \array_values($withRelationsCount),
8894
'withRelationsExistence' => \array_values($withRelationsExistence),
8995
], '', '&', PHP_QUERY_RFC3986));
96+
} catch (RequestException $e) {
97+
throw new Exception(
98+
(string)\json_encode(['message' => $e->getMessage()]),
99+
$e->getResponse() instanceof ResponseInterface ? $e->getResponse()->getStatusCode() : $e->getCode()
100+
);
90101
} catch (GuzzleException $e) {
91102
throw new Exception((string)\json_encode(['message' => $e->getMessage()]), $e->getCode());
92103
}
@@ -117,6 +128,11 @@ public function update(
117128
$response = $this->client->put(\trim($resource, '/') . '/' . $identifier . (
118129
$relation !== '' && $relatedIdentifier !== '' ? '/' . $relation . '/' . $relatedIdentifier : ''
119130
), ['json' => $content]);
131+
} catch (RequestException $e) {
132+
throw new Exception(
133+
(string)\json_encode(['message' => $e->getMessage()]),
134+
$e->getResponse() instanceof ResponseInterface ? $e->getResponse()->getStatusCode() : $e->getCode()
135+
);
120136
} catch (GuzzleException $e) {
121137
throw new Exception((string)\json_encode(['message' => $e->getMessage()]), $e->getCode());
122138
}
@@ -137,6 +153,11 @@ public function delete(
137153
$response = $this->client->delete(\trim($resource, '/') . '/' . $identifier . (
138154
$relation !== '' && $relatedIdentifier !== '' ? '/' . $relation . '/' . $relatedIdentifier : ''
139155
));
156+
} catch (RequestException $e) {
157+
throw new Exception(
158+
(string)\json_encode(['message' => $e->getMessage()]),
159+
$e->getResponse() instanceof ResponseInterface ? $e->getResponse()->getStatusCode() : $e->getCode()
160+
);
140161
} catch (GuzzleException $e) {
141162
throw new Exception((string)\json_encode(['message' => $e->getMessage()]), $e->getCode());
142163
}
@@ -180,6 +201,11 @@ protected function listInOneCall(
180201
$this->client->get(\trim($resource, '/') . '?' . $queryStringBuilder->getUrlQueryString(), [
181202
'headers' => $headers
182203
]);
204+
} catch (RequestException $e) {
205+
throw new Exception(
206+
(string)\json_encode(['message' => $e->getMessage()]),
207+
$e->getResponse() instanceof ResponseInterface ? $e->getResponse()->getStatusCode() : $e->getCode()
208+
);
183209
} catch (GuzzleException $e) {
184210
throw new Exception((string)\json_encode(['message' => $e->getMessage()]), $e->getCode());
185211
}

0 commit comments

Comments
 (0)