From 4af66d7cb132d8e16493d4404b2760c34017d5a4 Mon Sep 17 00:00:00 2001 From: Maxime Pelletier Date: Mon, 23 Jan 2023 12:27:56 -0500 Subject: [PATCH] Pass Messaging API errors to `NotFound` exceptions --- CHANGELOG.md | 4 ++++ src/Firebase/Messaging.php | 4 ++-- tests/Integration/MessagingTest.php | 18 ++++++++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41b045199..3cac9d2f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # CHANGELOG ## [Unreleased] +### Fixed +* When trying to work with unknown FCM tokens, errors returned from the Messaging REST API were not passed to the + `NotFound` exception, which prevented the inspection of further details. + (backported from [#760](https://github.com/kreait/firebase-php/pull/760)) ## [5.26.3] ### Fixed diff --git a/src/Firebase/Messaging.php b/src/Firebase/Messaging.php index 6c077aaf6..081b8b0b6 100644 --- a/src/Firebase/Messaging.php +++ b/src/Firebase/Messaging.php @@ -55,7 +55,7 @@ public function send($message, bool $validateOnly = false): array } catch (NotFound $e) { $token = $message->jsonSerialize()['token'] ?? null; if ($token) { - throw NotFound::becauseTokenNotFound($token); + throw NotFound::becauseTokenNotFound($token, $e->errors()); } throw $e; @@ -185,7 +185,7 @@ public function getAppInstance($registrationToken): AppInstance try { return $this->appInstanceApi->getAppInstanceAsync($token)->wait(); } catch (NotFound $e) { - throw NotFound::becauseTokenNotFound($token->value()); + throw NotFound::becauseTokenNotFound($token->value(), $e->errors()); } catch (MessagingException $e) { // The token is invalid throw new InvalidArgument("The registration token '{$token}' is invalid or not available", $e->getCode(), $e); diff --git a/tests/Integration/MessagingTest.php b/tests/Integration/MessagingTest.php index 315b78be6..764b43f3b 100644 --- a/tests/Integration/MessagingTest.php +++ b/tests/Integration/MessagingTest.php @@ -357,12 +357,26 @@ public function testGetAppInstanceWithInvalidToken(): void public function testSendMessageToUnknownToken(): void { $this->expectException(NotFound::class); - $this->messaging->send(['token' => self::$unknownToken]); + + try { + $this->messaging->send(['token' => self::$unknownToken]); + } catch (NotFound $e) { + $this->assertNotEmpty($e->errors()); + + throw $e; + } } public function testGetAppInstanceForUnknownToken(): void { $this->expectException(NotFound::class); - $this->messaging->getAppInstance(self::$unknownToken); + + try { + $this->messaging->getAppInstance(self::$unknownToken); + } catch (NotFound $e) { + $this->assertNotEmpty($e->errors()); + + throw $e; + } } }