Skip to content

Commit

Permalink
adjust error handling on upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ata-no-one committed Nov 8, 2024
1 parent 0f6afbb commit 68551f0
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions php/src/vaas/Vaas.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 68551f0

Please sign in to comment.