Skip to content
Snippets Groups Projects
Commit d3fb2348 authored by Patrick Kenny's avatar Patrick Kenny
Browse files

Issue #3450177 by ptmkenny: Throw custom exception when token validation fails

parent 2e6bfb59
Branches
Tags
1 merge request!32throw special exception when push token validation fails
Pipeline #183509 passed
<?php
declare(strict_types=1);
namespace Drupal\firebase_php\Exception;
/**
* Exception if the token is invalid according to Google.
*
* If you get this exception, the token can be deleted.
*/
class FirebasePhpInvalidTokenException extends FirebasePhpException {
}
......@@ -106,7 +106,16 @@ final class FirebasePhpSendTestMessageForm extends FormBase {
// @phpstan-ignore-next-line Exception is shown to admin, so no rethrowing.
catch (\Exception $e) {
Error::logException($this->logger('firebase_php'), $e);
$details = method_exists($e, 'errors') ? $e->errors() : 'No details available.';
// Try to get more information for the developer.
$details = 'No details available.';
if (method_exists($e, 'errors')) {
$details = $e->errors();
}
elseif ($e->getPrevious() instanceof \Throwable) {
$details = $e->getPrevious()->getMessage();
}
$this->messenger()->addError($this->t('Failed to send the test message. Error: %error More info: %errorDetails', [
'%error' => $e->getMessage(),
'%errorDetails' => json_encode($details),
......
......@@ -5,9 +5,12 @@ declare(strict_types=1);
namespace Drupal\firebase_php\Service;
use Drupal\firebase_php\Enum\SendingDestinationType;
use Drupal\firebase_php\Exception\FirebasePhpException;
use Drupal\firebase_php\Exception\FirebasePhpInvalidArgumentException;
use Drupal\firebase_php\Exception\FirebasePhpInvalidTokenException;
use Drupal\firebase_php\FirebasePhpMessagingApiInterface;
use Kreait\Firebase\Contract\Messaging;
use Kreait\Firebase\Exception\Messaging\InvalidMessage;
use Kreait\Firebase\Messaging\ApnsConfig;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\MulticastSendReport;
......@@ -42,9 +45,9 @@ class FirebasePhpMessagingApi extends FirebasePhpMessagingService implements Fir
): array {
$notification = $this->createNotification($notification_title, $notification_body, $notification_url);
$message_array = $this->createMessageSingleDevice($device_token, $notification, $data);
$message = $this->createCloudMessage($message_array, $badge_count, $apns_data);
$cloud_message = $this->createCloudMessage($message_array, $badge_count, $apns_data);
return $this->messaging->send($message);
return $this->sendCloudMessage($cloud_message);
}
/**
......@@ -61,9 +64,9 @@ class FirebasePhpMessagingApi extends FirebasePhpMessagingService implements Fir
): array {
$notification = $this->createNotification($notification_title, $notification_body, $notification_url);
$message_array = $this->createMessageTopic($topic, $notification, $data);
$message = $this->createCloudMessage($message_array, $badge_count, $apns_data);
$cloud_message = $this->createCloudMessage($message_array, $badge_count, $apns_data);
return $this->messaging->send($message);
return $this->sendCloudMessage($cloud_message);
}
/**
......@@ -189,4 +192,25 @@ class FirebasePhpMessagingApi extends FirebasePhpMessagingService implements Fir
return $message;
}
/**
* Sends a cloud message.
*
* @param \Kreait\Firebase\Messaging\CloudMessage $cloud_message
* The cloud message to send.
*
* @return array
* The result from the kreait/firebase-php library.
*/
private function sendCloudMessage(CloudMessage $cloud_message): array {
try {
return $this->messaging->send($cloud_message);
}
catch (InvalidMessage $e) {
if ($e->errors()['error']['code'] === 400) {
throw new FirebasePhpInvalidTokenException('Invalid registration token. Deletion recommended.', 400, $e);
}
throw new FirebasePhpException('Failed to send cloud message!', 404, $e);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment