Skip to content

Commit bb44c54

Browse files
author
Valentin Atanasov
authored
fix: handle invalid unix dates in filter (#1870)
1 parent 5bdcf70 commit bb44c54

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

lib/ae_mdw_web/plugs/paginated_plug.ex

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule AeMdwWeb.Plugs.PaginatedPlug do
33

44
import Plug.Conn
55

6+
alias AeMdw.Db.State
67
alias AeMdw.Db.Util, as: DbUtil
78
alias Phoenix.Controller
89
alias Plug.Conn
@@ -86,22 +87,28 @@ defmodule AeMdwWeb.Plugs.PaginatedPlug do
8687
when scope_type in @scope_types_keys do
8788
scope_type = Map.fetch!(@scope_types, scope_type)
8889

89-
case extract_range(range) do
90+
range
91+
|> extract_range()
92+
|> case do
9093
{:ok, first, last} when first < last ->
91-
{:ok, :forward, generate_range(state, scope_type, first, last)}
94+
{:forward, generate_range(state, scope_type, first, last)}
9295

9396
{:ok, first, last} when first > last ->
94-
{:ok, :backward, generate_range(state, scope_type, last, first)}
97+
{:backward, generate_range(state, scope_type, last, first)}
9598

9699
{:ok, first, last} ->
97100
if Map.get(params, "direction", "backward") == "forward" do
98-
{:ok, :forward, generate_range(state, scope_type, last, first)}
101+
{:forward, generate_range(state, scope_type, last, first)}
99102
else
100-
{:ok, :backward, generate_range(state, scope_type, last, first)}
103+
{:backward, generate_range(state, scope_type, last, first)}
101104
end
102105

103106
{:error, reason} ->
104-
{:error, reason}
107+
{nil, {:error, reason}}
108+
end
109+
|> case do
110+
{_direction, {:error, reason}} -> {:error, reason}
111+
{direction, range} -> {:ok, direction, range}
105112
end
106113
end
107114

@@ -222,15 +229,23 @@ defmodule AeMdwWeb.Plugs.PaginatedPlug do
222229
end
223230
end
224231

232+
@spec generate_range(State.t(), atom(), pos_integer(), pos_integer()) ::
233+
{atom(), Range.t()} | {:error, atom()}
225234
defp generate_range(state, :time, first, last) do
226-
{first_txi, last_txi} =
227-
DbUtil.time_to_txi(
228-
state,
229-
first |> DateTime.from_unix!() |> DateTime.to_unix(:millisecond),
230-
last |> DateTime.from_unix!() |> DateTime.to_unix(:millisecond)
231-
)
232-
233-
generate_range(state, :txi, first_txi, last_txi)
235+
with {_first, {:ok, first_parsed}} <- {first, DateTime.from_unix(first)},
236+
{_last, {:ok, last_parsed}} <- {last, DateTime.from_unix(last)} do
237+
{first_txi, last_txi} =
238+
DbUtil.time_to_txi(
239+
state,
240+
DateTime.to_unix(first_parsed, :millisecond),
241+
DateTime.to_unix(last_parsed, :millisecond)
242+
)
243+
244+
generate_range(state, :txi, first_txi, last_txi)
245+
else
246+
{invalid_unix_time, {:error, _reason}} ->
247+
{:error, "invalid unix time: #{invalid_unix_time}"}
248+
end
234249
end
235250

236251
defp generate_range(_state, scope_type, first, last) do

test/ae_mdw_web/plugs/paginated_plug_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ defmodule AeMdwWeb.Plugs.PaginatedPlugTest do
117117
|> PaginatedPlug.call([])
118118
|> get_assigns()
119119

120+
assert %{"error" => "invalid unix time: 10000000000000"} =
121+
conn
122+
|> with_store(store)
123+
|> put_query(%{
124+
"scope" => "time:10000000000000-200000000000",
125+
"direction" => "forward"
126+
})
127+
|> PaginatedPlug.call([])
128+
|> json_response(400)
129+
120130
assert %{"error" => "invalid scope: asdf"} =
121131
conn
122132
|> with_store(store)

0 commit comments

Comments
 (0)