5
5
use Clue \Redis \Protocol \Factory as ProtocolFactory ;
6
6
use Clue \Redis \Protocol \Parser \ParserInterface ;
7
7
use Knuckles \Faktory \Problems \CouldntConnect ;
8
+ use Knuckles \Faktory \Problems \ProtocolError ;
8
9
use Knuckles \Faktory \Problems \UnexpectedResponse ;
9
10
use Knuckles \Faktory \Utils \Json ;
10
11
use Psr \Log \LoggerAwareInterface ;
@@ -75,7 +76,7 @@ protected function handshake()
75
76
{
76
77
$ this ->readHi ();
77
78
$ this ->sendHello ();
78
- self :: checkOk ( $ this ->readLine (), operation: "Handshake " );
79
+ $ this ->readLine (operation: "Handshake " );
79
80
}
80
81
81
82
protected function readHi ()
@@ -102,13 +103,10 @@ protected function sendHello()
102
103
$ this ->send ("HELLO " , $ workerInfo );
103
104
}
104
105
105
- /**
106
- * Send a command and raise an error if the response is not OK.
107
- */
108
- public function operation ($ command , string ...$ args ): void
106
+ public function sendAndRead ($ command , string ...$ args ): mixed
109
107
{
110
108
$ this ->send ($ command , ...$ args );
111
- self :: checkOk ( $ this ->readLine (), operation: $ command );
109
+ return $ this ->readLine (operation: $ command );
112
110
}
113
111
114
112
public function send ($ command , string ...$ args ): void
@@ -122,7 +120,7 @@ public function send($command, string ...$args): void
122
120
fwrite ($ this ->connection , $ message );
123
121
}
124
122
125
- public function readLine (?int $ skipLines = 0 ): mixed
123
+ public function readLine (?int $ skipLines = 0 , $ operation = " Operation " ): mixed
126
124
{
127
125
if ($ this ->state == State::Disconnected) {
128
126
$ this ->connect ();
@@ -139,16 +137,11 @@ public function readLine(?int $skipLines = 0): mixed
139
137
$ messages = $ this ->responseParser ->pushIncoming ($ line );
140
138
if (empty ($ messages )) return null ;
141
139
142
- return $ messages [0 ]?->getValueNative();
143
- }
140
+ $ response = $ messages [0 ]?->getValueNative();
141
+ if (is_string ($ response ) && str_starts_with ($ response , "ERR " ))
142
+ throw UnexpectedResponse::from ($ operation , $ response );
144
143
145
- private static function checkOk (mixed $ result , $ operation = "Operation " )
146
- {
147
- if ($ result !== "OK " ) {
148
- throw UnexpectedResponse::from ($ operation , $ result );
149
- }
150
-
151
- return true ;
144
+ return $ response ;
152
145
}
153
146
154
147
public function setLogger (LoggerInterface $ logger ): void
0 commit comments