Skip to content

Commit

Permalink
Refactor error log
Browse files Browse the repository at this point in the history
  • Loading branch information
FelonEkonom committed May 12, 2023
1 parent 0b541e4 commit 8f566f3
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions lib/membrane/core/parent/child_life_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -500,33 +500,40 @@ defmodule Membrane.Core.Parent.ChildLifeController do
def handle_remove_children(children_or_children_groups, state) do
children_or_children_groups = Bunch.listify(children_or_children_groups)

all_children_and_children_groups =
refs =
state.children
|> Enum.flat_map(fn {ref, %{group: group}} -> [ref, group] end)
|> Enum.filter(fn {child_ref, child_entry} ->
child_ref in children_or_children_groups or
child_entry.group in children_or_children_groups
end)
|> Enum.map(fn {ref, _child_entry} -> ref end)

removed_children_and_children_groups =
Enum.map(refs, &ChildrenModel.get_child_data!(state, &1))
|> Enum.flat_map(&[&1.name, &1.group])
|> Enum.uniq()
|> List.delete(nil)

children_or_children_groups
|> Enum.find(&(&1 not in all_children_and_children_groups))
|> Enum.find(&(&1 not in removed_children_and_children_groups))
|> case do
nil ->
:ok

child_ref ->
children_groups =
Map.values(state.children)
|> Enum.map(& &1.group)
|> Enum.uniq()
|> List.delete(nil)

raise Membrane.ParentError, """
Trying to remove child #{inspect(child_ref)}, while such a child or children group does not exist.
Existing children and children groups are: #{inspect(all_children_and_children_groups, pretty: true)}
Existing children are: #{inspect(Map.keys(state.children), pretty: true)}
Existing children groups are: #{inspect(children_groups, pretty: true)}
"""
end

refs =
state.children
|> Enum.filter(fn {child_ref, child_entry} ->
child_ref in children_or_children_groups or
child_entry.group in children_or_children_groups
end)
|> Enum.map(fn {ref, _child_entry} -> ref end)

Membrane.Logger.debug("Removing children: #{inspect(refs)}")

state =
Expand Down

0 comments on commit 8f566f3

Please sign in to comment.