diff --git a/src/Exceptions/CouldNotSendMail.php b/src/Exceptions/CouldNotSendMail.php index 91d26ad..6a0e7b9 100644 --- a/src/Exceptions/CouldNotSendMail.php +++ b/src/Exceptions/CouldNotSendMail.php @@ -12,7 +12,7 @@ public static function invalidConfig(): CouldNotSendMail { return new static('The mail.php configuration is missing from address, transport, client and/or secret key configuration'); } - public static function serviceRespondedWithError(string $code, string $message): CouldNotSendMail { + public static function serviceRespondedWithError(?string $code, ?string $message): CouldNotSendMail { return new static('Microsoft Graph API responded with code ' . $code . ': ' . $message); } diff --git a/src/MsGraphMailServiceProvider.php b/src/MsGraphMailServiceProvider.php index 2c62ef5..3623d6e 100644 --- a/src/MsGraphMailServiceProvider.php +++ b/src/MsGraphMailServiceProvider.php @@ -11,6 +11,7 @@ class MsGraphMailServiceProvider extends ServiceProvider { /** * Boot any application services. * @return void + * @throws CouldNotSendMail */ public function boot() { $this->app->get('mail.manager')->extend('microsoft-graph', function (array $config) { diff --git a/src/MsGraphMailTransport.php b/src/MsGraphMailTransport.php index c4ef1d6..9c78934 100644 --- a/src/MsGraphMailTransport.php +++ b/src/MsGraphMailTransport.php @@ -3,7 +3,6 @@ namespace LaravelMsGraphMail; -use Exception; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\BadResponseException; @@ -17,6 +16,7 @@ use Swift_Mime_Attachment; use Swift_Mime_EmbeddedFile; use Swift_Mime_SimpleMessage; +use Throwable; class MsGraphMailTransport extends Transport { @@ -57,7 +57,6 @@ public function __construct(array $config, ClientInterface $client = null) { * @return int * @throws CouldNotSendMail * @throws CouldNotReachService - * @throws CouldNotGetToken */ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null): int { $this->beforeSendPerformed($message); @@ -76,11 +75,13 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul return $this->numberOfRecipients($message); } catch (BadResponseException $e) { // The API responded with 4XX or 5XX error - $response = json_decode((string)$e->getResponse()->getBody()); - throw CouldNotSendMail::serviceRespondedWithError($response->error->code, $response->error->message); + if ($e->hasResponse()) $response = json_decode((string)$e->getResponse()->getBody()); + throw CouldNotSendMail::serviceRespondedWithError($response->error->code ?? 'Unknown', $response->error->message ?? 'Unknown error'); } catch (ConnectException $e) { // A connection error (DNS, timeout, ...) occurred throw CouldNotReachService::networkError(); + } catch (Throwable $e) { + throw CouldNotReachService::unknownError(); } } @@ -223,7 +224,7 @@ protected function getAccessToken(): string { } catch (ConnectException $e) { // A connection error (DNS, timeout, ...) occurred throw CouldNotReachService::networkError(); - } catch (Exception $e) { + } catch (Throwable $e) { // An unknown error occurred throw CouldNotReachService::unknownError(); }