From 0d83dc4a45ebecedc7d8e9645da7744c8d655f30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Tue, 29 Aug 2023 12:24:42 +0200 Subject: [PATCH 1/2] Add Kino.start_child!/1 --- lib/kino.ex | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/kino.ex b/lib/kino.ex index 0b2ae7ef..4fd4eea9 100644 --- a/lib/kino.ex +++ b/lib/kino.ex @@ -445,6 +445,18 @@ defmodule Kino do DynamicSupervisor.start_child(Kino.DynamicSupervisor, child_spec) end + @doc """ + Similar to `start_child/2` but returns the new pid or raises an error. + """ + @spec start_child!(Supervisor.child_spec() | {module(), term()} | module()) :: pid() + def start_child!(child_spec) do + case start_child(child_spec) do + {:ok, pid} -> pid + {:ok, pid, _info} -> pid + other -> raise "failed to start child, reason: #{Kernel.inspect(other)}" + end + end + @doc """ Terminates a child started with `start_child/1`. From 7f05819d850080b3cc02882ff76832fbc490952e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Tue, 29 Aug 2023 13:53:36 +0200 Subject: [PATCH 2/2] Up --- lib/kino.ex | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/kino.ex b/lib/kino.ex index 4fd4eea9..c45120b7 100644 --- a/lib/kino.ex +++ b/lib/kino.ex @@ -451,12 +451,26 @@ defmodule Kino do @spec start_child!(Supervisor.child_spec() | {module(), term()} | module()) :: pid() def start_child!(child_spec) do case start_child(child_spec) do - {:ok, pid} -> pid - {:ok, pid, _info} -> pid - other -> raise "failed to start child, reason: #{Kernel.inspect(other)}" + {:ok, pid} -> + pid + + {:ok, pid, _info} -> + pid + + {:error, reason} -> + raise "failed to start child with the spec #{Kernel.inspect(child_spec)}.\n" <> + "Reason: #{start_supervised_error(reason)}" end end + defp start_supervised_error({{:EXIT, reason}, info}) when is_tuple(info), + do: Exception.format_exit(reason) + + defp start_supervised_error({reason, info}) when is_tuple(info), + do: Exception.format_exit(reason) + + defp start_supervised_error(reason), do: Exception.format_exit(reason) + @doc """ Terminates a child started with `start_child/1`.