Error handling fixes

This commit is contained in:
Chris 2024-03-01 21:21:09 +01:00
parent 9bd53062b0
commit c4f3e8ae50
2 changed files with 11 additions and 12 deletions

View File

@ -73,8 +73,8 @@ class CommandBusClient implements CommandBusInterface
function (DuplexStreamInterface $connection) {
$this->connection = $connection;
$this->emit(self::EVENT_CONNECTED);
$connection->on('error', function () {
$this->emit(self::EVENT_DISCONNECTED);
$connection->on('error', function ($error) {
$this->emit(self::EVENT_DISCONNECTED, [ $error ]);
if ($this->autoReconnect) {
Loop::addTimer(1, $this->reconnect(...));
}
@ -124,15 +124,14 @@ class CommandBusClient implements CommandBusInterface
$this->emit(self::EVENT_NOTIFY, [ $event, $data ]);
break;
case Message::MSGTYPE_ERROR: // error
var_dump($message);
$uuid = $message->getUuid();
$error = $message->getData()['error'];
if ($uuid === "") {
$this->emit('error', [ $error ]);
if ($uuid && array_key_exists($uuid, $this->pending)) {
$this->pending[$uuid]->reject(new \Exception($error));
unset($this->pending[$uuid]);
} else {
if (array_key_exists($uuid, $this->pending)) {
$this->pending[$uuid]->reject(new \Exception($error));
unset($this->pending[$uuid]);
}
$this->emit('error', [ $error ]);
}
break;
default:

View File

@ -29,16 +29,16 @@ class Message implements JsonSerializable
private array $messageData;
public function __construct(string $messageType, array $messageData = [], ?string $uuid = null)
public function __construct(string $messageType, array $messageData = [], null|string|false $uuid = null)
{
$this->uuid = $uuid ?? (string)Uuid::v7();
$this->uuid = ($uuid===null) ? (string)Uuid::v7() : $uuid;
$this->messageType = $messageType;
$this->messageData = $messageData;
}
public function getUuid(): string
{
return $this->uuid;
return $this->uuid ? $this->uuid : "";
}
public function getType(): string
@ -63,7 +63,7 @@ class Message implements JsonSerializable
if (!$json || empty($json['msg'])) {
throw new MessageException("Invalid data");
}
return new Message($json['msg'], $json['data'], $json['uuid']??"");
return new Message($json['msg'], $json['data'], $json['uuid']??false);
}
public function asResult($result): Message