tfwright / live_admin

Low-config admin UI for Phoenix apps, built on LiveView
MIT License
248 stars 22 forks source link

:locale not found in: nil #61

Closed Rio517 closed 6 months ago

Rio517 commented 11 months ago

I updated to 0.10.0 and ran into this error. I don't need a locale for my liveadmin.

[error] GenServer #PID<0.929.0> terminating
** (KeyError) key :locale not found in: nil. If you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map
    (live_admin 0.10.0) lib/live_admin/router.ex:117: LiveAdmin.Router.on_mount/4
    (phoenix_live_view 0.19.5) lib/phoenix_live_view/lifecycle.ex:149: anonymous fn/4 in Phoenix.LiveView.Lifecycle.mount/3
    (phoenix_live_view 0.19.5) lib/phoenix_live_view/lifecycle.ex:212: Phoenix.LiveView.Lifecycle.reduce_socket/3
    (phoenix_live_view 0.19.5) lib/phoenix_live_view/utils.ex:392: anonymous fn/6 in Phoenix.LiveView.Utils.maybe_call_live_view_mount!/5
    (telemetry 1.2.1) /home/mario/wunderwork_petal/deps/telemetry/src/telemetry.erl:321: :telemetry.span/3
    (phoenix_live_view 0.19.5) lib/phoenix_live_view/channel.ex:1041: Phoenix.LiveView.Channel.verified_mount/8
    (phoenix_live_view 0.19.5) lib/phoenix_live_view/channel.ex:59: Phoenix.LiveView.Channel.handle_info/2
    (stdlib 4.3) gen_server.erl:1123: :gen_server.try_dispatch/4
    (stdlib 4.3) gen_server.erl:1200: :gen_server.handle_msg/6
    (stdlib 4.3) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
