Skip to content

Commit

Permalink
Improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Ackermann committed Jul 8, 2021
1 parent 81f70ae commit 8cf4789
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Exceptions/CouldNotSendMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions src/MsGraphMailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 6 additions & 5 deletions src/MsGraphMailTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace LaravelMsGraphMail;

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\BadResponseException;
Expand All @@ -17,6 +16,7 @@
use Swift_Mime_Attachment;
use Swift_Mime_EmbeddedFile;
use Swift_Mime_SimpleMessage;
use Throwable;

class MsGraphMailTransport extends Transport {

Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
}

Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 8cf4789

Please sign in to comment.