Improve error handling

This commit is contained in:
Chris 2024-03-01 19:21:28 +01:00
parent 9d3f6d9ddd
commit 1255db21ab
2 changed files with 13 additions and 1 deletions

View File

@ -121,6 +121,18 @@ class CommandBusClient implements CommandBusInterface
$data = (array)($message->getData()['data']??[]); $data = (array)($message->getData()['data']??[]);
$this->emit(self::EVENT_NOTIFY, [ $event, $data ]); $this->emit(self::EVENT_NOTIFY, [ $event, $data ]);
break; break;
case Message::MSGTYPE_ERROR: // error
$uuid = $message->getUuid();
$error = $message->getData()['error'];
if ($uuid === "") {
$this->emit('error', [ $error ]);
} else {
if (array_key_exists($uuid, $this->pending)) {
$this->pending[$uuid]->reject(new \Exception($error));
unset($this->pending[$uuid]);
}
}
break;
default: default:
$this->connection->end('{"msg":"error","data":{"error":"Unexpected message type"}}'); $this->connection->end('{"msg":"error","data":{"error":"Unexpected message type"}}');
} }

View File

@ -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']??"");
} }
public function asResult($result): Message public function asResult($result): Message