mirror of
https://github.com/plausible/analytics.git
synced 2024-12-29 20:42:01 +03:00
0007c0c108
* Remove "Context" namespace level * Change Goal string representation * Alias Schemas in Plugin API Test Case template * Update schema & tests for SharedLink resource * Update Goals interface - make it possible to create revenue goals - extract "for site" query to a standalone function * Fixup typespecs * Alias Errors module in OpenAPI controllers * Add missing goals test * Implement Goals Plugins API resource * Add extra test to confirm changeset error propagation * Mute credo * Fix typos * Handle changeset traversal in `Errors` * Use upserts in `Goals.find_or_create` * Extract touch_site! to Site.Cache, address credo, improve code docs * Apply formatting * Remove unused inner join * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update error message on revenue goal currency clash * Remove unused code --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
54 lines
1.4 KiB
Elixir
54 lines
1.4 KiB
Elixir
defmodule PlausibleWeb.Plugins.API.Errors do
|
|
@moduledoc """
|
|
Common responses for Plugins API
|
|
"""
|
|
|
|
import Plug.Conn
|
|
import Plausible.ChangesetHelpers
|
|
|
|
@spec unauthorized(Plug.Conn.t()) :: Plug.Conn.t()
|
|
def unauthorized(conn) do
|
|
conn
|
|
|> put_resp_header("www-authenticate", ~s[Basic realm="Plugins API Access"])
|
|
|> error(:unauthorized, "Plugins API: unauthorized")
|
|
end
|
|
|
|
@spec error(
|
|
Plug.Conn.t(),
|
|
Plug.Conn.status(),
|
|
String.t() | [String.t()] | Ecto.Changeset.t() | [Ecto.Changeset.t()]
|
|
) ::
|
|
Plug.Conn.t()
|
|
def error(conn, status, message) when is_binary(message) do
|
|
error(conn, status, [message])
|
|
end
|
|
|
|
def error(conn, status, message) when is_map(message) do
|
|
error(conn, status, [message])
|
|
end
|
|
|
|
def error(conn, status, messages) when is_list(messages) do
|
|
response =
|
|
Jason.encode!(%{
|
|
errors:
|
|
Enum.map(messages, fn
|
|
message when is_binary(message) ->
|
|
%{detail: message}
|
|
|
|
%Ecto.Changeset{} = changeset ->
|
|
changeset
|
|
|> traverse_errors()
|
|
|> Enum.map(fn {key, message} ->
|
|
%{detail: "#{key}: #{message}"}
|
|
end)
|
|
end)
|
|
|> List.flatten()
|
|
})
|
|
|
|
conn
|
|
|> put_resp_content_type("application/json")
|
|
|> send_resp(status, response)
|
|
|> halt()
|
|
end
|
|
end
|