aboutsummaryrefslogtreecommitdiff
path: root/lib/mediasync/http_errors.ex
blob: c38486a8fe44380dbe61c979c76e6baa38db64a0 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
defmodule Mediasync.HTTPErrors do
  import Plug.Conn
  import Mediasync.Utils

  @video_url_too_large Jason.encode!(
                         %{
                           "error" => "video_url_too_large",
                           "max_size" => Application.compile_env(:mediasync, :max_video_url_size)
                         },
                         pretty: true
                       )

  @spec send_video_url_too_large(Plug.Conn.t()) :: Plug.Conn.t()
  @spec send_video_url_too_large(Plug.Conn.t(), []) :: Plug.Conn.t()
  def send_video_url_too_large(conn, _opts \\ []) do
    conn
    |> put_json_content_type()
    |> send_resp(
      400,
      @video_url_too_large
    )
  end

  @invalid_video_url Jason.encode!(%{"error" => "invalid_video_url"}, pretty: true)

  @spec send_invalid_video_url(Plug.Conn.t()) :: Plug.Conn.t()
  @spec send_invalid_video_url(Plug.Conn.t(), []) :: Plug.Conn.t()
  def send_invalid_video_url(conn, _opts \\ []) do
    conn
    |> put_json_content_type()
    |> send_resp(
      400,
      @invalid_video_url
    )
  end

  @not_found Jason.encode!(
               %{
                 "error" => "not_found",
                 "message" => "No page was found at this location."
               },
               pretty: true
             )

  @spec send_not_found(Plug.Conn.t()) :: Plug.Conn.t()
  @spec send_not_found(Plug.Conn.t(), []) :: Plug.Conn.t()
  def send_not_found(conn, _opts \\ []) do
    conn
    |> put_json_content_type()
    |> send_resp(
      404,
      @not_found
    )
  end

  @forbidden Jason.encode!(%{"error" => "forbidden"}, pretty: true)

  @spec send_forbidden(Plug.Conn.t()) :: Plug.Conn.t()
  @spec send_forbidden(Plug.Conn.t(), []) :: Plug.Conn.t()
  def send_forbidden(conn, _opts \\ []) do
    conn
    |> put_json_content_type()
    |> send_resp(
      403,
      @forbidden
    )
  end

  @invalid_csrf_token Jason.encode!(
                        %{
                          "error" => "invalid_csrf_token",
                          "message" => "Try reloading the previous page and retrying."
                        },
                        pretty: true
                      )

  @spec send_invalid_csrf_token(Plug.Conn.t()) :: Plug.Conn.t()
  @spec send_invalid_csrf_token(Plug.Conn.t(), []) :: Plug.Conn.t()
  def send_invalid_csrf_token(conn, _opts \\ []) do
    conn
    |> put_json_content_type()
    |> send_resp(400, @invalid_csrf_token)
  end

  @bad_gateway Jason.encode!(%{"error" => "bad_gateway"}, pretty: true)

  @spec send_bad_gateway(Plug.Conn.t()) :: Plug.Conn.t()
  @spec send_bad_gateway(Plug.Conn.t(), []) :: Plug.Conn.t()
  def send_bad_gateway(conn, _opts \\ []) do
    conn
    |> put_json_content_type()
    |> send_resp(502, @bad_gateway)
  end

  @unknown Jason.encode!(
             %{
               "error" => "unknown",
               "message" =>
                 "Something went wrong. Consider reloading the previous page and retrying your action."
             },
             pretty: true
           )

  @spec send_unknown(Plug.Conn.t()) :: Plug.Conn.t()
  @spec send_unknown(Plug.Conn.t(), []) :: Plug.Conn.t()
  def send_unknown(conn, _opts \\ []) do
    conn
    |> put_json_content_type()
    |> send_resp(500, @unknown)
  end
end