Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logs about errors during adding peer #191

Merged
merged 11 commits into from
Jun 3, 2024
9 changes: 9 additions & 0 deletions lib/fishjam/room_service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/fishjam_web/controllers/fallback_controller.ex
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
28 changes: 23 additions & 5 deletions lib/fishjam_web/controllers/peer_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}),
Expand All @@ -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

Expand All @@ -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
7 changes: 7 additions & 0 deletions lib/fishjam_web/controllers/room_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -74,6 +75,8 @@ defmodule FishjamWeb.RoomController do
end

def create(conn, params) do
Logger.debug("Start creating room")
mickel8 marked this conversation as resolved.
Show resolved Hide resolved

with {:ok, config} <- Room.Config.from_params(params),
{:ok, room, fishjam_address} <- RoomService.create_room(config) do
conn
Expand Down Expand Up @@ -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")

Expand Down