summaryrefslogtreecommitdiff
path: root/lib/mediasync/http_errors.ex
blob: 2b11ea6c0080195b6b7bb9ed8e0a4e08bb675f4b (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
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

  @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

  @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