Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Seam errors #240

Merged
merged 17 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"autoload": {
"psr-4": {
"Seam\\": "src/",
"Seam\\": ["src/", "src/Exceptions/"],
"Tests\\": "tests/"
}
},
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"devDependencies": {
"@prettier/plugin-php": "^0.22.1",
"@seamapi/nextlove-sdk-generator": "1.15.0",
"@seamapi/nextlove-sdk-generator": "1.15.2",
"@seamapi/types": "1.327.0",
"del": "^7.1.0",
"prettier": "^3.0.0"
Expand Down
22 changes: 22 additions & 0 deletions src/Exceptions/ActionAttemptError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Seam;

use Seam\Objects\ActionAttempt;

class ActionAttemptError extends \Exception
{
private ActionAttempt $actionAttempt;

public function __construct(string $message, ActionAttempt $actionAttempt)
{
parent::__construct($message);
$this->name = get_class($this);
$this->actionAttempt = $actionAttempt;
}

public function getActionAttempt(): ActionAttempt
{
return $this->actionAttempt;
}
}
22 changes: 22 additions & 0 deletions src/Exceptions/ActionAttemptFailedError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Seam;

use Seam\Objects\ActionAttempt;

class ActionAttemptFailedError extends ActionAttemptError
{
private string $errorCode;

public function __construct(ActionAttempt $actionAttempt)
{
parent::__construct($actionAttempt->error->message, $actionAttempt);
$this->name = get_class($this);
$this->errorCode = $actionAttempt->error->type;
}

public function getErrorCode(): string
{
return $this->errorCode;
}
}
17 changes: 17 additions & 0 deletions src/Exceptions/ActionAttemptTimeoutError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Seam;

use Seam\Objects\ActionAttempt;

class ActionAttemptTimeoutError extends ActionAttemptError
{
public function __construct(ActionAttempt $actionAttempt, float $timeout)
{
parent::__construct(
"Timed out waiting for action attempt after {$timeout}s",
$actionAttempt
);
$this->name = get_class($this);
}
}
44 changes: 44 additions & 0 deletions src/Exceptions/HttpApiError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Seam;

class HttpApiError extends \Exception
{
private string $errorCode;
private int $statusCode;
private string $requestId;
private ?object $data = null;

public function __construct(
object $error,
int $statusCode,
string $requestId
) {
$message = $error->message ?? "Unknown error";
parent::__construct($message);
$this->errorCode = $error->type ?? "unknown";
$this->statusCode = $statusCode;
$this->requestId = $requestId;
$this->data = $error->data ?? null;
}

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

public function getStatusCode(): int
{
return $this->statusCode;
}

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

public function getData(): mixed
{
return $this->data;
}
}
23 changes: 23 additions & 0 deletions src/Exceptions/HttpInvalidInputError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Seam;

class HttpInvalidInputError extends HttpApiError
{
private object $validationErrors;

public function __construct(
object $error,
int $statusCode,
string $requestId
) {
parent::__construct($error, $statusCode, $requestId);
$this->errorCode = "invalid_input";
$this->validationErrors = $error->validation_errors ?? (object) [];
}

public function getValidationErrorMessages(string $paramName): array
{
return $this->validationErrors->{$paramName}->_errors ?? [];
}
}
15 changes: 15 additions & 0 deletions src/Exceptions/HttpUnauthorizedError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Seam;

class HttpUnauthorizedError extends HttpApiError
{
public function __construct(string $requestId)
{
$error = (object) [
"type" => "unauthorized",
"message" => "Unauthorized",
];
parent::__construct($error, 401, $requestId);
}
}
100 changes: 39 additions & 61 deletions src/SeamClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@

use GuzzleHttp\Client as HTTPClient;
use \Exception as Exception;
use Seam\HttpApiError;
use Seam\HttpUnauthorizedError;
use Seam\HttpInvalidInputError;
use Seam\ActionAttemptFailedError;
use Seam\ActionAttemptTimeoutError;

define("LTS_VERSION", "1.0.0");

Expand Down Expand Up @@ -119,60 +124,34 @@ public function request(
} catch (Exception $ignoreError) {
}

if (($res_json->error ?? null) != null) {
throw new Exception(
"Error Calling \"" .
$method .
" " .
$path .
"\" : " .
($res_json->error->type ?? "") .
": " .
$res_json->error->message .
" [Request ID: " .
$request_id .
"]"
);
}

if ($status_code >= 400) {
$error_message = $response->getReasonPhrase();

throw new Exception(
"HTTP Error: " .
$error_message .
" [" .
$status_code .
"] " .
$method .
" " .
$path .
" [Request ID: " .
$request_id .
"]"
);
}
if ($status_code === 401) {
throw new HttpUnauthorizedError($request_id);
}

if ($inner_object) {
if (
!is_array($res_json->$inner_object) &&
($res_json->$inner_object ?? null) == null
) {
throw new Exception(
'Missing Inner Object "' .
$inner_object .
'" for ' .
$method .
" " .
$path .
" [Request ID: " .
$request_id .
"]"
if (($res_json->error ?? null) != null) {
if ($res_json->error->type === "invalid_input") {
throw new HttpInvalidInputError(
$res_json->error,
$status_code,
$request_id
);
}

throw new HttpApiError(
$res_json->error,
$status_code,
$request_id
);
}
return $res_json->$inner_object;

throw GuzzleHttpExceptionRequestException::create(
new GuzzleHttpPsr7Request($method, $path),
$response
);
}
return $res_json;

return $inner_object ? $res_json->$inner_object : $res_json;
}
}

Expand Down Expand Up @@ -2161,29 +2140,28 @@ public function list(array $action_attempt_ids): array

return array_map(fn($r) => ActionAttempt::from_json($r), $res);
}
public function poll_until_ready(string $action_attempt_id): ActionAttempt
{
public function poll_until_ready(
string $action_attempt_id,
float $timeout = 20.0
): ActionAttempt {
$seam = $this->seam;
$time_waiting = 0.0;
$polling_interval = 0.4;
$action_attempt = $seam->action_attempts->get($action_attempt_id);

while ($action_attempt->status == "pending") {
$action_attempt = $seam->action_attempts->get(
$action_attempt->action_attempt_id
);
if ($time_waiting > 20.0) {
throw new Exception(
"Timed out waiting for action attempt to be ready"
);
if ($time_waiting > $timeout) {
throw new ActionAttemptTimeoutError($action_attempt, $timeout);
}
$time_waiting += 0.4;
usleep(400000); // sleep for 0.4 seconds
$time_waiting += $polling_interval;
usleep($polling_interval * 1000000);
}

if ($action_attempt->status == "failed") {
throw new Exception(
"Action Attempt failed: " . $action_attempt->error->message
);
if ($action_attempt->status == "error") {
throw new ActionAttemptFailedError($action_attempt);
}

return $action_attempt;
Expand Down
5 changes: 1 addition & 4 deletions tests/AccessCodesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

use PHPUnit\Framework\TestCase;
use Tests\Fixture;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientErrorResponseException as ClientErrorResponseException;
use GuzzleHttp\Promise\Is;

final class AccessCodesTest extends TestCase
{
Expand Down Expand Up @@ -62,7 +59,7 @@ public function testAccessCodes(): void
$seam->access_codes->get(access_code_id: $access_code_id);

$this->fail("Expected the code to be deleted");
} catch (Exception $exception) {
} catch (\Seam\HttpApiError $exception) {
$this->assertTrue(
str_contains($exception->getMessage(), "Access Code Not Found")
);
Expand Down
Loading
Loading