Skip to content

Commit

Permalink
Added Error->isRetryable() and ResourceExhausted (#10)
Browse files Browse the repository at this point in the history
The `Error->isRetryable()` will tell the user whether retrying this error might resolve the issue.

ResourceExhausted error will be retruned when a user sends more requests that is allowed by their quota.
  • Loading branch information
stepanbujnak authored Oct 27, 2024
1 parent 1acf3d6 commit 530a3f3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class Error extends \Exception
{
private bool $retryable;

public static function checkMemory($var, callable $cleanup = null): void
{
if (!$var) {
Expand All @@ -24,7 +26,14 @@ public static function checkStatus($status, callable $cleanup = null): void
$status_code = Lib::get()->Pex_Status_GetCode($status);
$status_message = Lib::get()->Pex_Status_GetMessage($status);

throw new Error($status_message, $status_code);
$err = new Error($status_message, $status_code);
$err->retryable = Lib::get()->Pex_Status_IsRetryable($status);
throw $err;
}
}

public function isRetryable(): bool
{
return $this->retryable;
}
}
3 changes: 2 additions & 1 deletion lib/Lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Pex;

const PEX_SDK_MAJOR_VERSION = 4;
const PEX_SDK_MINOR_VERSION = 4;
const PEX_SDK_MINOR_VERSION = 5;

class Lib
{
Expand Down Expand Up @@ -67,6 +67,7 @@ public static function get(): \FFI
bool Pex_Status_OK(const Pex_Status *status);
int Pex_Status_GetCode(const Pex_Status *status);
const char *Pex_Status_GetMessage(const Pex_Status *status);
bool Pex_Status_IsRetryable(const Pex_Status *status);
// ----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions lib/StatusCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ class StatusCode
public const ConnectionError = 9;
public const LookupFailed = 10;
public const LookupTimedOut = 11;
public const ResourceExhausted = 12;
}

0 comments on commit 530a3f3

Please sign in to comment.