-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to handle "not found" with Beacon.DataSource.Behaviour? #359
Comments
It depends how you structure your site and pages, for eg on dockyard.com each blog post is a page so visiting a broken url will render a 404 page, eg: https://dockyard.com/blog/2023/11/01/nothing-here
At this moment it's up to you to make sure the data source is correct and you're calling an existing key. This process will be easier after we get to merge BeaconCMS/beacon_live_admin#84 which will let you manage the data source in the admin interface at runtime. Besides that, I've been discussing this problem with @APB9785 and exploring ways to warn users about incorrect data source, the issue to track is #369
I hope that answer your questions so I'm closing the issue, but feel free to post more comments. Thanks! |
Maybe I'm just confused with how the Datasource-thingy should work.. def live_data(:blog, ["posts", post_slug], _params) do
case BlogPosts.fetch_blog_post_by_slug(post_slug) do
{:ok, post} -> post
:error ->
nil # <==== what to return here to render a 404? nil? raise error?
end
end |
For this situation, you can define your own custom exception which is treated as 404 by Phoenix: defmodule YourApp.PageNotFoundError do
defexception plug_status: 404, message: nil, path: nil
def exception(opts) do
path = Keyword.fetch!(opts, :path)
%__MODULE__{message: "Page not found for path: #{Enum.join(path, "/")}", path: path}
end
end and then in your live_data handler: def live_data(:blog, ["posts", post_slug] = path, _params) do
case BlogPosts.fetch_blog_post_by_slug(post_slug) do
{:ok, post} -> %{post: post}
:error -> raise YourApp.PageNotFoundError, path: path
end
end Note the successful case creates a map of live_data assigns, which will be used in your template. |
Oh, didn't consider that option 😅 Not a fan of raising exceptions for non exceptional cases, but at least there is something. |
@kwando that's how Phoenix knows what to do :) |
How do we handle when data is not found in my BeaconDataSource? For instance in the demo app, what if I navigate to a blog post that does not exist?
Is there someway of signal back to beacon?
Maybe some async features here as well if the data is slow to get?
The text was updated successfully, but these errors were encountered: