From: Maksim Pechnikov Date: Sat, 22 Aug 2020 07:56:26 +0000 (+0300) Subject: updated errors on add emoji X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=0922791e4d2233d527dda23e66a952e3f359a3fe;p=akkoma updated errors on add emoji --- diff --git a/lib/pleroma/web/api_spec/operations/pleroma_emoji_file_operation.ex b/lib/pleroma/web/api_spec/operations/pleroma_emoji_file_operation.ex index 7dd4ce311..efbfce75f 100644 --- a/lib/pleroma/web/api_spec/operations/pleroma_emoji_file_operation.ex +++ b/lib/pleroma/web/api_spec/operations/pleroma_emoji_file_operation.ex @@ -71,7 +71,8 @@ defmodule Pleroma.Web.ApiSpec.PleromaEmojiFileOperation do 200 => Operation.response("Files Object", "application/json", files_object()), 404 => Operation.response("Not Found", "application/json", ApiError), 400 => Operation.response("Bad Request", "application/json", ApiError), - 409 => Operation.response("Conflict", "application/json", ApiError) + 409 => Operation.response("Conflict", "application/json", ApiError), + 422 => Operation.response("Unprocessable Entity", "application/json", ApiError) } } end @@ -118,7 +119,8 @@ defmodule Pleroma.Web.ApiSpec.PleromaEmojiFileOperation do responses: %{ 200 => Operation.response("Files Object", "application/json", files_object()), 400 => Operation.response("Bad Request", "application/json", ApiError), - 404 => Operation.response("Not Found", "application/json", ApiError) + 404 => Operation.response("Not Found", "application/json", ApiError), + 422 => Operation.response("Unprocessable Entity", "application/json", ApiError) } } end diff --git a/lib/pleroma/web/pleroma_api/controllers/emoji_file_controller.ex b/lib/pleroma/web/pleroma_api/controllers/emoji_file_controller.ex index d10f46fde..71c53df1d 100644 --- a/lib/pleroma/web/pleroma_api/controllers/emoji_file_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/emoji_file_controller.ex @@ -32,22 +32,13 @@ defmodule Pleroma.Web.PleromaAPI.EmojiFileController do |> put_status(:conflict) |> json(%{error: "An emoji with the \"#{shortcode}\" shortcode already exists"}) - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "pack \"#{pack_name}\" is not found"}) - {:error, :empty_values} -> conn |> put_status(:unprocessable_entity) |> json(%{error: "pack name, shortcode or filename cannot be empty"}) - {:error, _} -> - render_error( - conn, - :internal_server_error, - "Unexpected error occurred while adding file to pack." - ) + {:error, _} = error -> + handle_error(conn, error, %{pack_name: pack_name}) end end @@ -60,11 +51,6 @@ defmodule Pleroma.Web.PleromaAPI.EmojiFileController do {:ok, pack} <- Pack.update_file(pack, shortcode, new_shortcode, new_filename, force) do json(conn, pack.files) else - {:error, :doesnt_exist} -> - conn - |> put_status(:bad_request) - |> json(%{error: "Emoji \"#{shortcode}\" does not exist"}) - {:error, :already_exists} -> conn |> put_status(:conflict) @@ -73,22 +59,13 @@ defmodule Pleroma.Web.PleromaAPI.EmojiFileController do "New shortcode \"#{new_shortcode}\" is already used. If you want to override emoji use 'force' option" }) - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "pack \"#{pack_name}\" is not found"}) - {:error, :empty_values} -> conn - |> put_status(:bad_request) + |> put_status(:unprocessable_entity) |> json(%{error: "new_shortcode or new_filename cannot be empty"}) - {:error, _} -> - render_error( - conn, - :internal_server_error, - "Unexpected error occurred while updating file in pack." - ) + {:error, _} = error -> + handle_error(conn, error, %{pack_name: pack_name, code: shortcode}) end end @@ -97,30 +74,36 @@ defmodule Pleroma.Web.PleromaAPI.EmojiFileController do {:ok, pack} <- Pack.delete_file(pack, shortcode) do json(conn, pack.files) else - {:error, :doesnt_exist} -> - conn - |> put_status(:bad_request) - |> json(%{error: "Emoji \"#{shortcode}\" does not exist"}) - - {:error, :not_found} -> - conn - |> put_status(:not_found) - |> json(%{error: "pack \"#{pack_name}\" is not found"}) - {:error, :empty_values} -> conn - |> put_status(:bad_request) + |> put_status(:unprocessable_entity) |> json(%{error: "pack name or shortcode cannot be empty"}) - {:error, _} -> - render_error( - conn, - :internal_server_error, - "Unexpected error occurred while removing file from pack." - ) + {:error, _} = error -> + handle_error(conn, error, %{pack_name: pack_name, code: shortcode}) end end + defp handle_error(conn, {:error, :doesnt_exist}, %{code: emoji_code}) do + conn + |> put_status(:bad_request) + |> json(%{error: "Emoji \"#{emoji_code}\" does not exist"}) + end + + defp handle_error(conn, {:error, :not_found}, %{pack_name: pack_name}) do + conn + |> put_status(:not_found) + |> json(%{error: "pack \"#{pack_name}\" is not found"}) + end + + defp handle_error(conn, {:error, _}, _) do + render_error( + conn, + :internal_server_error, + "Unexpected error occurred while adding file to pack." + ) + end + defp get_filename(%Plug.Upload{filename: filename}), do: filename defp get_filename(url) when is_binary(url), do: Path.basename(url)