Skip to content

Commit f082b2d

Browse files
authored
Add MissingDependenciesError exception (#13689)
1 parent 31f5f12 commit f082b2d

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

lib/elixir/lib/exception.ex

+50
Original file line numberDiff line numberDiff line change
@@ -2131,6 +2131,56 @@ defmodule UnicodeConversionError do
21312131
end
21322132
end
21332133

2134+
defmodule MissingApplicationsError do
2135+
@moduledoc """
2136+
An exception that is raised when an application depends on one or more
2137+
missing applications.
2138+
2139+
This exception is used by Mix and other tools. It can also be used by library authors
2140+
when their library only requires an external application (like a dependency) for a subset
2141+
of features.
2142+
2143+
The fields of this exception are public. See `t:t/0`.
2144+
2145+
*Available since v1.18.0.*
2146+
2147+
## Examples
2148+
2149+
unless Application.spec(:plug, :vsn) do
2150+
raise MissingApplicationsError,
2151+
description: "application :plug is required for testing Plug-related functionality",
2152+
apps: [{:plug, "~> 1.0"}]
2153+
end
2154+
2155+
"""
2156+
2157+
@moduledoc since: "1.18.0"
2158+
2159+
@type t() :: %__MODULE__{
2160+
apps: [{Application.app(), Version.requirement()}, ...],
2161+
description: String.t()
2162+
}
2163+
2164+
defexception apps: [], description: "missing applications found"
2165+
2166+
@impl true
2167+
def message(%__MODULE__{apps: apps, description: description}) do
2168+
# We explicitly format these as tuples so that they're easier to copy-paste
2169+
# into dependencies.
2170+
formatted_apps =
2171+
Enum.map(apps, fn {app_name, requirement} ->
2172+
~s(\n {#{inspect(app_name)}, "#{requirement}"})
2173+
end)
2174+
2175+
"""
2176+
#{description}
2177+
2178+
To address this, include these applications as your dependencies:
2179+
#{formatted_apps}\
2180+
"""
2181+
end
2182+
end
2183+
21342184
defmodule Enum.OutOfBoundsError do
21352185
@moduledoc """
21362186
An exception that is raised when a function expects an enumerable to have

lib/elixir/test/elixir/exception_test.exs

+15
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,21 @@ defmodule ExceptionTest do
954954
test "ErlangError" do
955955
assert %ErlangError{original: :sample} |> message == "Erlang error: :sample"
956956
end
957+
958+
test "MissingApplicationsError" do
959+
assert %MissingApplicationsError{
960+
apps: [{:logger, "~> 1.18"}, {:ex_unit, Version.parse_requirement!(">= 0.0.0")}],
961+
description: "applications are required"
962+
}
963+
|> message == """
964+
applications are required
965+
966+
To address this, include these applications as your dependencies:
967+
968+
{:logger, "~> 1.18"}
969+
{:ex_unit, ">= 0.0.0"}\
970+
"""
971+
end
957972
end
958973

959974
describe "error_info" do

0 commit comments

Comments
 (0)