analytics/lib/plausible_web/views/error_view.ex
hq1 082ec91c63
OpenAPI: first pass on Plugins API - Shared Links (#3378)
* Update depenedencies: OpenAPISpex + cursor based pagination

* Update formatter config

* Add internal server error implementation

* Test errors

* Implement pagination interface

* Implement Plugins API module macros

* Implement Public API base URI

(to be used with path helpers once called from within
forwarded router's scope)

* Implement OpenAPI specs + schemas

* Implement Shared Links context module

* Add pagination and error views

* Add Shared Link view

* Implement Shared Link controller

* Expose SharedLink.t() spec

* Implement separate router for the Plugins API

* Update moduledocs

* Always wrap resource objects with `data`

* Update moduledoc

* Use https://github.com/open-api-spex/open_api_spex/pull/425

due to https://github.com/open-api-spex/open_api_spex/issues/92

* Rely on BASE_URL for swagger-ui server definition

* Fixup goals migration

* Migrate broken goals before deleting dupes

* Remove bypassing test rate limiting for which there's none anyway

* Move the context module under `Plausible.` namespace

* Bring back conn assignment to PluginsAPICase template

* Update test/plausible_web/plugins/api/controllers/shared_links_test.exs

Co-authored-by: Uku Taht <Uku.taht@gmail.com>

* Update renamed aliases

* Seed static token for development purposes

* Delegate Plugins API 500s to a familiar shape

* Simplify with statement

---------

Co-authored-by: Uku Taht <Uku.taht@gmail.com>
2023-10-02 11:18:49 +02:00

64 lines
1.7 KiB
Elixir

defmodule PlausibleWeb.ErrorView do
use PlausibleWeb, :view
def render("500.json", %{conn: %{private: %{PlausibleWeb.Plugins.API.Router => _}}}) do
contact_support_note =
if not Plausible.Release.selfhost?() do
"If the problem persists please contact support@plausible.io"
end
%{
errors: [
%{detail: "Internal server error, please try again. #{contact_support_note}"}
]
}
end
def render("500.json", _assigns) do
%{
status: 500,
message: "Server error"
}
end
def render("404.html", assigns) do
assigns =
assigns
|> Map.put(:status, 404)
|> Map.put_new(:message, "Oops! There's nothing here")
render("404_error.html", assigns)
end
def render(<<"5", _error_5xx::binary-size(2), ".html">>, assigns) do
current_user = assigns[:current_user]
selfhosted? = Plausible.Release.selfhost?()
last_event = Sentry.get_last_event_id_and_source()
case {selfhosted?, current_user, last_event} do
{false, current_user, {event_id, :plug}}
when is_binary(event_id) and not is_nil(current_user) ->
opts = %{
trace_id: event_id,
user_name: current_user.name,
user_email: current_user.email,
selfhosted?: selfhosted?
}
render("server_error.html", Map.merge(opts, assigns))
_ ->
render("server_error.html", Map.put(assigns, :selfhosted?, selfhosted?))
end
end
def template_not_found(template, assigns) do
assigns =
assigns
|> Map.put_new(:message, Phoenix.Controller.status_message_from_template(template))
|> Map.put(:status, String.trim_trailing(template, ".html"))
render("generic_error.html", assigns)
end
end