summaryrefslogtreecommitdiff
path: root/lib/mediasync/http_errors.ex
blob: cbbb6133eac7ffc6ad428c0e98b24f846394d456 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
defmodule Mediasync.HTTPErrors do
  import Plug.Conn
  import Mediasync.Utils

  @video_url_too_large Jason.encode!(
                         %{
                           "error" => "videoUrlTooLarge",
                           "maxSize" => 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" => "invalidVideoUrl"}, 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" => "notFound",
                 "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" => "invalidCsrfToken",
                          "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

  @spec send_bad_request(Plug.Conn.t()) :: Plug.Conn.t()
  @spec send_bad_request(Plug.Conn.t(), message: String.t() | nil) :: Plug.Conn.t()
  def send_bad_request(conn), do: send_bad_request(conn, message: nil)
  def send_bad_request(conn, []), do: send_bad_request(conn, message: nil)

  def send_bad_request(conn, message: message) do
    error = %{
      "error" => "badRequest"
    }

    error =
      if message do
        Map.put(error, "message", message)
      else
        error
      end

    conn
    |> put_json_content_type()
    |> send_resp(400, Jason.encode!(error))
  end

  @bad_gateway Jason.encode!(%{"error" => "badGateway"}, 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