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

View File

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