Skip to content

Commit

Permalink
better feedback when incoming message is not validated
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRDOrazio committed Nov 20, 2024
1 parent 5633b74 commit acafabc
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Health.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private static function validateMessageProperties(object $message): bool
*/
public function onMessage(ConnectionInterface $from, $msg)
{
echo sprintf('Receiving message "%s" from connection %d', $msg, $from->resourceId);
echo sprintf('Receiving message from connection %d: %s', $from->resourceId, $msg);
$messageReceived = json_decode($msg);
if (
json_last_error() === JSON_ERROR_NONE
Expand Down Expand Up @@ -163,6 +163,20 @@ public function onMessage(ConnectionInterface $from, $msg)
$message->text = $msg;
$this->sendMessage($from, $message);
}
} else {
if (json_last_error() !== JSON_ERROR_NONE) {
$errorMsg = json_last_error_msg();
} elseif (!property_exists($messageReceived, 'action')) {
$errorMsg = 'No action specified';
} elseif (!self::validateMessageProperties($messageReceived)) {
$errorMsg = 'Invalid message properties';
}
echo sprintf('Invalid message from connection %1$d: %2$s (%3$s)', $from->resourceId, $errorMsg, $msg);
$message = new \stdClass();
$message->type = "echobot";
$message->errorMsg = $errorMsg;
$message->text = sprintf('Invalid message from connection %d: %s', $from->resourceId, $msg);
$this->sendMessage($from, $message);
}
}

Expand Down

0 comments on commit acafabc

Please sign in to comment.