Generalize SSR.NotConfigured exception

This commit is contained in:
Douglas Vought 2023-09-14 18:31:55 -04:00 committed by Wout De Puysseleir
parent df7b4b8935
commit 1119f41cb2
4 changed files with 16 additions and 7 deletions

View file

@ -81,7 +81,7 @@ defmodule LiveSvelte do
SSR.render(assigns.name, props, slots)
rescue
SSR.NodeNotConfigured -> nil
SSR.NotConfigured -> nil
end
end

View file

@ -1,10 +1,7 @@
defmodule LiveSvelte.SSR.NodeNotConfigured do
defmodule LiveSvelte.SSR.NotConfigured do
@moduledoc false
defexception message: """
NodeJS is not configured. Please add the following to your application.ex:
{NodeJS.Supervisor, [path: LiveSvelte.SSR.server_path(), pool_size: 4]},
"""
defexception [:message]
end
defmodule LiveSvelte.SSR do

View file

@ -6,7 +6,12 @@ defmodule LiveSvelte.SSR.NodeJS do
try do
NodeJS.call!({"server", "render"}, [name, props, slots])
catch
:exit, {:noproc, _} -> raise LiveSvelte.SSR.NodeNotConfigured
:exit, {:noproc, _} ->
message = """
NodeJS is not configured. Please add the following to your application.ex:
{NodeJS.Supervisor, [path: LiveSvelte.SSR.NodeJS.server_path(), pool_size: 4]},
"""
raise %LiveSvelte.SSR.NotConfigured{message: message}
end
end

View file

@ -6,6 +6,13 @@ defmodule LiveSvelte.SSRTest do
assert Application.get_env(:live_svelte, :ssr_module) == LiveSvelte.SSR.NodeJS
end
test "Node.js raises the correct exception" do
load_config("config/config.exs")
assert_raise(LiveSvelte.SSR.NotConfigured, fn ->
LiveSvelte.SSR.NodeJS.render("Test", %{}, %{})
end)
end
test "It uses a different SSR module" do
load_config("test/ssr_test/test_config.exs")
assert Application.get_env(:live_svelte, :ssr_module) == SomeOtherSSRModule