From da2b7ca9caa5f9584d902eba926d25b5d6d5809b Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Wed, 1 Apr 2026 14:33:26 -0300 Subject: [PATCH] =?UTF-8?q?fix:=20Facebook/Instagram/Threads=20OAuthExcept?= =?UTF-8?q?ion=20type=20incorrectly=20treated=20as=20token=20error=20?= =?UTF-8?q?=E2=80=94=20now=20only=20code=20190=20triggers=20TokenExpiredEx?= =?UTF-8?q?ception?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Exceptions/Social/FacebookPublishException.php | 2 +- app/Exceptions/Social/InstagramPublishException.php | 2 +- app/Exceptions/Social/ThreadsPublishException.php | 2 +- .../Social/FacebookPublishExceptionTest.php | 13 ++++++++----- .../Social/InstagramPublishExceptionTest.php | 5 ++--- .../Social/ThreadsPublishExceptionTest.php | 2 +- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/app/Exceptions/Social/FacebookPublishException.php b/app/Exceptions/Social/FacebookPublishException.php index 369609e0..e362bfd6 100644 --- a/app/Exceptions/Social/FacebookPublishException.php +++ b/app/Exceptions/Social/FacebookPublishException.php @@ -22,7 +22,7 @@ public static function fromApiResponse(mixed $response): static $tokenSubcodes = [458, 459, 460, 463, 464, 467]; - if ($errorType === 'OAuthException' || $errorCode === 190 || in_array($errorSubcode, $tokenSubcodes, true)) { + if ($errorCode === 190 || in_array($errorSubcode, $tokenSubcodes, true)) { throw new TokenExpiredException( message: $errorMessage, platformErrorCode: $errorCode !== null ? (string) $errorCode : null, diff --git a/app/Exceptions/Social/InstagramPublishException.php b/app/Exceptions/Social/InstagramPublishException.php index c0bbf662..4b836d46 100644 --- a/app/Exceptions/Social/InstagramPublishException.php +++ b/app/Exceptions/Social/InstagramPublishException.php @@ -21,7 +21,7 @@ public static function fromApiResponse(mixed $response): static $errorUserMsg = data_get($body, 'error.error_user_msg'); $errorMessage = data_get($body, 'error.message', 'An unknown Instagram error occurred.'); - if ($errorType === 'OAuthException' || $errorCode === 190) { + if ($errorCode === 190) { throw new TokenExpiredException( message: $errorMessage, platformErrorCode: $errorCode !== null ? (string) $errorCode : null, diff --git a/app/Exceptions/Social/ThreadsPublishException.php b/app/Exceptions/Social/ThreadsPublishException.php index 1a2026f2..7ab5f07a 100644 --- a/app/Exceptions/Social/ThreadsPublishException.php +++ b/app/Exceptions/Social/ThreadsPublishException.php @@ -20,7 +20,7 @@ public static function fromApiResponse(mixed $response): static $errorCode = data_get($body, 'error.code'); $errorMessage = data_get($body, 'error.message', 'An unknown Threads error occurred.'); - if ($errorType === 'OAuthException' || $errorCode === 190) { + if ($errorCode === 190) { throw new TokenExpiredException( message: $errorMessage, platformErrorCode: $errorCode !== null ? (string) $errorCode : null, diff --git a/tests/Unit/Exceptions/Social/FacebookPublishExceptionTest.php b/tests/Unit/Exceptions/Social/FacebookPublishExceptionTest.php index 8db2b141..f7394c8f 100644 --- a/tests/Unit/Exceptions/Social/FacebookPublishExceptionTest.php +++ b/tests/Unit/Exceptions/Social/FacebookPublishExceptionTest.php @@ -40,19 +40,22 @@ FacebookPublishException::fromApiResponse($fakeResponse); })->throws(TokenExpiredException::class); -test('OAuthException type throws TokenExpiredException', function () { +test('OAuthException with non-190 code does not throw TokenExpiredException', function () { $response = Http::response([ 'error' => [ - 'message' => 'Invalid OAuth access token.', + 'message' => 'There was a problem uploading your video file.', 'type' => 'OAuthException', - 'code' => 400, + 'code' => 6000, ], ], 400); $fakeResponse = Http::fake(['*' => $response])->post('https://graph.facebook.com/test'); - FacebookPublishException::fromApiResponse($fakeResponse); -})->throws(TokenExpiredException::class); + $exception = FacebookPublishException::fromApiResponse($fakeResponse); + + expect($exception)->toBeInstanceOf(FacebookPublishException::class); + expect($exception->category)->toBe(ErrorCategory::MediaFormat); +}); test('code 4 maps to RateLimit category', function () { $response = Http::response([ diff --git a/tests/Unit/Exceptions/Social/InstagramPublishExceptionTest.php b/tests/Unit/Exceptions/Social/InstagramPublishExceptionTest.php index 771bfae0..eb376431 100644 --- a/tests/Unit/Exceptions/Social/InstagramPublishExceptionTest.php +++ b/tests/Unit/Exceptions/Social/InstagramPublishExceptionTest.php @@ -84,13 +84,12 @@ ->and($exception->userMessage)->toBe('Caption is too long (max 2,200 characters, 30 hashtags, 20 @mentions).'); }); -test('OAuthException type throws TokenExpiredException', function () { +test('code 190 throws TokenExpiredException', function () { $response = Http::response([ 'error' => [ 'message' => 'Invalid OAuth access token', 'type' => 'OAuthException', - 'code' => 400, - 'error_subcode' => 463, + 'code' => 190, ], ], 400); diff --git a/tests/Unit/Exceptions/Social/ThreadsPublishExceptionTest.php b/tests/Unit/Exceptions/Social/ThreadsPublishExceptionTest.php index 80d6ab3a..36995407 100644 --- a/tests/Unit/Exceptions/Social/ThreadsPublishExceptionTest.php +++ b/tests/Unit/Exceptions/Social/ThreadsPublishExceptionTest.php @@ -7,7 +7,7 @@ use App\Exceptions\TokenExpiredException; use Illuminate\Support\Facades\Http; -test('OAuthException type throws TokenExpiredException', function () { +test('code 190 throws TokenExpiredException', function () { $response = Http::response([ 'error' => [ 'message' => 'Invalid OAuth access token.',