Skip to content

Commit

Permalink
Add Kino.start_child!/1 (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko authored Aug 29, 2023
1 parent 8688d80 commit 4c72a93
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/kino.ex
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,32 @@ 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

{: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`.
Expand Down

0 comments on commit 4c72a93

Please sign in to comment.