Skip to content

Return an exception struct from put_locale_from_session/2 for an empty session - #20

Merged
kipcole9 merged 2 commits into
elixir-localize:mainfrom
rubas:fix/session-error-shape
Sep 27, 2026
Merged

kipcole9 merged 2 commits into
elixir-localize:mainfrom
rubas:fix/session-error-shape

Conversation

@rubas

@rubas rubas commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

Before

iex> Localize.Plug.put_locale_from_session(%{})
{:error, {Localize.UnknownLocaleError, "No locale was found in the session"}}

iex> Localize.Plug.put_locale_from_session(%{"localize_locale" => "!!!"})
{:error, %Localize.InvalidLocaleError{locale_id: "!!!"}}

Problem

  • The spec says {:error, Exception.t()}, and the 1.1.0 changelog says this function now reports errors as exception structs. The empty-session branch was missed.
  • A caller that handles the error with Exception.message/1 or a %{__exception__: true} match fails on this one branch.
  • The ### Returns doc still describes {:error, {exception, reason}}.

After

iex> Localize.Plug.put_locale_from_session(%{})
{:error, %Localize.Plug.NoSessionLocaleError{message: "No locale was found in the session"}}
Details

Cause: the fallback clause of Localize.Plug.put_locale_from_session/2 in lib/localize/plug.ex.

Design: a small Localize.Plug.NoSessionLocaleError, defined in Localize.Plug, keeps the existing message. Localize.UnknownLocaleError, which the tuple named, has only a :locale_id field and would print "The locale nil is not known."; no exception in localize takes a free message. The ### Returns doc now says {:error, exception} and names this case, and the module is listed in the Plugs docs group.

Tests: the existing empty-session test in test/localize_web_test.exs now matches %Localize.Plug.NoSessionLocaleError{} and checks its message; it fails on main.

Checks: mix test (267 passed), mix format --check-formatted, mix compile --warnings-as-errors. mix credo --strict reports only the existing Sigil_q.InterpolationTest module-name issue.


AI disclosure: AI models helped us find this issue, write the change, and review it.

…y session

The spec and the 1.1.0 changelog say {:error, Exception.t()}, and an
invalid session locale already returns a struct, but a session without
a locale still returned a {module, message} tuple.
UnknownLocaleError only carries a locale id, so its message for an
empty session was "The locale nil is not known.".
@kipcole9
kipcole9 merged commit 89ac044 into elixir-localize:main Sep 27, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants