Skip to content

Commit 3b8e8e3

Browse files
committed
fix command error message missing
1 parent 179e1d8 commit 3b8e8e3

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/Lookup/TcpLookupService.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Pulsar\Lookup;
1212

13-
13+
use Protobuf\AbstractMessage;
1414
use Pulsar\Exception\IOException;
1515
use Pulsar\Exception\OptionsException;
1616
use Pulsar\Exception\RuntimeException;
@@ -124,7 +124,7 @@ public function lookup(string $topic): Result
124124

125125
//
126126
default:
127-
throw new RuntimeException($subCommand->getMessage());
127+
$this->handleCommandFailed($subCommand);
128128
}
129129
}
130130

@@ -152,7 +152,7 @@ public function getPartitionedTopicMetadata(string $topic): int
152152
*/
153153
$subCommand = $results->subCommand;
154154
if ($subCommand->getResponse()->value() == CommandPartitionedTopicMetadataResponse\LookupType::Failed_VALUE) {
155-
throw new RuntimeException($subCommand->getMessage());
155+
$this->handleCommandFailed($subCommand);
156156
}
157157

158158
return $subCommand->getPartitions();
@@ -214,4 +214,20 @@ public function close()
214214
{
215215
$this->connection->close();
216216
}
217-
}
217+
218+
/**
219+
* @return void
220+
* @throws RuntimeException
221+
*/
222+
protected function handleCommandFailed(AbstractMessage $command)
223+
{
224+
$code = 0;
225+
$msg = $command->getMessage();
226+
if ($command->hasError()) {
227+
$code = $command->getError()->value();
228+
$msg = $command->getError()->name();
229+
}
230+
231+
throw RuntimeException($msg, $code);
232+
}
233+
}

src/Response.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,7 @@ public function getSubCommand(): AbstractMessage
110110
protected function checkError()
111111
{
112112
if ($this->subCommand instanceof CommandError) {
113-
throw new RuntimeException(
114-
$this->subCommand->getMessage(),
115-
$this->subCommand->getError()->value()
116-
);
113+
$this->handleSubCommandError();
117114
}
118115

119116
$this->checkServerError();
@@ -127,10 +124,22 @@ protected function checkError()
127124
protected function checkServerError()
128125
{
129126
if ($this->subCommand instanceof CommandSendError) {
130-
throw new RuntimeException(
131-
$this->subCommand->getMessage(),
132-
$this->subCommand->getError()->value()
133-
);
127+
$this->handleSubCommandError();
134128
}
135129
}
136-
}
130+
131+
/**
132+
* @return void
133+
* @throws RuntimeException
134+
*/
135+
protected function handleSubCommandError()
136+
{
137+
$code = 0;
138+
$msg = $this->subCommand->getMessage();
139+
if ($this->subCommand->hasError()) {
140+
$code = $this->subCommand->getError()->value();
141+
$msg = $msg ?: $this->subCommand->getError()->name();
142+
}
143+
throw new RuntimeException($msg, $code);
144+
}
145+
}

0 commit comments

Comments
 (0)