Skip to content
This repository has been archived by the owner on Jun 4, 2018. It is now read-only.

catch and log payload handling errors #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use GuzzleHttp;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use Slack\Message\Message;
Expand Down Expand Up @@ -33,16 +35,26 @@ class ApiClient
*/
protected $loop;

/**
* @var LoggerInterface
*/
protected $logger;

/**
* Creates a new API client instance.
*
* @param GuzzleHttp\ClientInterface $httpClient A Guzzle client instance to
* send requests with.
* @param LoopInterface $loop
* @param GuzzleHttp\ClientInterface $httpClient A Guzzle client instance to send requests with.
* @param LoggerInterface $logger
*/
public function __construct(LoopInterface $loop, GuzzleHttp\ClientInterface $httpClient = null)
public function __construct(
LoopInterface $loop,
GuzzleHttp\ClientInterface $httpClient = null,
LoggerInterface $logger = null)
{
$this->loop = $loop;
$this->httpClient = $httpClient ?: new GuzzleHttp\Client();
$this->logger = $logger ?: new NullLogger();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class Payload implements \ArrayAccess, \JsonSerializable
protected $data;

/**
* Creates a response object from a JSON message.
* Creates a payload object from a JSON message.
*
* @param string $json A JSON string.
*
* @return Response The parsed response.
* @return Payload The parsed message.
*/
public static function fromJson($json)
{
Expand Down
17 changes: 15 additions & 2 deletions src/RealTimeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Devristo\Phpws\Client\WebSocket;
use Devristo\Phpws\Messaging\WebSocketMessageInterface;
use Evenement\EventEmitterTrait;
use Exception;
use React\Promise;
use Slack\Message\Message;

Expand Down Expand Up @@ -361,13 +362,25 @@ public function isConnected()
/**
* Handles incoming websocket messages, parses them, and emits them as remote events.
*
* @param WebSocketMessageInterface $messageRaw A websocket message.
* @param WebSocketMessageInterface $message A websocket message.
*/
private function onMessage(WebSocketMessageInterface $message)
{
// parse the message and get the event name
$payload = Payload::fromJson($message->getData());

try {
$this->handlePayload($payload);
} catch (Exception $exception) {
$context = [
'payload' => $message->getData(),
'stackTrace' => $exception->getTrace(),
];
$this->logger->error('Payload handling error: '.$exception->getMessage(), $context);
}
}

private function handlePayload(Payload $payload)
{
if (isset($payload['type'])) {
switch ($payload['type']) {
case 'hello':
Expand Down