Last message: {Phoenix.Channel, %{"flash" => nil, "params" => %{"_csrf_token" => "QGMMPB8SO0sBEDZoWj06AmN8SR0KNy8QuUgvfYU8NeRQlnJ2T43QIggQ", "_mounts" => 1}, "session" => "SFMyNTY.g2gDaAJhBXQAAAAIZAACaWRtAAAAFHBoeC1GM1dtUWEzZEs3clNETmhrZAAMbGl2ZV9zZXNzaW9uaAJkABVsaXZlX2FkbWluXy9hZG1pbi9taW5uCAACMLA7QaZ1F2QACnBhcmVudF9waWRkAANuaWxkAAhy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s3clNETmhrbgYAR-8TlokBYgABUYA.KZG1iBvp5NKPqzCFVqAgpbn9loMJH2AeoxJbP-_hVdw", "url" => "http://local.wunderwork.io:4000/admin/min/org-memberships"}, {#PID<0.923.0>, #Reference<0.673198271.1378091009.215671>}, %Phoenix.Socket{assigns: %{}, channel: Phoenix.LiveView.Channel, channel_pid: nil, endpoint: PetalProWeb.Endpoint, handler: Phoenix.LiveView.Socket, id: "users_sessions:Fj4fDG5YYxwH1nYkQiinl7XMVuR6erxmMlo6nPGeXcU=", joined: false, join_ref: "11", private: %{connect_info: %{session: %{"_csrf_token" => "56kJyKnsOud96Sp07HzLCPHA", "live_socket_id" => "users_sessions:Fj4fDG5YYxwH1nYkQiinl7XMVuR6erxmMlo6nPGeXcU=", "locale" => "en", "user_token" => <<22, 62, 31, 12, 110, 88, 99, 28, 7, 214, 118, 36, 66, 40, 167, 151, 181, 204, 86, 228, 122, 122, 188, 102, 50, 90, 58, 156, 241, 158, 93, ...>>}, user_agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"}}, pubsub_server: PetalPro.PubSub, ref: nil, serializer: Phoenix.Socket.V2.JSONSerializer, topic: "lv:phx-F3WmQa3dK7rSDNhk", transport: :websocket, transport_pid: #PID<0.923.0>}}
State: #Reference<0.673198271.1378091009.215673>
[error] an exception was raised:
    ** (KeyError) key :locale not found in: nil. If you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map
        (live_admin 0.10.0) lib/live_admin/router.ex:117: LiveAdmin.Router.on_mount/4
        (phoenix_live_view 0.19.5) lib/phoenix_live_view/lifecycle.ex:149: anonymous fn/4 in Phoenix.LiveView.Lifecycle.mount/3
        (phoenix_live_view 0.19.5) lib/phoenix_live_view/lifecycle.ex:212: Phoenix.LiveView.Lifecycle.reduce_socket/3
        (phoenix_live_view 0.19.5) lib/phoenix_live_view/utils.ex:392: anonymous fn/6 in Phoenix.LiveView.Utils.maybe_call_live_view_mount!/5
        (telemetry 1.2.1) /home/mario/wunderwork_petal/deps/telemetry/src/telemetry.erl:321: :telemetry.span/3
        (phoenix_live_view 0.19.5) lib/phoenix_live_view/channel.ex:1041: Phoenix.LiveView.Channel.verified_mount/8
        (phoenix_live_view 0.19.5) lib/phoenix_live_view/channel.ex:59: Phoenix.LiveView.Channel.handle_info/2
        (stdlib 4.3) gen_server.erl:1123: :gen_server.try_dispatch/4
        (stdlib 4.3) gen_server.erl:1200: :gen_server.handle_msg/6
        (stdlib 4.3) proc_lib.erl:240: :proc_lib.init_p_do_apply/3

App versions:

"phoenix": "1.7.7"
"phoenix_ecto": "4.4.2"
"phoenix_html": "3.3.1"
"phoenix_live_dashboard": "0.8.0"
"phoenix_live_reload": "1.4.1"
"phoenix_live_view": "0.19.5"
"phoenix_pubsub": "2.1.3"
"phoenix_swoosh": "1.2.0"
"phoenix_template": "1.0.3"
"phoenix_view": "2.0.2"
"plug": "1.14.2"
"plug_cowboy": "2.6.1"
tfwright commented 11 months ago

I think I've seen this before and I think it's actually related to the session store rather than locale config. From what I observed this error goes away after the initial mount?

tfwright commented 11 months ago

I'm not able to reproduce this on current master, hopefully recent changes have resolved the issue, but feel free to reopen if it still causing you problems on the latest version.

Rio517 commented 10 months ago

FYI, This is still an issue on mount.

[info]Last message: {Phoenix.Channel, %{"flash" => nil, "params" => %{"_csrf_token" => "xxx", "_mounts" => 1}, "session" => "xxxx", "static" => "xxx", "url" => "https://app.wunderwork.io/admin/min/profiles"}, {#PID<0.3308.0>, #Reference<0.232030912.3053715457.94995>}, %Phoenix.Socket{assigns: %{}, channel: Phoenix.LiveView.Channel, channel_pid: nil, endpoint: PetalProWeb.Endpoint, handler: Phoenix.LiveView.Socket, id: "users_sessions:xxx=", joined: false, join_ref: "39", private: %{connect_info: %{session: %{"_csrf_token" => "Knxr_bpOpQ5mDZutbJQAIw0r", "live_socket_id" => "users_sessions:xxx=", "locale" => "en", "user_token" => <<, ...>>}, user_agent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"}}, pubsub_server: PetalPro.PubSub, ref: nil, serializer: Phoenix.Socket.V2.JSONSerializer, topic: "lv:phx-F4DPxEZwFYa2EwLx", transport: :websocket, transport_pid: #PID<0.3308.0>}}
[info]State: #Reference<0.232030912.3053715457.94997>
[info]15:41:07.129 [error] an exception was raised:
[info]    ** (KeyError) key :locale not found in: nil. If you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map
[info]        (live_admin 0.10.2) lib/live_admin/router.ex:117: LiveAdmin.Router.on_mount/4
[info]        (phoenix_live_view 0.19.5) lib/phoenix_live_view/lifecycle.ex:149: anonymous fn/4 in Phoenix.LiveView.Lifecycle.mount/3
[info]        (phoenix_live_view 0.19.5) lib/phoenix_live_view/lifecycle.ex:212: Phoenix.LiveView.Lifecycle.reduce_socket/3
[info]        (phoenix_live_view 0.19.5) lib/phoenix_live_view/utils.ex:392: anonymous fn/6 in Phoenix.LiveView.Utils.maybe_call_live_view_mount!/5
[info]        (telemetry 1.2.1) /app/deps/telemetry/src/telemetry.erl:321: :telemetry.span/3
[info]        (phoenix_live_view 0.19.5) lib/phoenix_live_view/channel.ex:1041: Phoenix.LiveView.Channel.verified_mount/8
[info]        (phoenix_live_view 0.19.5) lib/phoenix_live_view/channel.ex:59: Phoenix.LiveView.Channel.handle_info/2
[info]        (stdlib 4.3) gen_server.erl:1123: :gen_server.try_dispatch/4
TrevorHinesley commented 9 months ago

I'm experiencing this as well

tfwright commented 8 months ago

AFAICT this occurs when the app is restarted while sockets are connected. The restart clears the session data but the sockets do not rebuild the session until the socket disconnects, which causes the error and a full reload, after which the session data has been reinitialized.

I can certainly improve the error returned, making it easier to handle as necessary, but I'm not sure ignoring the missing session is a good idea.

If the error being raised is causing problems, I would say the "best" solution would be to implement your own session store, it's pretty easy to do, although I really need to add documentation around it.

I could also make it possible to disable the session store entirely, which would silence the error (but also would mean things like prefix would be "forgotten" on every navigation event).

Let me know if there are concerns one or more of the above options would not address.

Rio517 commented 8 months ago

FYI: In my case, this seems to cause refresh looping behavior for end users until they manually refresh the page.

Since I'm not using prefixes, perhaps silencing the error is the best approach in my case.

tfwright commented 8 months ago

I'm not sure how this could cause looping since as I said above the default session store initializes properly on mount after a reload. If you are able to share an app that demonstrates the behavior I can take a closer look.

tfwright commented 6 months ago

I was poking around a bit more at this issue and I realized https://github.com/tfwright/live_admin/commit/a9b5370ac9a7e065b5d334cbe9d7f6ff5272f6c0 may have exacerbated the issue because it causes the agent process to crash instead of just the LV process, which may have resulted in more errors that necessary being raised.

I went ahead and reverted that in b1e8ddc, as well as added a custom error message that clarifies what's happening.

With that I'm going to go ahead and close this since the original error should no longer be raised.