OptimumCredo.Check.Refactor.LiveViewBareMatch (OptimumCredo v0.5.0)

View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of high and works with any version of Elixir.

Explanation

LiveView modules must use case for operations that return {:ok, _} | {:error, _}.

Bare pattern matches crash the LiveView process on failure and hide error handling bugs. Credo's --warnings-as-errors treats unhandled match failures as warnings in some configurations, and the crash surfaces as an unhelpful 500 error.

Examples

Bad:

def handle_event("create", params, socket) do
  {:ok, post} = Posts.create_post(params)
  {:noreply, push_navigate(socket, to: ~p"/posts")}
end

Good:

def handle_event("create", params, socket) do
  case Posts.create_post(params) do
    {:ok, _post} ->
      {:noreply, socket |> put_flash(:info, "Created") |> push_navigate(to: ~p"/posts")}
    {:error, changeset} ->
      {:noreply, assign(socket, :changeset, changeset)}
  end
end

Check-Specific Parameters

There are no specific parameters for this check.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.