From 68551f005628df3d042e30b15bd682fecd4c9482 Mon Sep 17 00:00:00 2001 From: PT-ATA No One Date: Fri, 8 Nov 2024 14:40:27 +0000 Subject: [PATCH] adjust error handling on upload --- php/src/vaas/Vaas.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/php/src/vaas/Vaas.php b/php/src/vaas/Vaas.php index d6f68fff..052aca6a 100644 --- a/php/src/vaas/Vaas.php +++ b/php/src/vaas/Vaas.php @@ -3,7 +3,9 @@ namespace VaasSdk; use Amp\ByteStream\ReadableStream; +use Amp\CompositeCancellation; use Amp\DeferredCancellation; +use Amp\DeferredFuture; use Amp\Http\Client\HttpClient; use Amp\Http\Client\HttpClientBuilder; use Amp\Http\Client\HttpException; @@ -358,17 +360,23 @@ private function UploadStream( $request->setBody(StreamedContent::fromStream($fileStream, $fileSize)); $request->addHeader("Content-Length", $fileSize); $request->addHeader("Authorization", $uploadToken); - - $response = $this->httpClient->request($request, new TimeoutCancellation($this->uploadTimeoutInSeconds)); + $timeoutCancellation = new TimeoutCancellation($this->uploadTimeoutInSeconds); + $response = $this->httpClient->request($request, + new CompositeCancellation($timeoutCancellation, $cancellation->getCancellation())); if ($response->getStatus() > 399) { $reason = $response->getBody()->buffer($cancellation->getCancellation()); throw new UploadFailedException($reason, $response->getStatus()); } } catch (\Exception $e) { - if ($e instanceof HttpException) { - throw new UploadFailedException($e->getMessage(), $e->getCode()); - } - throw new VaasClientException($e->getMessage()); + $this->vaasConnection->RemoveResponse($requestId); + if ($e instanceof HttpException) { + $uploadFailedException = new UploadFailedException($e->getMessage(), $e->getCode()); + $futureResponse->getFuture()->error($uploadFailedException); + throw new $uploadFailedException; + } + $vaasClientException = new VaasClientException($e->getMessage()); + $futureResponse->getFuture()->error($vaasClientException); + throw $vaasClientException; } finally { EventLoop::cancel($pingTimer); $cancellation->cancel();