From 0c548f0ce9d5d83b429e8f7daaad8560f0486276 Mon Sep 17 00:00:00 2001 From: Artur Neumann Date: Fri, 27 Nov 2020 14:04:27 +0545 Subject: [PATCH] fix(client): Handle Guzzle ClientException (#324) --- src/Tus/Client.php | 10 +++++++--- tests/Tus/ClientTest.php | 8 +++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Tus/Client.php b/src/Tus/Client.php index bf2fc5a3..55e7278d 100644 --- a/src/Tus/Client.php +++ b/src/Tus/Client.php @@ -437,9 +437,13 @@ public function create(string $key) : string $headers += ['Upload-Concat' => 'partial']; } - $response = $this->getClient()->post($this->apiPath, [ - 'headers' => $headers, - ]); + try { + $response = $this->getClient()->post($this->apiPath, [ + 'headers' => $headers, + ]); + } catch (ClientException $e) { + $response = $e->getResponse(); + } $statusCode = $response->getStatusCode(); diff --git a/tests/Tus/ClientTest.php b/tests/Tus/ClientTest.php index 9cd68329..60838c88 100644 --- a/tests/Tus/ClientTest.php +++ b/tests/Tus/ClientTest.php @@ -1367,6 +1367,12 @@ public function it_throws_exception_when_unable_to_create_resource() : void $this->tusClientMock->file($filePath, $fileName); + $clientExceptionMock = m::mock(ClientException::class); + $clientExceptionMock + ->shouldReceive('getResponse') + ->once() + ->andReturn($responseMock); + $guzzleMock ->shouldReceive('post') ->once() @@ -1378,7 +1384,7 @@ public function it_throws_exception_when_unable_to_create_resource() : void 'Upload-Metadata' => 'filename ' . base64_encode($fileName), ], ]) - ->andReturn($responseMock); + ->andThrow($clientExceptionMock); $this->tusClientMock->create($key); }