Skip to content

Commit 040c120

Browse files
authored
fix: handle /tx/:hash endpoint when tx doesn't exist (#686)
1 parent 7d11889 commit 040c120

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

lib/ae_mdw/txs.ex

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -339,23 +339,22 @@ defmodule AeMdw.Txs do
339339
def fetch(tx_hash, add_spendtx_details? \\ true)
340340

341341
def fetch(tx_hash, add_spendtx_details?) when is_binary(tx_hash) do
342-
mb_hash = :aec_db.find_tx_location(tx_hash)
343-
344-
case :aec_chain.get_header(mb_hash) do
345-
{:ok, mb_header} ->
346-
mb_header
347-
|> :aec_headers.height()
348-
|> Blocks.fetch_txis_from_gen()
349-
|> Stream.map(&Database.fetch!(@table, &1))
350-
|> Enum.find_value(
351-
{:error, ErrInput.NotFound.exception(value: tx_hash)},
352-
fn
353-
Model.tx(id: ^tx_hash) = tx -> {:ok, render(tx, add_spendtx_details?)}
354-
_tx -> nil
355-
end
356-
)
357-
342+
with mb_hash when mb_hash != :not_found <- :aec_db.find_tx_location(tx_hash),
343+
{:ok, mb_header} <- :aec_chain.get_header(mb_hash) do
344+
mb_header
345+
|> :aec_headers.height()
346+
|> Blocks.fetch_txis_from_gen()
347+
|> Stream.map(&Database.fetch!(@table, &1))
348+
|> Enum.find_value(
349+
{:error, ErrInput.NotFound.exception(value: tx_hash)},
350+
fn
351+
Model.tx(id: ^tx_hash) = tx -> {:ok, render(tx, add_spendtx_details?)}
352+
_tx -> nil
353+
end
354+
)
355+
else
358356
:not_found ->
357+
tx_hash = :aeser_api_encoder.encode(:tx_hash, tx_hash)
359358
{:error, ErrInput.NotFound.exception(value: tx_hash)}
360359
end
361360
end

test/ae_mdw_web/controllers/tx_controller_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ defmodule AeMdwWeb.TxControllerTest do
1818
end
1919
end
2020
end
21+
22+
describe "tx" do
23+
test "when tx not found, it returns 404", %{conn: conn} do
24+
tx_hash = "th_2TbTPmKFU31WNQKfBGe5b5JDF9sFdAY7qot1smnxjbsEiu7LNr"
25+
26+
assert %{"error" => _error_msg} = conn |> get("/tx/#{tx_hash}") |> json_response(404)
27+
end
28+
end
2129
end

0 commit comments

Comments
 (0)