Merge pull request 'purge chat and shout endpoints' (#97) from purge-chat into develop
[akkoma] / lib / pleroma / web / mastodon_api / controllers / fallback_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.FallbackController do
6 use Pleroma.Web, :controller
7
8 def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
9 error_message =
10 changeset
11 |> Ecto.Changeset.traverse_errors(fn {message, _opt} -> message end)
12 |> Enum.map_join(", ", fn {_k, v} -> v end)
13
14 conn
15 |> put_status(:unprocessable_entity)
16 |> json(%{error: error_message})
17 end
18
19 def call(conn, {:error, :not_found}) do
20 render_error(conn, :not_found, "Record not found")
21 end
22
23 def call(conn, {:error, :forbidden}) do
24 render_error(conn, :forbidden, "Access denied")
25 end
26
27 def call(conn, {:error, error_message}) do
28 conn
29 |> put_status(:bad_request)
30 |> json(%{error: error_message})
31 end
32
33 def call(conn, {:error, status, message}) do
34 conn
35 |> put_status(status)
36 |> json(%{error: message})
37 end
38
39 def call(conn, _) do
40 conn
41 |> put_status(:internal_server_error)
42 |> json(dgettext("errors", "Something went wrong"))
43 end
44 end