File size: 2,264 Bytes
88211d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
defmodule PlausibleWeb.ErrorView do
  use Plausible
  use PlausibleWeb, :view

  def render("500.json", %{conn: %{assigns: %{plugins_api: true}}}) do
    contact_support_note =
      on_ee 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]
    last_event = Sentry.get_last_event_id_and_source()

    case {current_user, last_event} do
      {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
        }

        render("server_error.html", Map.merge(opts, assigns))

      _ ->
        render("server_error.html", assigns)
    end
  end

  def template_not_found(template, assigns) do
    if String.ends_with?(template, ".json") do
      fallback_json_error(template, assigns)
    else
      fallback_html_error(template, assigns)
    end
  end

  defp fallback_html_error(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

  defp fallback_json_error(template, _assigns) do
    status =
      String.split(template, ".")
      |> hd()
      |> String.to_integer()

    message = Plug.Conn.Status.reason_phrase(status)
    %{status: status, message: message}
  rescue
    _ -> %{status: 500, message: "Server error"}
  end

  defp url_path(%Plug.Conn{request_path: path, query_string: ""}), do: path
  defp url_path(%Plug.Conn{request_path: path, query_string: query}), do: path <> "?" <> query
end