Skip to content

Commit 609ca8e

Browse files
committed
Throw an error when no cldr provided
1 parent 61ea491 commit 609ca8e

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

lib/value_formatters.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,15 @@ defmodule ValueFormatters do
112112
end
113113
end
114114

115-
# FIXME: Nicer error
116-
defp cldr(opts, mod_name), do: Keyword.fetch!(opts, :cldr) |> Module.concat(mod_name)
115+
defp cldr(opts, mod_name) do
116+
with {:ok, cldr} <- Keyword.fetch(opts, :cldr) do
117+
Module.concat(cldr, mod_name)
118+
else
119+
:error ->
120+
raise ArgumentError,
121+
message: "The :cldr option is required."
122+
end
123+
end
117124

118125
defp format_number(value, number_definition, opts) when is_number(value) do
119126
precision = Map.get(number_definition, "precision")

test/cldr.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# For test purposes only
22
defmodule ValueFormatters.Cldr do
33
use Cldr,
4-
# Available locales are defined in the config files s.t. they can vary by environment.
54
otp_app: :value_formatters,
65
default_locale: "en",
76
data_dir: "./priv/cldr",

test/value_formatters_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,4 +621,12 @@ defmodule ValueFormattersTest do
621621
) == {:ok, "123.0 <span class=\"text-gray-500\">kg</span>"}
622622
end
623623
end
624+
625+
describe "missing cldr" do
626+
test "returns error when cldr is not available" do
627+
assert_raise ArgumentError, "The :cldr option is required.", fn ->
628+
ValueFormatters.to_string(1.234, %{"format" => "number"}, [])
629+
end
630+
end
631+
end
624632
end

0 commit comments

Comments
 (0)