Skip to content

Commit

Permalink
init channel errors can happen
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Nelson committed Oct 7, 2023
1 parent 11003bb commit 5a1880e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 10 additions & 1 deletion lib/live_state/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions test/bad_init_channel_test.exs
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions test/support/bad_init_channel.ex
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5a1880e

Please sign in to comment.