Skip to content

Commit

Permalink
Pass Messaging API errors to NotFound exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
pelletiermaxime authored and jeromegamez committed Jan 23, 2023
1 parent 9435178 commit 4af66d7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Firebase/Messaging.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 16 additions & 2 deletions tests/Integration/MessagingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit 4af66d7

Please sign in to comment.