Merge branch 'fix-auto-link-for-profile-fields' into 'develop'
[akkoma] / lib / pleroma / web / mastodon_api / controllers / fallback_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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, error_message}) do
24 conn
25 |> put_status(:bad_request)
26 |> json(%{error: error_message})
27 end
28
29 def call(conn, _) do
30 conn
31 |> put_status(:internal_server_error)
32 |> json(dgettext("errors", "Something went wrong"))
33 end
34 end