From b0137b879858733cec5a83d74ef88d8649cdc18c Mon Sep 17 00:00:00 2001 From: Jon Zimbel Date: Thu, 24 Oct 2024 08:31:14 -0400 Subject: [PATCH] tweak: Report exceptions raised under GTFS import endpoints for easier debugging in gtfs_creator CI --- .../controllers/api/gtfs_import_controller.ex | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/arrow_web/controllers/api/gtfs_import_controller.ex b/lib/arrow_web/controllers/api/gtfs_import_controller.ex index 0f8eade5..0e915991 100644 --- a/lib/arrow_web/controllers/api/gtfs_import_controller.ex +++ b/lib/arrow_web/controllers/api/gtfs_import_controller.ex @@ -1,6 +1,8 @@ defmodule ArrowWeb.API.GtfsImportController do use ArrowWeb, :controller + use Plug.ErrorHandler + require Logger import Ecto.Query @@ -94,6 +96,21 @@ defmodule ArrowWeb.API.GtfsImportController do check_jobs(conn, %{"status_filter" => "all"}) end + # Since all of this controller's endpoints are expected to be used exclusively + # by developers, in CI workflows, with authentication, we can send specific + # error info back so that it doesn't have to be hunted down separately in + # splunk. (It will still be logged to splunk as well, though.) + @impl Plug.ErrorHandler + def handle_errors(conn, %{reason: error}) when is_exception(error) do + send_resp(conn, conn.status, Exception.message(error)) + end + + def handle_errors(conn, error_info) do + # A throw or an exit--unlikely to happen. + details = Exception.format(error_info.kind, error_info.reason, error_info.stack) + send_resp(conn, conn.status, "Arrow encountered a problem:\n#{details}") + end + @spec to_resp({:ok, term} | error_tuple, Plug.Conn.t()) :: Plug.Conn.t() defp to_resp(result, conn) do case result do