@@ -591,38 +591,7 @@ defmodule Supervisor do
591
591
e in UndefinedFunctionError ->
592
592
case System . stacktrace do
593
593
[ { ^ module , :child_spec , [ ^ arg ] , _ } | _ ] ->
594
- raise ArgumentError , """
595
- The module #{ inspect module } was given as a child to a supervisor
596
- but it does not implement child_spec/1.
597
-
598
- If you own the given module, please define a child_spec/1 function
599
- that receives an argument and returns a child specification as a map.
600
- For example:
601
-
602
- def child_spec(opts) do
603
- %{
604
- id: __MODULE__,
605
- start: {__MODULE__, :start_link, [opts]},
606
- type: :worker,
607
- restart: :permanent,
608
- shutdown: 500
609
- }
610
- end
611
-
612
- Note that "use Agent", "use GenServer" and so on automatically define
613
- this function for you.
614
-
615
- However, if you don't own the given module and it doesn't implement
616
- child_spec/1, instead of passing the module name directly as a supervisor
617
- child, you will have to pass a child specification as a map:
618
-
619
- %{
620
- id: #{ inspect module } ,
621
- start: {#{ inspect module } , :start_link, [arg1, arg2]}
622
- }
623
-
624
- See the Supervisor documentation for more information.
625
- """
594
+ raise ArgumentError , child_spec_error ( module )
626
595
stack ->
627
596
reraise e , stack
628
597
end
@@ -647,6 +616,45 @@ defmodule Supervisor do
647
616
"""
648
617
end
649
618
619
+ defp child_spec_error ( module ) do
620
+ if Code . ensure_loaded? ( module ) do
621
+ """
622
+ The module #{ inspect module } was given as a child to a supervisor
623
+ but it does not implement child_spec/1.
624
+
625
+ If you own the given module, please define a child_spec/1 function
626
+ that receives an argument and returns a child specification as a map.
627
+ For example:
628
+
629
+ def child_spec(opts) do
630
+ %{
631
+ id: __MODULE__,
632
+ start: {__MODULE__, :start_link, [opts]},
633
+ type: :worker,
634
+ restart: :permanent,
635
+ shutdown: 500
636
+ }
637
+ end
638
+
639
+ Note that "use Agent", "use GenServer" and so on automatically define
640
+ this function for you.
641
+
642
+ However, if you don't own the given module and it doesn't implement
643
+ child_spec/1, instead of passing the module name directly as a supervisor
644
+ child, you will have to pass a child specification as a map:
645
+
646
+ %{
647
+ id: #{ inspect module } ,
648
+ start: {#{ inspect module } , :start_link, [arg1, arg2]}
649
+ }
650
+
651
+ See the Supervisor documentation for more information.
652
+ """
653
+ else
654
+ "The module #{ inspect module } was given as a child to a supervisor but it does not exist."
655
+ end
656
+ end
657
+
650
658
@ doc """
651
659
Builds and overrides a child specification.
652
660
0 commit comments