Skip to content

Commit

Permalink
Merge pull request #56 from mbta/ph-make-bad-screen-id-404
Browse files Browse the repository at this point in the history
fix: respond to non-existent screen_ids with 404, not 500
  • Loading branch information
handorff authored Mar 9, 2020
2 parents 07b835d + a46d467 commit 1fd1b53
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions lib/screens_web/controllers/screen_controller.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
defmodule ScreensWeb.ScreenController do
use ScreensWeb, :controller

@default_app_id "bus_eink"
@app_ids ["bus_eink", "gl_eink_single", "gl_eink_double"]

plug(:api_version)
plug(:environment_name)

Expand Down Expand Up @@ -28,22 +31,39 @@ defmodule ScreensWeb.ScreenController do
end

def index(conn, %{"id" => app_id})
when app_id in ["bus_eink", "gl_eink_single", "gl_eink_double"] do
when app_id in @app_ids do
conn
|> assign(:app_id, app_id)
|> assign(:screen_ids, screen_ids(app_id))
|> render("index_multi.html")
end

def index(conn, %{"id" => screen_id}) do
app_id =
screen_data =
:screens
|> Application.get_env(:screen_data)
|> Map.get(screen_id)
|> Map.get(:app_id)

case screen_data do
nil ->
render_not_found(conn)

%{app_id: app_id} ->
conn
|> assign(:app_id, app_id)
|> render("index.html")
end
end

def index(conn, _params) do
render_not_found(conn)
end

defp render_not_found(conn) do
conn
|> assign(:app_id, app_id)
|> render("index.html")
|> assign(:app_id, @default_app_id)
|> put_status(:not_found)
|> put_view(ScreensWeb.ErrorView)
|> render("404.html")
end
end

0 comments on commit 1fd1b53

Please sign in to comment.