Skip to content

Commit 68551f0

Browse files
committed
adjust error handling on upload
1 parent 0f6afbb commit 68551f0

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

php/src/vaas/Vaas.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace VaasSdk;
44

55
use Amp\ByteStream\ReadableStream;
6+
use Amp\CompositeCancellation;
67
use Amp\DeferredCancellation;
8+
use Amp\DeferredFuture;
79
use Amp\Http\Client\HttpClient;
810
use Amp\Http\Client\HttpClientBuilder;
911
use Amp\Http\Client\HttpException;
@@ -358,17 +360,23 @@ private function UploadStream(
358360
$request->setBody(StreamedContent::fromStream($fileStream, $fileSize));
359361
$request->addHeader("Content-Length", $fileSize);
360362
$request->addHeader("Authorization", $uploadToken);
361-
362-
$response = $this->httpClient->request($request, new TimeoutCancellation($this->uploadTimeoutInSeconds));
363+
$timeoutCancellation = new TimeoutCancellation($this->uploadTimeoutInSeconds);
364+
$response = $this->httpClient->request($request,
365+
new CompositeCancellation($timeoutCancellation, $cancellation->getCancellation()));
363366
if ($response->getStatus() > 399) {
364367
$reason = $response->getBody()->buffer($cancellation->getCancellation());
365368
throw new UploadFailedException($reason, $response->getStatus());
366369
}
367370
} catch (\Exception $e) {
368-
if ($e instanceof HttpException) {
369-
throw new UploadFailedException($e->getMessage(), $e->getCode());
370-
}
371-
throw new VaasClientException($e->getMessage());
371+
$this->vaasConnection->RemoveResponse($requestId);
372+
if ($e instanceof HttpException) {
373+
$uploadFailedException = new UploadFailedException($e->getMessage(), $e->getCode());
374+
$futureResponse->getFuture()->error($uploadFailedException);
375+
throw new $uploadFailedException;
376+
}
377+
$vaasClientException = new VaasClientException($e->getMessage());
378+
$futureResponse->getFuture()->error($vaasClientException);
379+
throw $vaasClientException;
372380
} finally {
373381
EventLoop::cancel($pingTimer);
374382
$cancellation->cancel();

0 commit comments

Comments
 (0)