Skip to content

Commit 786324a

Browse files
committed
Add Cldr.InvalidCurrencyError exception
1 parent 0e2dbd6 commit 786324a

5 files changed

Lines changed: 56 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
**Note that `ex_cldr` version 2.39.0 and later are supported on Elixir 1.12 and later only.**
44

5+
## Cldr v2.46.0
6+
7+
This is the changelog for Cldr v2.46.0 released on January 20th, 2026. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr/tags)
8+
9+
### Enhancements
10+
11+
* Adds an exception `Cldr.InvalidCurrencyCode` to allow differentiation between invalid and unknown currency codes. This is primarily to support between currency validation in [ex_money](https://github.com/kipcole9/money).
12+
513
## Cldr v2.45.2
614

715
This is the changelog for Cldr v2.45.2 released on January 19th, 2026. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr/tags)

lib/cldr.ex

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,11 +2009,14 @@ defmodule Cldr do
20092009
iex> Cldr.validate_currency("xtc")
20102010
{:ok, :XTC}
20112011
2012+
iex> Cldr.validate_currency("ZZZ")
2013+
{:error, {Cldr.UnknownCurrencyError, "The currency \\"ZZZ\\" is unknown"}}
2014+
20122015
iex> Cldr.validate_currency("invalid")
2013-
{:error, {Cldr.UnknownCurrencyError, "The currency \\"invalid\\" is invalid"}}
2016+
{:error, {Cldr.InvalidCurrencyError, "The currency \\"invalid\\" is invalid"}}
20142017
20152018
iex> Cldr.validate_currency(:invalid)
2016-
{:error, {Cldr.UnknownCurrencyError, "The currency :invalid is invalid"}}
2019+
{:error, {Cldr.InvalidCurrencyError, "The currency :invalid is invalid"}}
20172020
20182021
"""
20192022

@@ -2024,10 +2027,11 @@ defmodule Cldr do
20242027
def validate_currency(currency) when is_atom(currency) do
20252028
currency
20262029
|> Atom.to_string()
2027-
|> validate_currency
2030+
|> validate_currency()
20282031
|> case do
2029-
{:error, _} -> {:error, unknown_currency_error(currency)}
2030-
ok -> ok
2032+
{:error, {Cldr.UnknownCurrencyError, _}} -> {:error, unknown_currency_error(currency)}
2033+
{:error, {Cldr.InvalidCurrencyError, _}} -> {:error, invalid_currency_error(currency)}
2034+
{:ok, currency} -> {:ok, currency}
20312035
end
20322036
end
20332037

@@ -2060,11 +2064,11 @@ defmodule Cldr do
20602064
end
20612065

20622066
def validate_currency(invalid_currency) do
2063-
{:error, unknown_currency_error(invalid_currency)}
2067+
{:error, invalid_currency_error(invalid_currency)}
20642068
end
20652069

20662070
@doc """
2067-
Returns an error tuple for an invalid currency.
2071+
Returns an error tuple for an unknown currency.
20682072
20692073
## Arguments
20702074
@@ -2076,12 +2080,33 @@ defmodule Cldr do
20762080
20772081
## Examples
20782082
2079-
iex> Cldr.unknown_currency_error("invalid")
2080-
{Cldr.UnknownCurrencyError, "The currency \\"invalid\\" is invalid"}
2083+
iex> Cldr.unknown_currency_error("XAB")
2084+
{Cldr.UnknownCurrencyError, "The currency \\"XAB\\" is unknown"}
20812085
20822086
"""
20832087
def unknown_currency_error(currency) do
2084-
{Cldr.UnknownCurrencyError, "The currency #{inspect(currency)} is invalid"}
2088+
{Cldr.UnknownCurrencyError, "The currency #{inspect(currency)} is unknown"}
2089+
end
2090+
2091+
@doc """
2092+
Returns an error tuple for an invalid currency.
2093+
2094+
## Arguments
2095+
2096+
* `currency` is any currency code **not** returned by `Cldr.known_currencies/0`
2097+
2098+
## Returns
2099+
2100+
* `{:error, {Cldr.InvalidCurrencyError, message}}`
2101+
2102+
## Examples
2103+
2104+
iex> Cldr.invalid_currency_error("invalid")
2105+
{Cldr.InvalidCurrencyError, "The currency \\"invalid\\" is invalid"}
2106+
2107+
"""
2108+
def invalid_currency_error(currency) do
2109+
{Cldr.InvalidCurrencyError, "The currency #{inspect(currency)} is invalid"}
20852110
end
20862111

20872112
@doc """

lib/cldr/exception.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ defmodule Cldr.FormatCompileError do
104104
end
105105

106106
defmodule Cldr.UnknownCurrencyError do
107+
@moduledoc """
108+
Exception raised when there is an unknown currency code.
109+
"""
110+
defexception [:message]
111+
112+
def exception(message) do
113+
%__MODULE__{message: message}
114+
end
115+
end
116+
117+
defmodule Cldr.InvalidCurrencyError do
107118
@moduledoc """
108119
Exception raised when there is an invalid currency code.
109120
"""

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Cldr.Mixfile do
22
use Mix.Project
33

4-
@version "2.45.2"
4+
@version "2.46.0"
55

66
def project do
77
[

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"dialyxir": {:hex, :dialyxir, "1.4.7", "dda948fcee52962e4b6c5b4b16b2d8fa7d50d8645bbae8b8685c3f9ecb7f5f4d", [:mix], [{:erlex, ">= 0.2.8", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "b34527202e6eb8cee198efec110996c25c5898f43a4094df157f8d28f27d9efe"},
77
"earmark_parser": {:hex, :earmark_parser, "1.4.44", "f20830dd6b5c77afe2b063777ddbbff09f9759396500cdbe7523efd58d7a339c", [:mix], [], "hexpm", "4778ac752b4701a5599215f7030989c989ffdc4f6df457c5f36938cc2d2a2750"},
88
"erlex": {:hex, :erlex, "0.2.8", "cd8116f20f3c0afe376d1e8d1f0ae2452337729f68be016ea544a72f767d9c12", [:mix], [], "hexpm", "9d66ff9fedf69e49dc3fd12831e12a8a37b76f8651dd21cd45fcf5561a8a7590"},
9-
"ex_doc": {:hex, :ex_doc, "0.39.3", "519c6bc7e84a2918b737aec7ef48b96aa4698342927d080437f61395d361dcee", [:mix], [{:earmark_parser, "~> 1.4.44", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "0590955cf7ad3b625780ee1c1ea627c28a78948c6c0a9b0322bd976a079996e1"},
9+
"ex_doc": {:hex, :ex_doc, "0.40.0", "2635974389b80fd3ca61b0f993d459dad05b4a8f9b069dcfbbc5f6a8a6aef60e", [:mix], [{:earmark_parser, "~> 1.4.44", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "c040735250e2752b6e1102eeb4aa3f1dca74c316db873ae09f955d42136e7e5b"},
1010
"expo": {:hex, :expo, "1.1.1", "4202e1d2ca6e2b3b63e02f69cfe0a404f77702b041d02b58597c00992b601db5", [:mix], [], "hexpm", "5fb308b9cb359ae200b7e23d37c76978673aa1b06e2b3075d814ce12c5811640"},
1111
"gettext": {:hex, :gettext, "1.0.2", "5457e1fd3f4abe47b0e13ff85086aabae760497a3497909b8473e0acee57673b", [:mix], [{:expo, "~> 0.5.1 or ~> 1.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "eab805501886802071ad290714515c8c4a17196ea76e5afc9d06ca85fb1bfeb3"},
1212
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},

0 commit comments

Comments
 (0)