|
16 | 16 | use Symfony\Component\HttpClient\Exception\ServerException;
|
17 | 17 | use Symfony\Component\HttpClient\TraceableHttpClient;
|
18 | 18 | use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
| 19 | +use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; |
| 20 | +use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface; |
19 | 21 | use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
20 | 22 | use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
21 | 23 | use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
@@ -52,24 +54,36 @@ public function getHeaders(bool $throw = true): array
|
52 | 54 |
|
53 | 55 | public function getContent(bool $throw = true): string
|
54 | 56 | {
|
55 |
| - $this->content = $this->response->getContent(false); |
56 |
| - |
57 | 57 | if ($throw) {
|
58 |
| - $this->checkStatusCode($this->response->getStatusCode()); |
| 58 | + try { |
| 59 | + $this->checkStatusCode($this->response->getStatusCode()); |
| 60 | + } catch (HttpExceptionInterface $httpException) { |
| 61 | + try { |
| 62 | + return $this->content = $this->response->getContent(false); |
| 63 | + } catch (TransportExceptionInterface $e) { |
| 64 | + throw $httpException ?? $e; |
| 65 | + } |
| 66 | + } |
59 | 67 | }
|
60 | 68 |
|
61 |
| - return $this->content; |
| 69 | + return $this->content = $this->response->getContent(false); |
62 | 70 | }
|
63 | 71 |
|
64 | 72 | public function toArray(bool $throw = true): array
|
65 | 73 | {
|
66 |
| - $this->content = $this->response->toArray(false); |
67 |
| - |
68 | 74 | if ($throw) {
|
69 |
| - $this->checkStatusCode($this->response->getStatusCode()); |
| 75 | + try { |
| 76 | + $this->checkStatusCode($this->response->getStatusCode()); |
| 77 | + } catch (HttpExceptionInterface $httpException) { |
| 78 | + try { |
| 79 | + return $this->content = $this->response->toArray(false); |
| 80 | + } catch (DecodingExceptionInterface | TransportExceptionInterface $e) { |
| 81 | + throw $httpException ?? $e; |
| 82 | + } |
| 83 | + } |
70 | 84 | }
|
71 | 85 |
|
72 |
| - return $this->content; |
| 86 | + return $this->content = $this->response->toArray(false); |
73 | 87 | }
|
74 | 88 |
|
75 | 89 | public function cancel(): void
|
|
0 commit comments