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(social): type the deliberate publish exceptions so they keep a clean message
Paulo Castellano5b26747dde24
diff --git a/app/Services/Social/AbstractLinkedInPublisher.php b/app/Services/Social/AbstractLinkedInPublisher.phpindex 16c8f7d28..5fd6f5e7b 100644--- a/app/Services/Social/AbstractLinkedInPublisher.php+++ b/app/Services/Social/AbstractLinkedInPublisher.php@@ -6,6 +6,7 @@ namespace App\Services\Social;use App\Enums\Media\Type as MediaType;use App\Enums\SocialAccount\Platform;+use App\Exceptions\Social\ErrorCategory;use App\Exceptions\Social\LinkedInPublishException;use App\Exceptions\TokenExpiredException;use App\Models\PostPlatform;@@ -160,13 +161,19 @@ abstract class AbstractLinkedInPublisher$document = $media->first(fn ($item) => $item->isDocument());if (! $document) {- throw new \Exception("No PDF document for {$this->label()} document post");+ throw new LinkedInPublishException(+ userMessage: "No PDF document was found for this {$this->label()} document post.",+ category: ErrorCategory::MediaFormat,+ );}$documentUrn = $this->uploadDocument($document);if (! $documentUrn) {- throw new \Exception("{$this->label()} document upload failed");+ throw new LinkedInPublishException(+ userMessage: "{$this->label()} did not accept the document. Please try again.",+ category: ErrorCategory::ServerError,+ );}$payload = $this->basePayload($content);@@ -262,7 +269,10 @@ abstract class AbstractLinkedInPublisher$imageUrn = data_get($initData, 'value.image');if (! $uploadUrl || ! $imageUrn) {- throw new \Exception("{$this->label()} image upload init missing uploadUrl or image URN");+ throw new LinkedInPublishException(+ userMessage: "{$this->label()} did not accept the image upload. Please try again.",+ category: ErrorCategory::ServerError,+ );}$tempFile = tempnam(sys_get_temp_dir(), 'li_image_');@@ -337,7 +347,10 @@ abstract class AbstractLinkedInPublisher$uploadToken = data_get($initData, 'value.uploadToken', '');if (! $videoUrn || empty($uploadInstructions)) {- throw new \Exception("{$this->label()} video upload init missing video URN or upload instructions");+ throw new LinkedInPublishException(+ userMessage: "{$this->label()} did not accept the video upload. Please try again.",+ category: ErrorCategory::ServerError,+ );}$uploadedPartIds = [];@@ -414,7 +427,10 @@ abstract class AbstractLinkedInPublisher$documentUrn = data_get($initData, 'value.document');if (! $uploadUrl || ! $documentUrn) {- throw new \Exception("{$this->label()} document upload init missing uploadUrl or document URN");+ throw new LinkedInPublishException(+ userMessage: "{$this->label()} did not accept the document upload. Please try again.",+ category: ErrorCategory::ServerError,+ );}// The Documents API is not chunked — upload the PDF in a single request.@@ -475,13 +491,19 @@ abstract class AbstractLinkedInPublisher}if ($status === 'PROCESSING_FAILED') {- throw new \Exception("{$this->label()} {$label} processing failed");+ throw new LinkedInPublishException(+ userMessage: "{$this->label()} {$label} processing failed.",+ category: ErrorCategory::ServerError,+ );}sleep($this->processingPollSeconds());}- throw new \Exception("{$this->label()} {$label} processing did not complete in time");+ throw new LinkedInPublishException(+ userMessage: "{$this->label()} {$label} processing did not complete in time.",+ category: ErrorCategory::ServerError,+ );}/**@@ -505,7 +527,10 @@ abstract class AbstractLinkedInPublisher$response = Http::withOptions(['sink' => $tempFile])->timeout(600)->get($url);if ($response->failed()) {- throw new \Exception('Failed to download media: HTTP '.$response->status());+ throw new LinkedInPublishException(+ userMessage: "Could not fetch the media to upload to {$this->label()}. Please try again.",+ category: ErrorCategory::ServerError,+ );}}diff --git a/app/Services/Social/FacebookPublisher.php b/app/Services/Social/FacebookPublisher.phpindex 9816fc9d5..0a64cec46 100644--- a/app/Services/Social/FacebookPublisher.php+++ b/app/Services/Social/FacebookPublisher.php@@ -55,7 +55,10 @@ class FacebookPublisherContentType::FacebookReel => $this->publishReel($pageId, $accessToken, $content, $media->first()),ContentType::FacebookStory => $this->publishStory($pageId, $accessToken, $media->first()),ContentType::FacebookPost => $this->publishPost($pageId, $accessToken, $content, $media, $aspectRatio),- default => throw new \Exception("Unsupported Facebook content type: {$contentType?->value}"),+ default => throw new FacebookPublishException(+ userMessage: "Unsupported Facebook content type: {$contentType?->value}",+ category: ErrorCategory::MediaFormat,+ ),};}@@ -64,7 +67,10 @@ class FacebookPublisher// Text only postif ($media->isEmpty()) {if ($content === null || $content === '') {- throw new \Exception('Facebook text posts require content. Please add text to your post.');+ throw new FacebookPublishException(+ userMessage: 'Facebook text posts require content. Please add text to your post.',+ category: ErrorCategory::MediaFormat,+ );}return $this->publishTextPost($pageId, $accessToken, $content);@@ -87,7 +93,10 @@ class FacebookPublisherreturn $this->publishMultiImagePost($pageId, $accessToken, $content, $media, $aspectRatio);}- throw new \Exception('Unsupported media type for Facebook');+ throw new FacebookPublishException(+ userMessage: 'Unsupported media type for Facebook',+ category: ErrorCategory::MediaFormat,+ );}private function publishTextPost(string $pageId, string $accessToken, string $content): array@@ -173,7 +182,10 @@ class FacebookPublisher}if (empty($attachedMedia)) {- throw new \Exception('Failed to upload any images to Facebook');+ throw new FacebookPublishException(+ userMessage: 'Failed to upload any images to Facebook',+ category: ErrorCategory::ServerError,+ );}// Create the post with attached media@@ -360,7 +372,10 @@ class FacebookPublisher$videoId = $response->json()['video_id'] ?? null;if (! $videoId) {- throw new \Exception('Facebook story upload failed: no video ID returned');+ throw new FacebookPublishException(+ userMessage: 'Facebook did not accept the story video. Please try again.',+ category: ErrorCategory::ServerError,+ );}$transferResponse = $this->facebookHttp()->post("{$this->baseUrl}/{$videoId}", [diff --git a/app/Services/Social/LinkedInPagePublisher.php b/app/Services/Social/LinkedInPagePublisher.phpindex 1c3b50fce..bfef3c53a 100644--- a/app/Services/Social/LinkedInPagePublisher.php+++ b/app/Services/Social/LinkedInPagePublisher.php@@ -5,6 +5,8 @@ declare(strict_types=1);namespace App\Services\Social;use App\Enums\SocialAccount\Platform;+use App\Exceptions\Social\ErrorCategory;+use App\Exceptions\Social\LinkedInPublishException;/*** Publishes posts to a LinkedIn company page on behalf of an administering member.@@ -21,7 +23,10 @@ class LinkedInPagePublisher extends AbstractLinkedInPublisher$organizationId = $this->account->meta['organization_id'] ?? null;if (! $organizationId) {- throw new \Exception('LinkedIn Page organization ID not configured');+ throw new LinkedInPublishException(+ userMessage: 'LinkedIn Page organization ID not configured',+ category: ErrorCategory::Permission,+ );}return "urn:li:organization:{$organizationId}";diff --git a/app/Services/Social/ThreadsPublisher.php b/app/Services/Social/ThreadsPublisher.phpindex c37402f18..fe256fa34 100644--- a/app/Services/Social/ThreadsPublisher.php+++ b/app/Services/Social/ThreadsPublisher.php@@ -4,6 +4,7 @@ declare(strict_types=1);namespace App\Services\Social;+use App\Exceptions\Social\ErrorCategory;use App\Exceptions\Social\ThreadsPublishException;use App\Models\PostPlatform;use App\Services\Social\Concerns\HasSocialHttpClient;@@ -41,7 +42,10 @@ class ThreadsPublisher// Text only postif ($media->isEmpty()) {if (empty($content)) {- throw new \Exception('Threads text posts require content. Please add text to your post.');+ throw new ThreadsPublishException(+ userMessage: 'Threads text posts require content. Please add text to your post.',+ category: ErrorCategory::MediaFormat,+ );}return $this->publishTextPost($userId, $accessToken, $content);@@ -83,7 +87,10 @@ class ThreadsPublisher$containerId = $containerResponse->json()['id'] ?? null;if (! $containerId) {- throw new \Exception('Threads text container creation failed: no container ID returned');+ throw new ThreadsPublishException(+ userMessage: 'Threads did not accept the post. Please try again.',+ category: ErrorCategory::ServerError,+ );}// Step 2: Publish@@ -111,7 +118,10 @@ class ThreadsPublisher$containerId = $containerResponse->json()['id'] ?? null;if (! $containerId) {- throw new \Exception('Threads image container creation failed: no container ID returned');+ throw new ThreadsPublishException(+ userMessage: 'Threads did not accept the image. Please try again.',+ category: ErrorCategory::ServerError,+ );}// Step 2: Wait for image processing@@ -142,7 +152,10 @@ class ThreadsPublisher$containerId = $containerResponse->json()['id'] ?? null;if (! $containerId) {- throw new \Exception('Threads video container creation failed: no container ID returned');+ throw new ThreadsPublishException(+ userMessage: 'Threads did not accept the video. Please try again.',+ category: ErrorCategory::ServerError,+ );}// Wait for video processing@@ -198,7 +211,10 @@ class ThreadsPublisher}if (empty($childContainers)) {- throw new \Exception('Failed to create any carousel items');+ throw new ThreadsPublishException(+ userMessage: 'Failed to create any carousel items',+ category: ErrorCategory::ServerError,+ );}// Step 2: Create carousel container@@ -219,7 +235,10 @@ class ThreadsPublisher$carouselId = $carouselResponse->json()['id'] ?? null;if (! $carouselId) {- throw new \Exception('Threads carousel container creation failed: no container ID returned');+ throw new ThreadsPublishException(+ userMessage: 'Threads did not accept the carousel. Please try again.',+ category: ErrorCategory::ServerError,+ );}// Step 3: Publish carousel@@ -244,7 +263,10 @@ class ThreadsPublisher$mediaId = $publishResponse->json()['id'] ?? null;if (! $mediaId) {- throw new \Exception('Threads publish failed: no media ID returned');+ throw new ThreadsPublishException(+ userMessage: 'Threads did not accept the post. Please publish again.',+ category: ErrorCategory::ServerError,+ );}// Get permalink@@ -289,14 +311,20 @@ class ThreadsPublisherif ($status === 'ERROR') {$errorMessage = data_get($data, 'error_message', 'Unknown error');- throw new \Exception('Threads media processing failed: '.$errorMessage);+ throw new ThreadsPublishException(+ userMessage: 'Threads media processing failed. Please try a different file.',+ category: ErrorCategory::ServerError,+ );}sleep(3);}Log::warning('Threads media processing timeout', ['container_id' => $containerId]);- throw new \Exception('Threads media processing timeout after '.$maxAttempts.' attempts');+ throw new ThreadsPublishException(+ userMessage: 'Threads took too long to process the media. Please try again.',+ category: ErrorCategory::ServerError,+ );}private function handleApiError(Response $response): neverdiff --git a/app/Services/Social/XPublisher.php b/app/Services/Social/XPublisher.phpindex 4c8102f48..be844daaa 100644--- a/app/Services/Social/XPublisher.php+++ b/app/Services/Social/XPublisher.php@@ -72,7 +72,10 @@ class XPublisher}if (empty($content) && empty($mediaIds)) {- throw new \Exception('X posts require either text or media. Please add content to your post.');+ throw new XPublishException(+ userMessage: 'X posts require either text or media. Please add content to your post.',+ category: ErrorCategory::MediaFormat,+ );}$response = $this->getHttpClient()@@ -115,7 +118,10 @@ class XPublisher$downloadResponse = Http::withOptions(['sink' => $tempFile])->timeout(600)->get($mediaItem->url);if ($downloadResponse->failed()) {- throw new \Exception('Failed to download media: HTTP '.$downloadResponse->status());+ throw new XPublishException(+ userMessage: 'Could not fetch the media to upload to X. Please try again.',+ category: ErrorCategory::ServerError,+ );}// Recover the MIME from the downloaded bytes when the item carries@@ -215,7 +221,10 @@ class XPublisher$mediaId = $initData['data']['id'] ?? $initData['media_id'] ?? null;if (! $mediaId) {- throw new \Exception('No media_id returned from INIT');+ throw new XPublishException(+ userMessage: 'X did not accept the media upload. Please try again.',+ category: ErrorCategory::ServerError,+ );}// APPEND - Read from temp file in 1MB chunks. Matches thediff --git a/tests/Feature/Services/Social/LinkedInPublisherTest.php b/tests/Feature/Services/Social/LinkedInPublisherTest.phpindex 94b837cc5..23025e4d1 100644--- a/tests/Feature/Services/Social/LinkedInPublisherTest.php+++ b/tests/Feature/Services/Social/LinkedInPublisherTest.php@@ -4,6 +4,7 @@ declare(strict_types=1);use App\Enums\PostPlatform\ContentType;use App\Enums\SocialAccount\Platform;+use App\Exceptions\Social\LinkedInPublishException;use App\Exceptions\TokenExpiredException;use App\Models\Post;use App\Models\PostPlatform;@@ -789,7 +790,7 @@ test('linkedin publisher throws when document init response is missing the urn',});expect(fn () => $this->publisher->publish($this->postPlatform))- ->toThrow(Exception::class, 'missing uploadUrl or document URN');+ ->toThrow(LinkedInPublishException::class, 'did not accept the document upload');Http::assertNotSent(fn ($request) => str_contains($request->url(), '/rest/posts'));});