Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
handles HTTP client and server exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhineng committed Dec 4, 2023
1 parent f4cfc6e commit 23248a1
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 30 deletions.
11 changes: 0 additions & 11 deletions src/BatchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Dew\Tablestore\Concerns\InteractsWithRequest;
use Dew\Tablestore\Exceptions\BatchHandlerException;
use Google\Protobuf\Internal\Message;
use Protos\BatchGetRowRequest;
use Protos\BatchGetRowResponse;
use Protos\BatchWriteRowRequest;
Expand Down Expand Up @@ -229,14 +228,4 @@ protected function toChangesRequest(BatchBuilder $builder): RowInBatchWriteRowRe
->setCondition($this->toCondition($builder))
->setReturnContent($this->toReturnContent($builder));
}

/**
* Communicate with Tablestore with the given message.
*/
protected function send(string $endpoint, Message $message): string
{
return $this->tablestore->send($endpoint, $message)
->getBody()
->getContents();
}
}
31 changes: 31 additions & 0 deletions src/Concerns/InteractsWithRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,37 @@
use Dew\Tablestore\BatchBuilder;
use Dew\Tablestore\Builder;
use Dew\Tablestore\ConditionFilter;
use Dew\Tablestore\Exceptions\TablestoreException;
use Dew\Tablestore\PaginationFilter;
use Dew\Tablestore\Tablestore;
use Google\Protobuf\Internal\Message;
use GuzzleHttp\Exception\BadResponseException;
use Protos\Condition;
use Protos\Filter;
use Protos\ReturnContent;
use RuntimeException;

trait InteractsWithRequest
{
/**
* The Tablestore client.
*/
protected Tablestore $tablestore;

/**
* Communicate with Tablestore with the given message.
*/
protected function send(string $endpoint, Message $message): string
{
try {
return $this->tablestore()->send($endpoint, $message)
->getBody()
->getContents();
} catch (BadResponseException $e) {
throw TablestoreException::fromResponse($e->getResponse());
}
}

/**
* Build a condition Protobuf message.
*/
Expand Down Expand Up @@ -76,4 +99,12 @@ public function buildFilter(BatchBuilder|Builder $builder): Filter

throw new RuntimeException('Missing filter data to build with.');
}

/**
* The Tablestore client.
*/
public function tablestore(): Tablestore
{
return $this->tablestore;
}
}
38 changes: 38 additions & 0 deletions src/Exceptions/TablestoreException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Dew\Tablestore\Exceptions;

use Exception;
use Protos\Error;
use Psr\Http\Message\ResponseInterface;

class TablestoreException extends Exception
{
/**
* Create a Tablestore exception.
*/
public function __construct(
protected Error $e
) {
parent::__construct($e->getMessage());
}

/**
* Create a Tablestore exception from error response.
*/
public static function fromResponse(ResponseInterface $response): self
{
$error = new Error;
$error->mergeFromString($response->getBody()->getContents());

return new self($error);
}

/**
* Get the error message.
*/
public function getError(): Error
{
return $this->e;
}
}
19 changes: 0 additions & 19 deletions src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Dew\Tablestore\Concerns\InteractsWithRequest;
use Dew\Tablestore\Responses\RowDecodableResponse;
use Google\Protobuf\Internal\Message;
use Protos\DeleteRowRequest;
use Protos\DeleteRowResponse;
use Protos\GetRowRequest;
Expand Down Expand Up @@ -121,22 +120,4 @@ public function getRow(Builder $builder): RowDecodableResponse

return new RowDecodableResponse($response);
}

/**
* Communicate with Tablestore with the given message.
*/
protected function send(string $endpoint, Message $message): string
{
return $this->tablestore->send($endpoint, $message)
->getBody()
->getContents();
}

/**
* The tablestore client.
*/
public function tablestore(): Tablestore
{
return $this->tablestore;
}
}
31 changes: 31 additions & 0 deletions tests/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

use Dew\Tablestore\Attribute;
use Dew\Tablestore\Builder;
use Dew\Tablestore\Exceptions\TablestoreException;
use Dew\Tablestore\Handler;
use Dew\Tablestore\PrimaryKey;
use Dew\Tablestore\Tablestore;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Psr7\Response;
use Protos\Error;
use Protos\Filter;
use Protos\FilterType;
use Psr\Http\Message\RequestInterface;

test('filter build determination primary keys', function () {
$handler = new Handler(Mockery::mock(Tablestore::class));
Expand Down Expand Up @@ -169,3 +174,29 @@
->maxVersions(2)
->get();
});

test('handles http client error', function () {
$error = (new Error)->setCode('foo')->setMessage('bar');
$exception = new ClientException('',
Mockery::mock(RequestInterface::class),
new Response(body: $error->serializeToString())
);
$mockedTs = Mockery::mock(Tablestore::class);
$mockedTs->expects()->send(Mockery::any(), Mockery::any())->andThrows($exception);
$handler = new Handler($mockedTs);
$builder = (new Builder)->setTable('test')->handlerUsing($handler);
expect(fn () => $builder->get())->toThrow(TablestoreException::class, 'bar');
});

test('handles http server error', function () {
$error = (new Error)->setCode('foo')->setMessage('bar');
$exception = new ServerException('',
Mockery::mock(RequestInterface::class),
new Response(body: $error->serializeToString())
);
$mockedTs = Mockery::mock(Tablestore::class);
$mockedTs->expects()->send(Mockery::any(), Mockery::any())->andThrows($exception);
$handler = new Handler($mockedTs);
$builder = (new Builder)->setTable('test')->handlerUsing($handler);
expect(fn () => $builder->get())->toThrow(TablestoreException::class, 'bar');
});

0 comments on commit 23248a1

Please sign in to comment.