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 Kino.start_child!/1 #322

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExUnit does {:start_spec, reason}, but I think it is confusing since it doesn't really have to do with the child spec itself:

** (RuntimeError) failed to start child with the spec Foo.
Reason: bad child specification, got: {:already_started, #PID<0.330.0>}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your call if you want to change it here. :)


@doc """
Terminates a child started with `start_child/1`.

Expand Down
Loading