updated errors on add emoji

This commit is contained in:
Maksim Pechnikov 2020-08-22 10:56:26 +03:00
parent f5845ff033
commit 0922791e4d
2 changed files with 32 additions and 47 deletions

View file

@ -71,7 +71,8 @@ defmodule Pleroma.Web.ApiSpec.PleromaEmojiFileOperation do
200 => Operation.response("Files Object", "application/json", files_object()), 200 => Operation.response("Files Object", "application/json", files_object()),
404 => Operation.response("Not Found", "application/json", ApiError), 404 => Operation.response("Not Found", "application/json", ApiError),
400 => Operation.response("Bad Request", "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 end
@ -118,7 +119,8 @@ defmodule Pleroma.Web.ApiSpec.PleromaEmojiFileOperation do
responses: %{ responses: %{
200 => Operation.response("Files Object", "application/json", files_object()), 200 => Operation.response("Files Object", "application/json", files_object()),
400 => Operation.response("Bad Request", "application/json", ApiError), 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 end

View file

@ -32,22 +32,13 @@ defmodule Pleroma.Web.PleromaAPI.EmojiFileController do
|> put_status(:conflict) |> put_status(:conflict)
|> json(%{error: "An emoji with the \"#{shortcode}\" shortcode already exists"}) |> 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} -> {:error, :empty_values} ->
conn conn
|> put_status(:unprocessable_entity) |> put_status(:unprocessable_entity)
|> json(%{error: "pack name, shortcode or filename cannot be empty"}) |> json(%{error: "pack name, shortcode or filename cannot be empty"})
{:error, _} -> {:error, _} = error ->
render_error( handle_error(conn, error, %{pack_name: pack_name})
conn,
:internal_server_error,
"Unexpected error occurred while adding file to pack."
)
end end
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 {:ok, pack} <- Pack.update_file(pack, shortcode, new_shortcode, new_filename, force) do
json(conn, pack.files) json(conn, pack.files)
else else
{:error, :doesnt_exist} ->
conn
|> put_status(:bad_request)
|> json(%{error: "Emoji \"#{shortcode}\" does not exist"})
{:error, :already_exists} -> {:error, :already_exists} ->
conn conn
|> put_status(:conflict) |> 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" "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} -> {:error, :empty_values} ->
conn conn
|> put_status(:bad_request) |> put_status(:unprocessable_entity)
|> json(%{error: "new_shortcode or new_filename cannot be empty"}) |> json(%{error: "new_shortcode or new_filename cannot be empty"})
{:error, _} -> {:error, _} = error ->
render_error( handle_error(conn, error, %{pack_name: pack_name, code: shortcode})
conn,
:internal_server_error,
"Unexpected error occurred while updating file in pack."
)
end end
end end
@ -97,29 +74,35 @@ defmodule Pleroma.Web.PleromaAPI.EmojiFileController do
{:ok, pack} <- Pack.delete_file(pack, shortcode) do {:ok, pack} <- Pack.delete_file(pack, shortcode) do
json(conn, pack.files) json(conn, pack.files)
else else
{:error, :doesnt_exist} -> {:error, :empty_values} ->
conn
|> put_status(:unprocessable_entity)
|> json(%{error: "pack name or shortcode cannot be empty"})
{: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 conn
|> put_status(:bad_request) |> put_status(:bad_request)
|> json(%{error: "Emoji \"#{shortcode}\" does not exist"}) |> json(%{error: "Emoji \"#{emoji_code}\" does not exist"})
end
{:error, :not_found} -> defp handle_error(conn, {:error, :not_found}, %{pack_name: pack_name}) do
conn conn
|> put_status(:not_found) |> put_status(:not_found)
|> json(%{error: "pack \"#{pack_name}\" is not found"}) |> json(%{error: "pack \"#{pack_name}\" is not found"})
end
{:error, :empty_values} -> defp handle_error(conn, {:error, _}, _) do
conn
|> put_status(:bad_request)
|> json(%{error: "pack name or shortcode cannot be empty"})
{:error, _} ->
render_error( render_error(
conn, conn,
:internal_server_error, :internal_server_error,
"Unexpected error occurred while removing file from pack." "Unexpected error occurred while adding file to pack."
) )
end end
end
defp get_filename(%Plug.Upload{filename: filename}), do: filename defp get_filename(%Plug.Upload{filename: filename}), do: filename
defp get_filename(url) when is_binary(url), do: Path.basename(url) defp get_filename(url) when is_binary(url), do: Path.basename(url)