From 319f8c4c45d44d5ffb9f00131a624be4e7713d64 Mon Sep 17 00:00:00 2001 From: Viktor Baranov Date: Fri, 11 Aug 2023 18:22:15 +0300 Subject: [PATCH] API v1 500 error convert to 404, if just path is incorrect --- CHANGELOG.md | 1 + .../controllers/api/rpc/rpc_translator.ex | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6acbe7470..569c081ef3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ ### Fixes +- [#8187](https://github.com/blockscout/blockscout/pull/8187) - API v1 500 error convert to 404, if requested path is incorrect - [#7852](https://github.com/blockscout/blockscout/pull/7852) - Token balances refactoring & fixes - [#7872](https://github.com/blockscout/blockscout/pull/7872) - Fix pending gas price in pending tx - [#7875](https://github.com/blockscout/blockscout/pull/7875) - Fix twin compiler version diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/rpc_translator.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/rpc_translator.ex index 53a5ec3add..b27554eed3 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/rpc_translator.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/rpc_translator.ex @@ -29,7 +29,7 @@ defmodule BlockScoutWeb.API.RPC.RPCTranslator do end def call(%Conn{params: %{"module" => module, "action" => action}} = conn, translations) do - with true <- valid_api_request_path(conn), + with {:valid_api_request, true} <- {:valid_api_request, valid_api_request_path(conn)}, {:ok, {controller, write_actions}} <- translate_module(translations, module), {:ok, action} <- translate_action(action), true <- action_accessed?(action, write_actions), @@ -58,6 +58,13 @@ defmodule BlockScoutWeb.API.RPC.RPCTranslator do :rate_limit_reached -> AccessHelper.handle_rate_limit_deny(conn) + {:valid_api_request, false} -> + conn + |> put_status(404) + |> put_view(RPCView) + |> Controller.render(:error, error: "Not found") + |> halt() + _ -> conn |> put_status(500) @@ -119,7 +126,8 @@ defmodule BlockScoutWeb.API.RPC.RPCTranslator do end defp valid_api_request_path(conn) do - if conn.request_path == "/api" || conn.request_path == "/api/v1" do + if conn.request_path == "/api" || conn.request_path == "/api/" || conn.request_path == "/api/v1" || + conn.request_path == "/api/v1/" do true else false