improve ajax error messages

This commit is contained in:
byte[] 2020-09-27 23:53:14 -04:00
parent fc159c3782
commit a00cdab739
3 changed files with 30 additions and 12 deletions

View file

@ -8,7 +8,8 @@ function fetchJson(verb, endpoint, body) {
credentials: 'same-origin', credentials: 'same-origin',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'x-csrf-token': window.booru.csrfToken 'x-csrf-token': window.booru.csrfToken,
'x-requested-with': 'xmlhttprequest'
}, },
}; };
@ -24,7 +25,8 @@ function fetchHtml(endpoint) {
return fetch(endpoint, { return fetch(endpoint, {
credentials: 'same-origin', credentials: 'same-origin',
headers: { headers: {
'x-csrf-token': window.booru.csrfToken 'x-csrf-token': window.booru.csrfToken,
'x-requested-with': 'xmlhttprequest'
}, },
}); });
} }

View file

@ -7,6 +7,13 @@ defmodule PhilomenaWeb.NotAuthorizedPlug do
def call(conn), do: call(conn, nil) def call(conn), do: call(conn, nil)
def call(conn, _opts) do def call(conn, _opts) do
case conn.assigns.ajax? do
true ->
conn
|> Conn.resp(:forbidden, "You can't access that page.")
|> Conn.halt()
_false ->
conn conn
|> Controller.fetch_flash() |> Controller.fetch_flash()
|> Controller.put_flash(:error, "You can't access that page.") |> Controller.put_flash(:error, "You can't access that page.")
@ -14,3 +21,4 @@ defmodule PhilomenaWeb.NotAuthorizedPlug do
|> Conn.halt() |> Conn.halt()
end end
end end
end

View file

@ -7,6 +7,13 @@ defmodule PhilomenaWeb.NotFoundPlug do
def call(conn), do: call(conn, nil) def call(conn), do: call(conn, nil)
def call(conn, _opts) do def call(conn, _opts) do
case conn.assigns.ajax? do
true ->
conn
|> Conn.resp(:not_found, "Couldn't find what you were looking for!")
|> Conn.halt()
false ->
conn conn
|> Controller.fetch_flash() |> Controller.fetch_flash()
|> Controller.put_flash(:error, "Couldn't find what you were looking for!") |> Controller.put_flash(:error, "Couldn't find what you were looking for!")
@ -14,3 +21,4 @@ defmodule PhilomenaWeb.NotFoundPlug do
|> Conn.halt() |> Conn.halt()
end end
end end
end