From 3046da7c67706eea9e05d5cc996282f21d5b59cf Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sun, 1 Dec 2019 21:30:58 -0500 Subject: [PATCH] add nicer lookup error handlers --- config/config.exs | 4 +++- lib/philomena_web/plugs/not_authorized_plug.ex | 14 ++++++++++++++ lib/philomena_web/plugs/not_found_plug.ex | 14 ++++++++++++++ .../templates/image/_image_container.html.slime | 2 +- lib/search/evaluator.ex | 2 +- 5 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 lib/philomena_web/plugs/not_authorized_plug.ex create mode 100644 lib/philomena_web/plugs/not_found_plug.ex diff --git a/config/config.exs b/config/config.exs index 2a3caee1..9d8f05c2 100644 --- a/config/config.exs +++ b/config/config.exs @@ -44,7 +44,9 @@ config :elastix, json_codec: Jason config :canary, - repo: Philomena.Repo + repo: Philomena.Repo, + unauthorized_handler: {PhilomenaWeb.NotAuthorizedPlug, :call}, + not_found_handler: {PhilomenaWeb.NotFoundPlug, :call} # Configures the endpoint config :philomena, PhilomenaWeb.Endpoint, diff --git a/lib/philomena_web/plugs/not_authorized_plug.ex b/lib/philomena_web/plugs/not_authorized_plug.ex new file mode 100644 index 00000000..ff985733 --- /dev/null +++ b/lib/philomena_web/plugs/not_authorized_plug.ex @@ -0,0 +1,14 @@ +defmodule PhilomenaWeb.NotAuthorizedPlug do + alias Phoenix.Controller + alias Plug.Conn + + def init([]), do: [] + + def call(conn), do: call(conn, nil) + def call(conn, _opts) do + conn + |> Controller.put_flash(:error, "You can't access that page.") + |> Controller.redirect(to: conn.assigns.referrer) + |> Conn.halt() + end +end \ No newline at end of file diff --git a/lib/philomena_web/plugs/not_found_plug.ex b/lib/philomena_web/plugs/not_found_plug.ex new file mode 100644 index 00000000..03e9275e --- /dev/null +++ b/lib/philomena_web/plugs/not_found_plug.ex @@ -0,0 +1,14 @@ +defmodule PhilomenaWeb.NotFoundPlug do + alias Phoenix.Controller + alias Plug.Conn + + def init([]), do: [] + + def call(conn), do: call(conn, nil) + def call(conn, _opts) do + conn + |> Controller.put_flash(:error, "Couldn't find what you were looking for!") + |> Controller.redirect(to: conn.assigns.referrer) + |> Conn.halt() + end +end \ No newline at end of file diff --git a/lib/philomena_web/templates/image/_image_container.html.slime b/lib/philomena_web/templates/image/_image_container.html.slime index b2b11c7e..00149135 100644 --- a/lib/philomena_web/templates/image/_image_container.html.slime +++ b/lib/philomena_web/templates/image/_image_container.html.slime @@ -38,7 +38,7 @@ picture img alt=hover_text - - {:filtered_video, hover_text} + - {:filtered_video, hover_text} -> video autoplay="autoplay" muted="muted" loop="loop" playsinline="playsinline" img alt=hover_text diff --git a/lib/search/evaluator.ex b/lib/search/evaluator.ex index 15fccea6..cb3f2ab7 100644 --- a/lib/search/evaluator.ex +++ b/lib/search/evaluator.ex @@ -49,7 +49,7 @@ defmodule Search.Evaluator do query_re = wildcard_to_regex(query_val) wrap(doc[atomify(term)]) - |> Enum.any?(&Regex.match?(query_re, &1)) + |> Enum.any?(&Regex.match?(query_re, &1 || "")) end def hits?(doc, %{match_phrase: phrase_query}) do