Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
refactor: TikTok, YouTube, Facebook publishers use platform exceptions
Paulo Castellano2320c9ae6323
diff --git a/app/Services/Social/FacebookPublisher.php b/app/Services/Social/FacebookPublisher.phpindex c356c7e4e..ce0cfa831 100644--- a/app/Services/Social/FacebookPublisher.php+++ b/app/Services/Social/FacebookPublisher.php@@ -5,7 +5,7 @@ declare(strict_types=1);namespace App\Services\Social;use App\Enums\PostPlatform\ContentType;-use App\Exceptions\TokenExpiredException;+use App\Exceptions\Social\FacebookPublishException;use App\Models\PostPlatform;use Illuminate\Http\Client\Response;use Illuminate\Support\Facades\Http;@@ -13,24 +13,6 @@ use Illuminate\Support\Facades\Log;class FacebookPublisher{- /**- * Meta Graph API error codes that indicate token issues.- *- * @see https://developers.facebook.com/docs/graph-api/guides/error-handling- */- private const TOKEN_ERROR_CODES = [- 190, // Invalid OAuth access token- ];-- private const TOKEN_ERROR_SUBCODES = [- 458, // App not installed- 459, // User checkpointed- 460, // Password changed- 463, // Session expired- 464, // Unconfirmed user- 467, // Invalid access token- ];-private string $baseUrl = 'https://graph.facebook.com/v24.0';public function publish(PostPlatform $postPlatform): array@@ -371,24 +353,6 @@ class FacebookPublisherprivate function handleApiError(Response $response, string $context): void{- $body = $response->json() ?? [];- $error = $body['error'] ?? [];- $errorCode = $error['code'] ?? null;- $errorSubcode = $error['error_subcode'] ?? null;- $errorType = $error['type'] ?? null;- $message = $error['message'] ?? $response->body();-- $isTokenError = $errorType === 'OAuthException'- || in_array($errorCode, self::TOKEN_ERROR_CODES)- || in_array($errorSubcode, self::TOKEN_ERROR_SUBCODES);-- if ($isTokenError) {- throw new TokenExpiredException(- "{$context}: {$message}",- $errorCode ? (string) $errorCode : null- );- }-- throw new \Exception("{$context}: {$message}");+ throw FacebookPublishException::fromApiResponse($response);}}diff --git a/app/Services/Social/TikTokPublisher.php b/app/Services/Social/TikTokPublisher.phpindex 79d9099f8..2a8835d54 100644--- a/app/Services/Social/TikTokPublisher.php+++ b/app/Services/Social/TikTokPublisher.php@@ -4,6 +4,7 @@ declare(strict_types=1);namespace App\Services\Social;+use App\Exceptions\Social\TikTokPublishException;use App\Exceptions\TokenExpiredException;use App\Models\PostPlatform;use App\Models\SocialAccount;@@ -14,23 +15,6 @@ use Illuminate\Support\Facades\Log;class TikTokPublisher{- /**- * TikTok API error codes that indicate token issues.- *- * @see https://developers.tiktok.com/doc/tiktok-api-v2-error-handling- */- private const TOKEN_ERROR_CODES = [- 'access_token_invalid',- 'access_token_expired',- 'token_expired',- ];-- private const TOKEN_ERROR_NUMERIC_CODES = [- 10001, // Invalid Access Token- 10002, // Access Token Expired- 10003, // Invalid Client Key- ];-private string $baseUrl = 'https://open.tiktokapis.com/v2';private string $accessToken;@@ -255,8 +239,8 @@ class TikTokPublisher}if (in_array($status, ['FAILED', 'PUBLISH_FAILED'])) {- $errorCode = data_get($data, 'data')['fail_reason'] ?? 'Unknown error';- throw new \Exception("TikTok publish failed: {$errorCode}");+ $failReason = data_get($data, 'data.fail_reason', 'Unknown error');+ throw TikTokPublishException::fromFailReason($failReason, json_encode($data));}// PROCESSING_UPLOAD, PROCESSING_DOWNLOAD, SENDING_TO_USER_INBOX - continue waiting@@ -309,23 +293,6 @@ class TikTokPublisherprivate function handleApiError(Response $response, string $context): void{- $body = $response->json() ?? [];- $error = $body['error'] ?? [];- $errorCode = $error['code'] ?? $body['error']['code'] ?? null;- $errorMessage = $error['message'] ?? $body['error']['message'] ?? $response->body();-- // TikTok can return error codes as strings or numeric codes- $isTokenError = in_array($errorCode, self::TOKEN_ERROR_CODES)- || in_array((int) $errorCode, self::TOKEN_ERROR_NUMERIC_CODES)- || $response->status() === 401;-- if ($isTokenError) {- throw new TokenExpiredException(- "{$context}: {$errorMessage}",- is_string($errorCode) ? $errorCode : (string) $errorCode- );- }-- throw new \Exception("{$context}: {$errorMessage}");+ throw TikTokPublishException::fromApiResponse($response);}}diff --git a/app/Services/Social/YouTubePublisher.php b/app/Services/Social/YouTubePublisher.phpindex 8f7f132f8..b182dae0b 100644--- a/app/Services/Social/YouTubePublisher.php+++ b/app/Services/Social/YouTubePublisher.php@@ -4,6 +4,7 @@ declare(strict_types=1);namespace App\Services\Social;+use App\Exceptions\Social\YouTubePublishException;use App\Exceptions\TokenExpiredException;use App\Models\PostPlatform;use App\Models\SocialAccount;@@ -231,22 +232,6 @@ class YouTubePublisherprivate function handleGoogleError(\Google\Service\Exception $e): never{- $message = $e->getMessage();- $errors = $e->getErrors();- $reason = data_get($errors, '0.reason');-- Log::error('YouTube API error', [- 'message' => $message,- 'reason' => $reason,- 'errors' => $errors,- ]);-- $tokenReasons = ['authError', 'forbidden', 'unauthorized', 'invalid_grant'];-- if ($e->getCode() === 401 || in_array($reason, $tokenReasons)) {- throw new TokenExpiredException("YouTube API error: {$message}", $reason);- }-- throw new \Exception("YouTube API error: {$message}");+ throw YouTubePublishException::fromGoogleException($e);}}diff --git a/tests/Unit/Services/Social/TikTokPublisherTest.php b/tests/Unit/Services/Social/TikTokPublisherTest.phpindex 4a81fca07..311cee7eb 100644--- a/tests/Unit/Services/Social/TikTokPublisherTest.php+++ b/tests/Unit/Services/Social/TikTokPublisherTest.php@@ -4,6 +4,7 @@ declare(strict_types=1);use App\Enums\PostPlatform\ContentType;use App\Enums\SocialAccount\Platform;+use App\Exceptions\Social\TikTokPublishException;use App\Exceptions\TokenExpiredException;use App\Models\Post;use App\Models\PostPlatform;@@ -151,8 +152,8 @@ test('tiktok publisher throws token expired exception on auth error', function (Http::fake(['https://open.tiktokapis.com/v2/post/publish/video/init/' => Http::response(['error' => [- 'code' => 'access_token_expired',- 'message' => 'Access token has expired',+ 'code' => 'access_token_invalid',+ 'message' => 'Access token is invalid',],], 401),]);@@ -309,5 +310,5 @@ test('tiktok publisher throws exception when publish fails', function () {]);expect(fn () => $this->publisher->publish($this->postPlatform))- ->toThrow(Exception::class, 'TikTok publish failed: video_rejected');+ ->toThrow(TikTokPublishException::class);});