diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 7172ca3..f55c2ba 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -57,17 +57,17 @@ "problemMatcher": "$mixTestFailure" }, { - "label": "run testbed test at cursor", + "label": "mix test focused", "type": "shell", "command": "mix", "args": [ "test", - "../${relativeFile}:${lineNumber}", + "${relativeFile}:${lineNumber}", "--color", "--trace" ], "options": { - "cwd": "${workspaceRoot}/testbed", + "cwd": "${workspaceRoot}", "requireFiles": [ "test/**/test_helper.exs", "test/**/*_test.exs" diff --git a/lib/live_state/channel.ex b/lib/live_state/channel.ex index 5e5c08d..c8cfed7 100644 --- a/lib/live_state/channel.ex +++ b/lib/live_state/channel.ex @@ -107,7 +107,8 @@ defmodule LiveState.Channel do {:noreply, initialize_state(state, socket)} {:error, error} -> - {:error, error} + push_error(socket, error) + {:noreply, socket} end end @@ -194,6 +195,14 @@ defmodule LiveState.Channel do push(socket, name, detail) end + def push_error(socket, message) when is_binary(message) do + push(socket, "error", %{message: message}) + end + + def push_error(socket, error) do + push(socket, "error", error) + end + defp push_state_change(socket, state, version) do payload = %{} |> Map.put(state_key(), state) |> Map.put(state_version_key(), version) push(socket, "state:change", payload) diff --git a/test/bad_init_channel_test.exs b/test/bad_init_channel_test.exs new file mode 100644 index 0000000..cbc2d8e --- /dev/null +++ b/test/bad_init_channel_test.exs @@ -0,0 +1,30 @@ +defmodule LiveState.LiveStateChannelTest do + use ExUnit.Case + + import Phoenix.ChannelTest + alias LiveState.Test.BadInitChannel + alias LiveState.Test.UserSocket + + import LiveState.TestHelpers + + @endpoint LiveState.Test.Endpoint + + setup do + start_supervised(@endpoint) + start_supervised(Phoenix.PubSub.child_spec(name: LiveState.Test.PubSub)) + + {:ok, _, socket} = + socket(UserSocket, "wut", %{}) + |> subscribe_and_join(BadInitChannel, "wutever", %{}) + + {:ok, %{socket: socket}} + end + + test "init" do + assert_push( + "error", + %{message: "you stink"} + ) + end + +end diff --git a/test/support/bad_init_channel.ex b/test/support/bad_init_channel.ex new file mode 100644 index 0000000..c4b75dd --- /dev/null +++ b/test/support/bad_init_channel.ex @@ -0,0 +1,10 @@ +defmodule LiveState.Test.BadInitChannel do + @moduledoc false + + use LiveState.Channel, web_module: LiveState.Test.Web + + def init(_channel, _params, _socket) do + {:error, "you stink"} + end + +end