chore(portal): Log error for unknown channel messages (#8299)

Instead of crashing, it would make sense to log these and let the
connected entity maintain its WebSocket connection.

This should never happen in practice if we maintain our version
compatibility matrix properly, but it will help reduce the blast radius
of a channel message bug that happens to slip out into the wild.

Fixes #4679
This commit is contained in:
Jamil
2025-03-03 21:21:39 +00:00
committed by GitHub
parent a6110d7f5f
commit fee808bc62
6 changed files with 44 additions and 0 deletions

View File

@@ -900,6 +900,13 @@ defmodule API.Client.Channel do
end
end
# Catch-all for unknown messages
def handle_in(message, payload, socket) do
Logger.error("Unknown client message", message: message, payload: payload)
{:reply, {:error, %{reason: :unknown_message}}, socket}
end
defp select_relays(socket, except_ids \\ []) do
{:ok, relays} =
Relays.all_connected_relays_for_account(socket.assigns.subject.account, except_ids)

View File

@@ -657,6 +657,13 @@ defmodule API.Gateway.Channel do
end
end
# Catch-all for unknown messages
def handle_in(message, payload, socket) do
Logger.error("Unknown gateway message", message: message, payload: payload)
{:reply, {:error, %{reason: :unknown_message}}, socket}
end
defp encode_ref(socket, tuple) do
ref =
tuple

View File

@@ -2,6 +2,7 @@ defmodule API.Relay.Channel do
use API, :channel
alias Domain.Relays
require OpenTelemetry.Tracer
require Logger
@impl true
def join("relay", %{"stamp_secret" => stamp_secret}, socket) do
@@ -45,4 +46,12 @@ defmodule API.Relay.Channel do
{:noreply, socket}
end
end
# Catch-all for unknown messages
@impl true
def handle_in(message, payload, socket) do
Logger.error("Unknown relay message", message: message, payload: payload)
{:reply, {:error, %{reason: :unknown_message}}, socket}
end
end

View File

@@ -2384,4 +2384,11 @@ defmodule API.Client.ChannelTest do
assert client.id == client_id
end
end
describe "handle_in/3 for unknown messages" do
test "it doesn't crash", %{socket: socket} do
ref = push(socket, "unknown_message", %{})
assert_reply ref, :error, %{reason: :unknown_message}
end
end
end

View File

@@ -1181,4 +1181,11 @@ defmodule API.Gateway.ChannelTest do
assert upserted_activity.account_id == account.id
end
end
describe "handle_in/3 for unknown messages" do
test "it doesn't crash", %{socket: socket} do
ref = push(socket, "unknown_message", %{})
assert_reply ref, :error, %{reason: :unknown_message}
end
end
end

View File

@@ -52,4 +52,11 @@ defmodule API.Relay.ChannelTest do
assert_push "init", %{}
end
end
describe "handle_in/3 for unknown messages" do
test "it doesn't crash", %{socket: socket} do
ref = push(socket, "unknown_message", %{})
assert_reply ref, :error, %{reason: :unknown_message}
end
end
end