updated errors on add emoji
authorMaksim Pechnikov <parallel588@gmail.com>
Sat, 22 Aug 2020 07:56:26 +0000 (10:56 +0300)
committerMaksim Pechnikov <parallel588@gmail.com>
Sat, 22 Aug 2020 07:56:26 +0000 (10:56 +0300)
lib/pleroma/web/api_spec/operations/pleroma_emoji_file_operation.ex
lib/pleroma/web/pleroma_api/controllers/emoji_file_controller.ex

index 7dd4ce311e56b7606efd9603666f17faa02713f9..efbfce75f57b8e4af96f97c82ff7a172e084b8bc 100644 (file)
@@ -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
index d10f46fde5b37ef673eacef304a295ec76a2497a..71c53df1d19f2768ea43a2ae5df15e9ead6f9697 100644 (file)
@@ -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)