Skip to content

Commit 2e8f9ad

Browse files
committed
refactor: started providing previous exceptions where possible
1 parent 9bc5968 commit 2e8f9ad

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/Exceptions/CouldNotSendNotification.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,26 @@
66

77
use Psr\Http\Message\ResponseInterface;
88
use RuntimeException;
9+
use Throwable;
910

1011
class CouldNotSendNotification extends RuntimeException
1112
{
12-
public static function pushbulletRespondedWithAnError(ResponseInterface $response): self
13+
public static function pushbulletRespondedWithAnError(ResponseInterface $response, Throwable $previous = null): self
1314
{
1415
$code = $response->getStatusCode();
1516

1617
$message = $response->getBody();
1718

18-
return new self("Pushbullet responded with error: `{$code} - {$message}`.");
19+
return new self("Pushbullet responded with error: `{$code} - {$message}`.", 0, $previous);
1920
}
2021

2122
public static function providedEmailIsInvalid(string $email): self
2223
{
2324
return new self("Provided email `{$email}` of `notifiable` is not valid.");
2425
}
2526

26-
public static function couldNotCommunicateWithPushbullet(): self
27+
public static function couldNotCommunicateWithPushbullet(Throwable $previous = null): self
2728
{
28-
return new self('Could not connect to Pushbullet API.');
29+
return new self('Could not connect to Pushbullet API.', 0, $previous);
2930
}
3031
}

src/Pushbullet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public function send($params): ResponseInterface
6868
'headers' => $this->getHeaders(),
6969
]);
7070
} catch (ClientException $exception) {
71-
throw CouldNotSendNotification::pushbulletRespondedWithAnError($exception->getResponse());
71+
throw CouldNotSendNotification::pushbulletRespondedWithAnError($exception->getResponse(), $exception);
7272
} catch (Exception $exception) {
73-
throw CouldNotSendNotification::couldNotCommunicateWithPushbullet();
73+
throw CouldNotSendNotification::couldNotCommunicateWithPushbullet($exception);
7474
}
7575
}
7676
}

tests/Exceptions/CouldNotSendNotificationTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace NotificationChannels\Pushbullet\Test\Exceptions;
66

7+
use Exception;
78
use NotificationChannels\Pushbullet\Exceptions\CouldNotSendNotification;
89
use PHPUnit\Framework\TestCase;
910
use Psr\Http\Message\ResponseInterface;
@@ -24,12 +25,14 @@ public function invalid_email_exception_can_be_created(): void
2425
/** @test */
2526
public function pushbullet_connection_exception_can_be_created(): void
2627
{
27-
$exception = CouldNotSendNotification::couldNotCommunicateWithPushbullet();
28+
$previousException = new Exception();
29+
$exception = CouldNotSendNotification::couldNotCommunicateWithPushbullet($previousException);
2830

2931
$this->assertEquals(
3032
'Could not connect to Pushbullet API.',
3133
$exception->getMessage()
3234
);
35+
$this->assertSame($previousException, $exception->getPrevious());
3336
}
3437

3538
/** @test */
@@ -45,11 +48,14 @@ public function pushbullet_error_exception_can_be_created(): void
4548
->method('getBody')
4649
->willReturn('Oops');
4750

48-
$exception = CouldNotSendNotification::pushbulletRespondedWithAnError($response);
51+
$previousException = new Exception();
52+
53+
$exception = CouldNotSendNotification::pushbulletRespondedWithAnError($response, $previousException);
4954

5055
$this->assertEquals(
5156
'Pushbullet responded with error: `400 - Oops`.',
5257
$exception->getMessage()
5358
);
59+
$this->assertSame($previousException, $exception->getPrevious());
5460
}
5561
}

0 commit comments

Comments
 (0)