diff --git a/src/ApiClient.php b/src/ApiClient.php index 831219e..d21f00f 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -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; @@ -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(); } /** diff --git a/src/Payload.php b/src/Payload.php index e1d657e..c750870 100644 --- a/src/Payload.php +++ b/src/Payload.php @@ -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) { diff --git a/src/RealTimeClient.php b/src/RealTimeClient.php index be0480e..51bb5d9 100644 --- a/src/RealTimeClient.php +++ b/src/RealTimeClient.php @@ -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; @@ -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':