Skip to content

Commit

Permalink
Gracefully handle requests for non-feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
jerodsanto committed Apr 9, 2024
1 parent f5d67f0 commit 6c4c756
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/changelog/schema/feed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ defmodule Changelog.Feed do

def get_by_slug(slug), do: Repo.get_by(__MODULE__, slug: slug)

def get_by_slug!(slug), do: Repo.get_by!(__MODULE__, slug: slug)

def with_podcast_id(query \\ __MODULE__, id),
do: from(q in query, where: fragment("? = ANY(?)", ^id, q.podcast_ids))

Expand Down
8 changes: 2 additions & 6 deletions lib/changelog_web/feeds.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ defmodule ChangelogWeb.Feeds do

# Special case for "The Changelog" feed which gets its episodes from
# News, Friends & Interviews
def generate("podcast") do
podcast = Podcast.changelog()
episodes = get_episodes(podcast)
render("podcast.xml", podcast: podcast, episodes: episodes)
end
def generate("podcast"), do: generate(Podcast.changelog())

def generate(%Feed{} = feed) do
episodes = get_episodes(feed)
Expand All @@ -99,7 +95,7 @@ defmodule ChangelogWeb.Feeds do

# When we have an unknown slug, look for a podcast followed by a feed
def generate(slug) do
generate(Podcast.get_by_slug(slug) || Feed.get_by_slug(slug))
generate(Podcast.get_by_slug(slug) || Feed.get_by_slug!(slug))
end

defp get_episodes(%Feed{} = feed) do
Expand Down
12 changes: 12 additions & 0 deletions test/changelog_web/controllers/feed_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ defmodule ChangelogWeb.FeedControllerTest do
assert conn.resp_body =~ e.title
end

test "a podcast feed that doesn't exist", %{conn: conn} do
assert_raise Ecto.NoResultsError, fn ->
get(conn, ~p"/comments/feed")
end
end

test "the plusplus feed with incorrect slug doesn't exist", %{conn: conn} do
Application.put_env(:changelog, :plusplus_slug, "8675309")
conn = get(conn, Routes.feed_path(conn, :plusplus, "justguessing"))
Expand Down Expand Up @@ -146,6 +152,12 @@ defmodule ChangelogWeb.FeedControllerTest do
refute conn.resp_body =~ e3.title
end

test "a custom feed that does not exist", %{conn: conn} do
assert_raise Ecto.NoResultsError, fn ->
get(conn, ~p"/feeds/8675309")
end
end

test "the posts feed", %{conn: conn} do
p1 = insert(:published_post)
p2 = insert(:post)
Expand Down

0 comments on commit 6c4c756

Please sign in to comment.