Phoenix_live_view: LiveView rendered via a controller raises error

Created on 18 Feb 2020  路  1Comment  路  Source: phoenixframework/phoenix_live_view

Environment

  • Elixir version (elixir -v): 1.10.0
  • Phoenix version (mix deps): 1.4.13
  • Phoenix LiveView version (mix deps): 0.7.1
  • NodeJS version (node -v): 10.18.1
  • NPM version (npm -v): 6.13.4
  • Operating system: Linux
  • Browsers you attempted to reproduce this bug on (the more the merrier): Chromium 79.0.3945.130 (Build) (64 bit)
  • Does the problem persist after removing "assets/node_modules" and trying again? Yes/no: yes

Actual behavior

So, the story is that I'm trying the rendering of one only LiveView via a Controller because i need to send a 404 in some cases and I didn't know how to do it in another way. Anyway, that LiveView was previously routed using live so it has an handle_params function defined and is rendered with this line:

    live_render(conn, Journey.Manage.TableLive, router: Journey.Manage.Router,
      session: %{"params" => params})

so with a router option defined because I wanted to generate paths for it.

Trying out the view raises this error

13:31:16.478 [error] GenServer #PID<0.980.0> terminating
** (RuntimeError) cannot invoke handle_params/3 for Journey.Manage.TableLive because Journey.Manage.TableLivewas not mounted at the router with the live/3 macro under URL "http://localhost:4001/cont/location/page/3"
    (phoenix_live_view 0.7.1) lib/phoenix_live_view/channel.ex:203: Phoenix.LiveView.Channel.maybe_call_mount_handle_params/4
    (phoenix_live_view 0.7.1) lib/phoenix_live_view/channel.ex:604: Phoenix.LiveView.Channel.verified_mount/4
    (phoenix_live_view 0.7.1) lib/phoenix_live_view/channel.ex:34: Phoenix.LiveView.Channel.handle_info/2
    (stdlib 3.10) gen_server.erl:637: :gen_server.try_dispatch/4
    (stdlib 3.10) gen_server.erl:711: :gen_server.handle_msg/6
    (stdlib 3.10) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: {:mount, Phoenix.LiveView.Channel}
State: {%{"joins" => 0, "params" => %{"_csrf_token" => "<a token>"}, "session" => "<a session>", "static" => "<a static>", "url" => "http://localhost:4001/cont/location/page/3"}, {#PID<0.976.0>, #Reference<0.2186393052.255590402.119379>}, %Phoenix.Socket{assigns: %{}, channel: Phoenix.LiveView.Channel, channel_pid: nil, endpoint: Journey.Manage.Endpoint, handler: Phoenix.LiveView.Socket, id: nil, join_ref: "4", joined: false, private: %{session: %{"_csrf_token" => "Jx6sy-47xywhm5eN_9T2EsJT"}}, pubsub_server: Journey.Manage.PubSub, ref: nil, serializer: Phoenix.Socket.V2.JSONSerializer, topic: "lv:phx-FfR_EBHS3cBWbQMh", transport: :websocket, transport_pid: #PID<0.976.0>}}
13:31:16.478 [error] an exception was raised:
    ** (RuntimeError) cannot invoke handle_params/3 for Journey.Manage.TableLive because Journey.Manage.TableLivewas not mounted at the router with the live/3 macro under URL "http://localhost:4001/cont/location/page/3"
        (phoenix_live_view 0.7.1) lib/phoenix_live_view/channel.ex:203: Phoenix.LiveView.Channel.maybe_call_mount_handle_params/4
        (phoenix_live_view 0.7.1) lib/phoenix_live_view/channel.ex:604: Phoenix.LiveView.Channel.verified_mount/4
        (phoenix_live_view 0.7.1) lib/phoenix_live_view/channel.ex:34: Phoenix.LiveView.Channel.handle_info/2
        (stdlib 3.10) gen_server.erl:637: :gen_server.try_dispatch/4
        (stdlib 3.10) gen_server.erl:711: :gen_server.handle_msg/6
        (stdlib 3.10) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

Expected behavior

At least that the handle_params function is ignored and the error not raised

Most helpful comment

So, the story is that I'm trying the rendering of one only LiveView via a Controller because i need to send a 404 in some cases and I didn't know how to do it in another way

You can have a plug in your router that handles these cases or you can raise an exception on LiveView mount and make the exception be treated as 404 by implementing Plug.Exception.

We may remove this error in the future but for now we want to push folks to declare their LiveView in the router as much as possible.

Thanks!

>All comments

So, the story is that I'm trying the rendering of one only LiveView via a Controller because i need to send a 404 in some cases and I didn't know how to do it in another way

You can have a plug in your router that handles these cases or you can raise an exception on LiveView mount and make the exception be treated as 404 by implementing Plug.Exception.

We may remove this error in the future but for now we want to push folks to declare their LiveView in the router as much as possible.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings