Skip to content

Commit

Permalink
feat: improved error UX (#15)
Browse files Browse the repository at this point in the history
Co-authored-by: Pete Gautier <pete@momentohq.com>
  • Loading branch information
pgautier404 and pgautier404 authored Oct 13, 2022
1 parent 6852fbb commit 8979539
Show file tree
Hide file tree
Showing 6 changed files with 603 additions and 179 deletions.
263 changes: 240 additions & 23 deletions src/Cache/CacheOperationTypes/CacheOperationTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,173 @@
namespace Momento\Cache\CacheOperationTypes;

use Cache_client\_GetResponse;
use Cache_client\ECacheResult;
use Control_client\_ListCachesResponse;
use Cache_client\_SetResponse;
use Momento\Cache\Errors\InternalServerError;
use Momento\Cache\Errors\MomentoErrorCode;
use Momento\Cache\Errors\SdkError;

class CreateCacheResponse {}
trait ErrorBody {
private SdkError $innerException;
private MomentoErrorCode $errorCode;
private string $message;

class DeleteCacheResponse {}
public function __construct(SdkError $error)
{
parent::__construct();
$this->innerException = $error;
$this->message = "{$error->messageWrapper}: {$error->getMessage()}";
$this->errorCode = $error->errorCode;
}

public function innerException() : SdkError
{
return $this->innerException;
}

public function errorCode() : MomentoErrorCode
{
return $this->errorCode;
}

class ListCachesResponse
public function message() : string
{
return $this->message;
}
}

abstract class ResponseBase
{
protected string $baseType;

public function __construct() {
$this->baseType = get_parent_class($this);
}

protected function isError() : bool
{
return get_class($this) == "{$this->baseType}Error";
}

protected function isSuccess() : bool
{
return get_class($this) == "{$this->baseType}Success";
}

protected function isAlreadyExists() : bool
{
return get_class($this) == "{$this->baseType}AlreadyExists";
}

protected function isHit() : bool
{
return get_class($this) == "{$this->baseType}Hit";
}

protected function isMiss() : bool
{
return get_class($this) == "{$this->baseType}Miss";
}
}

abstract class CreateCacheResponse extends ResponseBase {

public function asSuccess() : CreateCacheResponseSuccess|null
{
if ($this->isSuccess())
{
return $this;
}
return null;
}

public function asError() : CreateCacheResponseError|null
{
if ($this->isError())
{
return $this;
}
return null;
}

public function asAlreadyExists() : CreateCacheResponseAlreadyExists|null
{
if ($this->isAlreadyExists())
{
return $this;
}
return null;
}

}

class CreateCacheResponseSuccess extends CreateCacheResponse { }

class CreateCacheResponseAlreadyExists extends CreateCacheResponse { }

class CreateCacheResponseError extends CreateCacheResponse
{
use ErrorBody;
}

abstract class DeleteCacheResponse extends ResponseBase {
public function asSuccess() : DeleteCacheResponseSuccess|null
{
if ($this->isSuccess())
{
return $this;
}
return null;
}

public function asError() : DeleteCacheResponseError|null
{
if ($this->isError())
{
return $this;
}
return null;
}
}

class DeleteCacheResponseSuccess extends DeleteCacheResponse { }

class DeleteCacheResponseError extends DeleteCacheResponse
{
use ErrorBody;
}

abstract class ListCachesResponse extends ResponseBase {

public function asSuccess() : ListCachesResponseSuccess|null
{
if ($this->isSuccess())
{
return $this;
}
return null;
}

public function asError() : ListCachesResponseError|null
{
if ($this->isError())
{
return $this;
}
return null;
}

}

class ListCachesResponseSuccess extends ListCachesResponse
{
private string $nextToken;
private array $caches = [];

public function __construct(_ListCachesResponse $response) {
parent::__construct();
$this->nextToken = $response->getNextToken() ? $response->getNextToken() : "";
foreach ($response->getCache() as $cache) {
array_push($this->caches , new CacheInfo($cache));
$this->caches[] = new CacheInfo($cache);
}
}

Expand All @@ -35,6 +183,11 @@ public function nextToken() : string
}
}

class ListCachesResponseError extends ListCachesResponse
{
use ErrorBody;
}

class CacheInfo
{
private string $name;
Expand All @@ -48,14 +201,33 @@ public function name() : string {
}
}

class CacheSetResponse
abstract class CacheSetResponse extends ResponseBase
{
public function asSuccess(): CacheSetResponseSuccess|null
{
if ($this->isSuccess()) {
return $this;
}
return null;
}

public function asError(): CacheSetResponseError|null
{
if ($this->isError()) {
return $this;
}
return null;
}
}

class CacheSetResponseSuccess extends CacheSetResponse
{
private string $key;
private string $value;

public function __construct(_SetResponse $grpcSetResponse, string $key, string $value)
{
parent::__construct();
$this->key = $key;
$this->value = $value;
}
Expand All @@ -70,38 +242,83 @@ public function value() : string {

}

enum CacheGetStatus {
case HIT;
case MISS;
class CacheSetResponseError extends CacheSetResponse
{
use ErrorBody;
}

class CacheGetResponse {
abstract class CacheGetResponse extends ResponseBase
{

public function asHit() : CacheGetResponseHit|null
{
if ($this->isHit()) {
return $this;
}
return null;
}

public function asMiss() : CacheGetResponseMiss|null
{
if ($this->isMiss()) {
return $this;
}
return null;
}

public function asError() : CacheGetResponseError|null
{
if ($this->isError()) {
return $this;
}
return null;
}
}

class CacheGetResponseHit extends CacheGetResponse
{
private string $value;
private CacheGetStatus $status;

public function __construct(_GetResponse $grpcGetResponse) {
parent::__construct();
$this->value = $grpcGetResponse->getCacheBody();
$ecacheResult = $grpcGetResponse->getResult();
if ($ecacheResult == ECacheResult::Hit) {
$this->status = CacheGetStatus::HIT;
} else if ($ecacheResult == ECacheResult::Miss) {
$this->status = CacheGetStatus::MISS;
} else {
throw new InternalServerError("CacheService returned an unexpected result: $ecacheResult");
}
}

public function value() : string
{
return $this->value;
}

public function status() : string
}

class CacheGetResponseMiss extends CacheGetResponse { }

class CacheGetResponseError extends CacheGetResponse
{
use ErrorBody;
}

abstract class CacheDeleteResponse extends ResponseBase {
public function asSuccess(): CacheDeleteResponseSuccess|null
{
return $this->status->name;
if ($this->isSuccess()) {
return $this;
}
return null;
}

public function asError(): CacheDeleteResponseError|null
{
if ($this->isError()) {
return $this;
}
return null;
}
}

class CacheDeleteResponse {}
class CacheDeleteResponseSuccess extends CacheDeleteResponse { }

class CacheDeleteResponseError extends CacheDeleteResponse
{
use ErrorBody;
}
Loading

0 comments on commit 8979539

Please sign in to comment.