Skip to content

Commit

Permalink
refactor: make all exceptions extend BlueskyException
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi committed Feb 10, 2024
1 parent 6ba12a4 commit 4341d22
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 29 deletions.
28 changes: 28 additions & 0 deletions src/Exceptions/BlueskyClientException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace NotificationChannels\Bluesky\Exceptions;

abstract class BlueskyClientException extends BlueskyException
{
public static function create(int $status, ?string $error, ?string $message): static
{
$suffix = static::createSuffix($status, $error, $message);

return match ($error) {
'AccountTakedown' => new static("Account is suspended ({$suffix})"),
'ExpiredToken' => new static("Token is expired ({$suffix})"),
'InvalidToken' => new static("Token is invalid ({$suffix})"),
default => new static(static::getDefaultMessage() . " ({$suffix})")
};
}

abstract protected static function getDefaultMessage(): string;

protected static function createSuffix(int $status, ?string $error, ?string $message): string
{
return str($status)
->when($error || $message)
->append(', ' . implode(': ', array_filter([$error, $message])))
->toString();
}
}
21 changes: 0 additions & 21 deletions src/Exceptions/BlueskyException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,4 @@

abstract class BlueskyException extends \Exception
{
public static function create(int $status, ?string $error, ?string $message): static
{
$suffix = static::createSuffix($status, $error, $message);

return match ($error) {
'AccountTakedown' => new static("Account is suspended ({$suffix})"),
'ExpiredToken' => new static("Token is expired ({$suffix})"),
'InvalidToken' => new static("Token is invalid ({$suffix})"),
default => new static(static::getDefaultMessage() . " ({$suffix})")
};
}

abstract protected static function getDefaultMessage(): string;

protected static function createSuffix(int $status, ?string $error, ?string $message): string
{
return str($status)
->when($error || $message)
->append(', ' . implode(': ', array_filter([$error, $message])))
->toString();
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/CouldNotCreatePost.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NotificationChannels\Bluesky\Exceptions;

final class CouldNotCreatePost extends BlueskyException
final class CouldNotCreatePost extends BlueskyClientException
{
protected static function getDefaultMessage(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/CouldNotCreateSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NotificationChannels\Bluesky\Exceptions;

final class CouldNotCreateSession extends BlueskyException
final class CouldNotCreateSession extends BlueskyClientException
{
protected static function getDefaultMessage(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/CouldNotRefreshSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NotificationChannels\Bluesky\Exceptions;

final class CouldNotRefreshSession extends BlueskyException
final class CouldNotRefreshSession extends BlueskyClientException
{
protected static function getDefaultMessage(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/CouldNotResolveHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NotificationChannels\Bluesky\Exceptions;

final class CouldNotResolveHandle extends BlueskyException
final class CouldNotResolveHandle extends BlueskyClientException
{
protected static function getDefaultMessage(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/NoBlueskyChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NotificationChannels\Bluesky\Exceptions;

final class NoBlueskyChannel extends \Exception
final class NoBlueskyChannel extends BlueskyException
{
public static function create(string $class): self
{
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/NoBlueskyIdentityFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NotificationChannels\Bluesky\Exceptions;

final class NoBlueskyIdentityFound extends \Exception
final class NoBlueskyIdentityFound extends BlueskyException
{
public static function create(): self
{
Expand Down
4 changes: 2 additions & 2 deletions src/RichText/Facets/Facet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace NotificationChannels\Bluesky\RichText\Facets;

use NotificationChannels\Bluesky\BlueskyClient;
use NotificationChannels\Bluesky\Exceptions\BlueskyException;
use NotificationChannels\Bluesky\Exceptions\BlueskyClientException;
use NotificationChannels\Bluesky\RichText\Facets\Features\Link;
use NotificationChannels\Bluesky\RichText\Facets\Features\Mention;

Expand Down Expand Up @@ -52,7 +52,7 @@ private static function detectMentions(string $text, BlueskyClient $client): arr

try {
$did = $client->resolveHandle($handle);
} catch (BlueskyException) {
} catch (BlueskyClientException) {
return null;
}

Expand Down

0 comments on commit 4341d22

Please sign in to comment.