Skip to content

Commit

Permalink
Make error classes' props private and expose public methods
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-balitskyi committed Jan 3, 2025
1 parent 724bef3 commit 2999c93
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 33 deletions.
7 changes: 6 additions & 1 deletion src/Exceptions/SeamActionAttemptError.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

class SeamActionAttemptError extends \Exception
{
public ActionAttempt $actionAttempt;
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;
}
}
7 changes: 6 additions & 1 deletion src/Exceptions/SeamActionAttemptFailedError.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

class SeamActionAttemptFailedError extends SeamActionAttemptError
{
public string $errorCode;
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;
}
}
8 changes: 4 additions & 4 deletions src/Exceptions/SeamHttpApiError.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

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

public function __construct(object $error, int $statusCode, string $requestId)
{
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\SeamHttpApiError $exception) {
$this->assertTrue(
str_contains($exception->getMessage(), "Access Code Not Found")
);
Expand Down
12 changes: 6 additions & 6 deletions tests/ActionAttemptsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function testFailedActionAttempt(): void
"status" => "error",
"error" => [
"message" => "Failed",
"type" => "foo"
"type" => "failed_attempt"
]
]
);
Expand All @@ -60,9 +60,9 @@ public function testFailedActionAttempt(): void
$seam->action_attempts->poll_until_ready($action_attempt->action_attempt_id);
$this->fail("Expected SeamActionAttemptFailedError");
} catch (\Seam\SeamActionAttemptFailedError $e) {
$this->assertEquals($action_attempt->action_attempt_id, $e->actionAttempt->action_attempt_id);
$this->assertEquals("error", $e->actionAttempt->status);
$this->assertEquals("foo", $e->errorCode);
$this->assertEquals($action_attempt->action_attempt_id, $e->getActionAttempt()->action_attempt_id);
$this->assertEquals("error", $e->getActionAttempt()->status);
$this->assertEquals("failed_attempt", $e->getErrorCode());
$this->assertEquals("Failed", $e->getMessage());
}
}
Expand Down Expand Up @@ -92,8 +92,8 @@ public function testTimeoutActionAttempt(): void
$seam->action_attempts->poll_until_ready($action_attempt->action_attempt_id, 0.1);
$this->fail("Expected TimeoutError");
} catch (\Seam\SeamActionAttemptTimeoutError $e) {
$this->assertEquals($action_attempt->action_attempt_id, $e->actionAttempt->action_attempt_id);
$this->assertEquals("pending", $e->actionAttempt->status);
$this->assertEquals($action_attempt->action_attempt_id, $e->getActionAttempt()->action_attempt_id);
$this->assertEquals("pending", $e->getActionAttempt()->status);
$this->assertEquals("Timed out waiting for action attempt after 0.1s", $e->getMessage());
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/ClientSessionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPUnit\Framework\TestCase;
use Tests\Fixture;
use GuzzleHttp\Client;

final class ClientSessionsTest extends TestCase
{
Expand Down
1 change: 0 additions & 1 deletion tests/ConnectWebviewsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPUnit\Framework\TestCase;
use Tests\Fixture;
use GuzzleHttp\Client;

final class ConnectWebviewsTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/ConnectedAccountsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testDeleteConnectedAccount(): void
} catch (\Seam\SeamHttpApiError $exception) {
$this->assertTrue(
str_contains(
$exception->errorCode,
$exception->getErrorCode(),
"connected_account_not_found"
)
);
Expand Down
5 changes: 2 additions & 3 deletions tests/DevicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPUnit\Framework\TestCase;
use Tests\Fixture;
use GuzzleHttp\Client;

final class DevicesTest extends TestCase
{
Expand Down Expand Up @@ -47,9 +46,9 @@ public function testGetAndListDevices(): void
$seam->devices->get(device_id: $device_id);

$this->fail("Expected the device to be deleted");
} catch (Exception $exception) {
} catch (\Seam\SeamHttpApiError $exception) {
$this->assertTrue(
str_contains($exception->errorCode, "device_not_found")
str_contains($exception->getErrorCode(), "device_not_found")
);
}

Expand Down
3 changes: 1 addition & 2 deletions tests/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPUnit\Framework\TestCase;
use Tests\Fixture;
use GuzzleHttp\Client;

final class EventsTest extends TestCase
{
Expand Down Expand Up @@ -38,7 +37,7 @@ public function testListEventsError(): void
$seam = Fixture::getTestServer();
try {
$seam->events->list(since: "invalid_date");
} catch (Exception $e) {
} catch (\Seam\SeamHttpInvalidInputError $e) {
$this->assertTrue(
str_contains($e->getMessage(), "Must be parsable date string")
);
Expand Down
14 changes: 7 additions & 7 deletions tests/HttpErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function testInvalidInputError(): void
);
$this->fail("Expected InvalidInputError");
} catch (\Seam\SeamHttpInvalidInputError $e) {
$this->assertEquals(400, $e->statusCode);
$this->assertNotEmpty($e->requestId);
$this->assertEquals('invalid_input', $e->errorCode);
$this->assertEquals(400, $e->getStatusCode());
$this->assertNotEmpty($e->getRequestId());
$this->assertEquals('invalid_input', $e->getErrorCode());
$this->assertEquals(
['Expected array, received number'],
$e->getValidationErrorMessages('device_ids')
Expand All @@ -43,7 +43,7 @@ public function testUnauthorizedError(): void
$seam->devices->list();
$this->fail("Expected UnauthorizedError");
} catch (\Seam\SeamHttpUnauthorizedError $e) {
$this->assertNotEmpty($e->requestId);
$this->assertNotEmpty($e->getRequestId());
}
}

Expand All @@ -55,9 +55,9 @@ public function testApiError(): void
$seam->devices->get(device_id: "nonexistent_device_id");
$this->fail("Expected ApiError");
} catch (\Seam\SeamHttpApiError $e) {
$this->assertEquals(404, $e->statusCode);
$this->assertNotEmpty($e->requestId);
$this->assertEquals('device_not_found', $e->errorCode);
$this->assertEquals(404, $e->getStatusCode());
$this->assertNotEmpty($e->getRequestId());
$this->assertEquals('device_not_found', $e->getErrorCode());
}
}

Expand Down
1 change: 0 additions & 1 deletion tests/LocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPUnit\Framework\TestCase;
use Tests\Fixture;
use GuzzleHttp\Client;

final class LocksTest extends TestCase
{
Expand Down
1 change: 0 additions & 1 deletion tests/WorkspacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPUnit\Framework\TestCase;
use Tests\Fixture;
use GuzzleHttp\Client;

final class WorkspacesTest extends TestCase
{
Expand Down

0 comments on commit 2999c93

Please sign in to comment.