Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💥 Improve exceptions #5

Merged
merged 2 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Exceptions/PushwooshException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace NotificationChannels\Pushwoosh\Exceptions;

use RuntimeException;

/**
* Exception thrown following communication failure with the Pushwoosh API.
*/
class PushwooshException extends RuntimeException
{
//
}
40 changes: 40 additions & 0 deletions src/Exceptions/UnknownDeviceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace NotificationChannels\Pushwoosh\Exceptions;

use Illuminate\Support\Arr;
use Throwable;

class UnknownDeviceException extends PushwooshException
{
protected $devices;

/**
* Create a new unknown device exception.
*
* @param mixed $devices
* @param int $code
* @param \Throwable|null $previous
* @return void
*/
public function __construct($devices, $code = 0, Throwable $previous = null)
{
$this->devices = (array)$devices;

parent::__construct(
sprintf('Unknown device(s) referenced: %s', implode(', ', Arr::flatten($this->devices))),
$code,
$previous
);
}

/**
* Get the unknown devices per message.
*
* @return string[][]
*/
public function getDevices()
{
return $this->devices;
}
}
8 changes: 5 additions & 3 deletions src/Pushwoosh.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use NotificationChannels\Pushwoosh\Exceptions\PushwooshException;
use NotificationChannels\Pushwoosh\Exceptions\UnknownDeviceException;

class Pushwoosh
{
Expand Down Expand Up @@ -42,17 +44,17 @@ public function createMessage(PushwooshPendingMessage $message)
try {
$response = $this->client->send($request);
} catch (GuzzleException $exception) {
throw PushwooshException::failedTransmission($exception);
throw new PushwooshException('Failed to create message(s)', 0, $exception);
}

$response = \GuzzleHttp\json_decode($response->getBody()->getContents());

if (isset($response->status_code) && $response->status_code !== 200) {
throw PushwooshException::apiError($response);
throw new PushwooshException($response->status_message);
}

if (isset($response->response->UnknownDevices)) {
throw PushwooshException::unknownDevices($response);
throw new UnknownDeviceException($response->response->UnknownDevices);
}

$message->wasSent();
Expand Down
52 changes: 0 additions & 52 deletions src/PushwooshException.php

This file was deleted.

9 changes: 5 additions & 4 deletions tests/Unit/PushwooshTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Str;
use Mockery;
use NotificationChannels\Pushwoosh\Exceptions\PushwooshException;
use NotificationChannels\Pushwoosh\Exceptions\UnknownDeviceException;
use NotificationChannels\Pushwoosh\Pushwoosh;
use NotificationChannels\Pushwoosh\PushwooshException;
use NotificationChannels\Pushwoosh\PushwooshPendingMessage;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -44,7 +45,7 @@ public function testApiError()
);

$this->expectException(PushwooshException::class);
$this->expectExceptionMessage('Pushwoosh API error: Access denied or application not found');
$this->expectExceptionMessage('Access denied or application not found');

$this->pushwoosh->createMessage(
new PushwooshPendingMessage($this->pushwoosh)
Expand Down Expand Up @@ -80,8 +81,8 @@ public function testUnknownDevices()
new Response(200, [], file_get_contents(__DIR__ . '/../Fixtures/unknown-devices.json'))
);

$this->expectException(PushwooshException::class);
$this->expectExceptionMessage('Unknown device(s) mentioned: foo, bar');
$this->expectException(UnknownDeviceException::class);
$this->expectExceptionMessage('Unknown device(s) referenced: foo, bar');

$this->pushwoosh->createMessage(
new PushwooshPendingMessage($this->pushwoosh)
Expand Down