Skip to content

Commit

Permalink
chore: improve resource exhausted error message (#247)
Browse files Browse the repository at this point in the history
* chore: improve resource exhausted error message

* make sure to user lower case grpc error message when parsing
  • Loading branch information
anitarua authored Nov 5, 2024
1 parent fbb430a commit c29417e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
61 changes: 60 additions & 1 deletion src/Cache/Errors/Errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,66 @@ class InvalidArgumentError extends SdkError
class LimitExceededError extends SdkError
{
public string $errorCode = MomentoErrorCode::LIMIT_EXCEEDED_ERROR;
public string $messageWrapper = 'Request rate, bandwidth, or object size exceeded the limits for this account. To resolve this error, reduce your request rate, or contact us at [email protected] to request a limit increase';
public string $messageWrapper = LimitExceededMessageWrapper::UNKNOWN_LIMIT_EXCEEDED;

public function __construct(
string $message, int $code = 0, ?\Throwable $previous = null, ?array $metadata = null
)
{
parent::__construct($message, $code, $previous);
$this->setMessageWrapper($message, $metadata);
}

private function setMessageWrapper(string $message, ?array $metadata = null) {
// If provided, we use the `err` metadata value to determine the most
// appropriate error message to set.
if (array_key_exists("err", $metadata)) {
$errCause = $metadata["err"][0];
if (!is_null($errCause)) {
$this->messageWrapper = LimitExceededMessageWrapper::$errorCauseToLimitExceededMessage[$errCause];
return;
}
}

// If `err` metadata is unavailable, try to use the error details field
// to set an appropriate error message.
$lowerCasedMessage = strtolower($message);
if (str_contains($lowerCasedMessage, "subscribers")) {
$this->messageWrapper = LimitExceededMessageWrapper::TOPIC_SUBSCRIPTIONS_LIMIT_EXCEEDED;
} elseif (str_contains($lowerCasedMessage, "operations")) {
$this->messageWrapper = LimitExceededMessageWrapper::OPERATIONS_RATE_LIMIT_EXCEEDED;
} elseif (str_contains($lowerCasedMessage, "throughput")) {
$this->messageWrapper = LimitExceededMessageWrapper::THROUGHPUT_RATE_LIMIT_EXCEEDED;
} elseif (str_contains($lowerCasedMessage, "request limit")) {
$this->messageWrapper = LimitExceededMessageWrapper::REQUEST_SIZE_LIMIT_EXCEEDED;
} elseif (str_contains($lowerCasedMessage, "item size")) {
$this->messageWrapper = LimitExceededMessageWrapper::ITEM_SIZE_LIMIT_EXCEEDED;
} elseif (str_contains($lowerCasedMessage, "element size")) {
$this->messageWrapper = LimitExceededMessageWrapper::ELEMENT_SIZE_LIMIT_EXCEEDED;
} else {
// If all else fails, set a generic "limit exceeded" message
$this->messageWrapper = LimitExceededMessageWrapper::UNKNOWN_LIMIT_EXCEEDED;
}
}
}

abstract class LimitExceededMessageWrapper {
public const TOPIC_SUBSCRIPTIONS_LIMIT_EXCEEDED = "Topic subscriptions limit exceeded for this account";
public const OPERATIONS_RATE_LIMIT_EXCEEDED = "Request rate limit exceeded for this account";
public const THROUGHPUT_RATE_LIMIT_EXCEEDED = "Bandwidth limit exceeded for this account";
public const REQUEST_SIZE_LIMIT_EXCEEDED = "Request size limit exceeded for this account";
public const ITEM_SIZE_LIMIT_EXCEEDED = "Item size limit exceeded for this account";
public const ELEMENT_SIZE_LIMIT_EXCEEDED = "Element size limit exceeded for this account";
public const UNKNOWN_LIMIT_EXCEEDED = "Limit exceeded for this account";

public static array $errorCauseToLimitExceededMessage = [
"topic_subscriptions_limit_exceeded" => LimitExceededMessageWrapper::TOPIC_SUBSCRIPTIONS_LIMIT_EXCEEDED,
"operations_rate_limit_exceeded" => LimitExceededMessageWrapper::OPERATIONS_RATE_LIMIT_EXCEEDED,
"throughput_rate_limit_exceeded" => LimitExceededMessageWrapper::THROUGHPUT_RATE_LIMIT_EXCEEDED,
"request_size_limit_exceeded" => LimitExceededMessageWrapper::REQUEST_SIZE_LIMIT_EXCEEDED,
"item_size_limit_exceeded" => LimitExceededMessageWrapper::ITEM_SIZE_LIMIT_EXCEEDED,
"element_size_limit_exceeded" => LimitExceededMessageWrapper::ELEMENT_SIZE_LIMIT_EXCEEDED,
];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/_ErrorConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function convert($grpcStatus, ?array $metadata = null): SdkError
} else {
$class = self::$rpcToError[$status];
}
return new $class($details, $status, null, $metadata);
return new $class($details, $status, null, $grpcStatus->metadata);
}

return new UnknownError(
Expand Down

0 comments on commit c29417e

Please sign in to comment.