From 453531f95cefacdb69d64b5432daf5ddc7666632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Szuma?= <56085570+Rados13@users.noreply.github.com> Date: Mon, 3 Jun 2024 10:15:42 +0200 Subject: [PATCH] Add logs about errors during adding peer (#191) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add logs about errors during adding peer * Add generic error logger handler * More error logs * Apply TODOs * Fix formating * Update peer_controller * Changes after review * Update lib/fishjam_web/controllers/peer_controller.ex Co-authored-by: Michał Śledź * Update log messages * Update lib/fishjam/room_service.ex Co-authored-by: Michał Śledź * Move Logger.debug --------- Co-authored-by: Michał Śledź --- lib/fishjam/room_service.ex | 9 ++++++ .../controllers/fallback_controller.ex | 4 +++ .../controllers/peer_controller.ex | 28 +++++++++++++++---- .../controllers/room_controller.ex | 7 +++++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/lib/fishjam/room_service.ex b/lib/fishjam/room_service.ex index b1ad68b0..c4389db5 100644 --- a/lib/fishjam/room_service.ex +++ b/lib/fishjam/room_service.ex @@ -118,7 +118,10 @@ defmodule Fishjam.RoomService do @impl true def handle_call({:create_room, config}, _from, state) do + Logger.debug("Creating a new room") + with {:ok, room_pid, room_id} <- Room.start(config) do + Logger.debug("Room created successfully") room = Room.get_state(room_id) Process.monitor(room_pid) @@ -133,7 +136,13 @@ defmodule Fishjam.RoomService do {:reply, {:ok, room, Fishjam.address()}, state} else {:error, :room_already_exists} = error -> + Logger.warning("Room creation failed, because it already exists") + {:reply, error, state} + + reason -> + Logger.warning("Room creation failed with reason: #{inspect(reason)}") + {:reply, {:error, :room_doesnt_start}, state} end end diff --git a/lib/fishjam_web/controllers/fallback_controller.ex b/lib/fishjam_web/controllers/fallback_controller.ex index 00f424eb..0388e4dc 100644 --- a/lib/fishjam_web/controllers/fallback_controller.ex +++ b/lib/fishjam_web/controllers/fallback_controller.ex @@ -1,7 +1,11 @@ defmodule FishjamWeb.FallbackController do use FishjamWeb, :controller + require Logger + def call(conn, {:error, status, reason}) do + Logger.debug("Generic error handler status: #{status}, reason: #{reason}") + conn |> put_resp_content_type("application/json") |> put_status(status) diff --git a/lib/fishjam_web/controllers/peer_controller.ex b/lib/fishjam_web/controllers/peer_controller.ex index 39baa587..9b26d2b0 100644 --- a/lib/fishjam_web/controllers/peer_controller.ex +++ b/lib/fishjam_web/controllers/peer_controller.ex @@ -2,6 +2,7 @@ defmodule FishjamWeb.PeerController do use FishjamWeb, :controller use OpenApiSpex.ControllerSpecs + require Logger alias Fishjam.Peer alias Fishjam.Room alias Fishjam.RoomService @@ -74,6 +75,8 @@ defmodule FishjamWeb.PeerController do {:ok, peer_type} <- Peer.parse_type(peer_type_string), {:ok, _room_pid} <- RoomService.find_room(room_id), {:ok, peer} <- Room.add_peer(room_id, peer_type, peer_options) do + Logger.debug("Successfully added peer to room: #{room_id}") + assigns = [ peer: peer, token: PeerToken.generate(%{peer_id: peer.id, room_id: room_id}), @@ -86,19 +89,30 @@ defmodule FishjamWeb.PeerController do |> render("show.json", assigns) else :error -> - {:error, :bad_request, "Invalid request body structure"} + msg = "Invalid request body structure" + log_warning(room_id, msg) + + {:error, :bad_request, msg} {:error, :room_not_found} -> - {:error, :not_found, "Room #{room_id} does not exist"} + msg = "Room #{room_id} does not exist" + log_warning(room_id, msg) + {:error, :not_found, msg} {:error, :invalid_type} -> - {:error, :bad_request, "Invalid peer type"} + msg = "Invalid peer type" + log_warning(room_id, msg) + {:error, :bad_request, msg} {:error, {:peer_disabled_globally, type}} -> - {:error, :bad_request, "Peers of type #{type} are disabled on this Fishjam"} + msg = "Peers of type #{type} are disabled on this Fishjam" + log_warning(room_id, msg) + {:error, :bad_request, msg} {:error, {:reached_peers_limit, type}} -> - {:error, :service_unavailable, "Reached #{type} peers limit in room #{room_id}"} + msg = "Reached #{type} peers limit in room #{room_id}" + log_warning(room_id, msg) + {:error, :service_unavailable, msg} end end @@ -111,4 +125,8 @@ defmodule FishjamWeb.PeerController do {:error, :peer_not_found} -> {:error, :not_found, "Peer #{id} does not exist"} end end + + defp log_warning(room_id, msg) do + Logger.warning("Unable to add peer to room #{room_id}, reason: #{msg}") + end end diff --git a/lib/fishjam_web/controllers/room_controller.ex b/lib/fishjam_web/controllers/room_controller.ex index a28130f6..62db8b78 100644 --- a/lib/fishjam_web/controllers/room_controller.ex +++ b/lib/fishjam_web/controllers/room_controller.ex @@ -2,6 +2,7 @@ defmodule FishjamWeb.RoomController do use FishjamWeb, :controller use OpenApiSpex.ControllerSpecs + require Logger alias Fishjam.Room alias Fishjam.RoomService alias FishjamWeb.ApiSpec @@ -74,6 +75,8 @@ defmodule FishjamWeb.RoomController do end def create(conn, params) do + Logger.debug("Start creating room") + with {:ok, config} <- Room.Config.from_params(params), {:ok, room, fishjam_address} <- RoomService.create_room(config) do conn @@ -104,6 +107,10 @@ defmodule FishjamWeb.RoomController do room_id = Map.get(params, "roomId") {:error, :bad_request, "Cannot add room with id \"#{room_id}\" - room already exists"} + {:error, :room_doesnt_start} -> + room_id = Map.get(params, "roomId") + {:error, :bad_request, "Cannot add room with id \"#{room_id}\" - unexpected error"} + {:error, :invalid_room_id} -> room_id = Map.get(params, "roomId")