22
33namespace Clue \React \Redis ;
44
5- use Evenement \EventEmitter ;
6- use Clue \Redis \Protocol \Parser \ParserInterface ;
7- use Clue \Redis \Protocol \Parser \ParserException ;
8- use Clue \Redis \Protocol \Serializer \SerializerInterface ;
95use Clue \Redis \Protocol \Factory as ProtocolFactory ;
10- use UnderflowException ;
11- use RuntimeException ;
12- use InvalidArgumentException ;
13- use React \Promise \Deferred ;
146use Clue \Redis \Protocol \Model \ErrorReply ;
157use Clue \Redis \Protocol \Model \ModelInterface ;
168use Clue \Redis \Protocol \Model \MultiBulkReply ;
9+ use Clue \Redis \Protocol \Parser \ParserException ;
10+ use Clue \Redis \Protocol \Parser \ParserInterface ;
11+ use Clue \Redis \Protocol \Serializer \SerializerInterface ;
12+ use Evenement \EventEmitter ;
13+ use React \Promise \Deferred ;
1714use React \Stream \DuplexStreamInterface ;
1815
1916/**
@@ -47,18 +44,20 @@ public function __construct(DuplexStreamInterface $stream, ParserInterface $pars
4744 $ stream ->on ('data ' , function ($ chunk ) use ($ parser , $ that ) {
4845 try {
4946 $ models = $ parser ->pushIncoming ($ chunk );
50- }
51- catch (ParserException $ error ) {
52- $ that ->emit ('error ' , array ($ error ));
47+ } catch (ParserException $ error ) {
48+ $ that ->emit ('error ' , array (new \UnexpectedValueException (
49+ 'Invalid data received: ' . $ error ->getMessage () . ' (EBADMSG) ' ,
50+ defined ('SOCKET_EBADMSG ' ) ? SOCKET_EBADMSG : 77 ,
51+ $ error
52+ )));
5353 $ that ->close ();
5454 return ;
5555 }
5656
5757 foreach ($ models as $ data ) {
5858 try {
5959 $ that ->handleMessage ($ data );
60- }
61- catch (UnderflowException $ error ) {
60+ } catch (\UnderflowException $ error ) {
6261 $ that ->emit ('error ' , array ($ error ));
6362 $ that ->close ();
6463 return ;
@@ -84,11 +83,20 @@ public function __call($name, $args)
8483 static $ pubsubs = array ('subscribe ' , 'unsubscribe ' , 'psubscribe ' , 'punsubscribe ' );
8584
8685 if ($ this ->ending ) {
87- $ request ->reject (new RuntimeException ('Connection closed ' ));
86+ $ request ->reject (new \RuntimeException (
87+ 'Connection ' . ($ this ->closed ? 'closed ' : 'closing ' ). ' (ENOTCONN) ' ,
88+ defined ('SOCKET_ENOTCONN ' ) ? SOCKET_ENOTCONN : 107
89+ ));
8890 } elseif (count ($ args ) !== 1 && in_array ($ name , $ pubsubs )) {
89- $ request ->reject (new InvalidArgumentException ('PubSub commands limited to single argument ' ));
91+ $ request ->reject (new \InvalidArgumentException (
92+ 'PubSub commands limited to single argument (EINVAL) ' ,
93+ defined ('SOCKET_EINVAL ' ) ? SOCKET_EINVAL : 22
94+ ));
9095 } elseif ($ name === 'monitor ' ) {
91- $ request ->reject (new \BadMethodCallException ('MONITOR command explicitly not supported ' ));
96+ $ request ->reject (new \BadMethodCallException (
97+ 'MONITOR command explicitly not supported (ENOTSUP) ' ,
98+ defined ('SOCKET_ENOTSUP ' ) ? SOCKET_ENOTSUP : (defined ('SOCKET_EOPNOTSUPP ' ) ? SOCKET_EOPNOTSUPP : 95 )
99+ ));
92100 } else {
93101 $ this ->stream ->write ($ this ->serializer ->getRequestMessage ($ name , $ args ));
94102 $ this ->requests []= $ request ;
@@ -131,7 +139,10 @@ public function handleMessage(ModelInterface $message)
131139 }
132140
133141 if (!$ this ->requests ) {
134- throw new UnderflowException ('Unexpected reply received, no matching request found ' );
142+ throw new \UnderflowException (
143+ 'Unexpected reply received, no matching request found (ENOMSG) ' ,
144+ defined ('SOCKET_ENOMSG ' ) ? SOCKET_ENOMSG : 42
145+ );
135146 }
136147
137148 $ request = array_shift ($ this ->requests );
@@ -173,8 +184,11 @@ public function close()
173184 // reject all remaining requests in the queue
174185 while ($ this ->requests ) {
175186 $ request = array_shift ($ this ->requests );
176- /* @var $request Request */
177- $ request ->reject (new RuntimeException ('Connection closing ' ));
187+ /* @var $request Deferred */
188+ $ request ->reject (new \RuntimeException (
189+ 'Connection closing (ENOTCONN) ' ,
190+ defined ('SOCKET_ENOTCONN ' ) ? SOCKET_ENOTCONN : 107
191+ ));
178192 }
179193 }
180194}
0 commit comments