Soundcheck.Check.Warning.CrashingMatchInCallback (soundcheck v0.1.0)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

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

Explanation

Flags tagged-tuple matches on a call's result, such as {:ok, post} = Blog.update_post(...), in LiveView and LiveComponent callbacks. When the call returns anything else, the match raises, the LiveView process crashes, and the browser remounts it: the user sees a flash of reloading and loses unsaved state, such as form input.

Before:

def handle_event("save", %{"post" => params}, socket) do
  {:ok, post} = Blog.update_post(socket.assigns.post, params)
  {:noreply, assign(socket, :post, post)}
end

After:

def handle_event("save", %{"post" => params}, socket) do
  case Blog.update_post(socket.assigns.post, params) do
    {:ok, post} -> {:noreply, assign(socket, :post, post)}
    {:error, changeset} -> {:noreply, assign(socket, :form, to_form(changeset))}
  end
end

The check looks at mount/3, handle_params/3, handle_event/3, handle_info/2, handle_async/3, and update/2, and at matches whose left side is a tuple tagged with an atom ({:ok, _}, {:error, _}, {:ok, _, _}) and whose right side is a function call or a pipe ending in one. Matches inside anonymous functions are skipped: they run elsewhere, such as in start_async/3. Helpers the callbacks call are not followed. A match that should crash, because failure means a bug, is fine; silence it with a soundcheck:disable-next-line SC603 comment that says so.

Confidence: medium. Tier: syntactic (works without compiling).

Check-Specific Parameters

Use the following parameters to configure this check:

:excluded_paths

Paths to skip (atoms, binaries as path segments, or regexes).

This parameter defaults to [~r/\/test\//, "test"].

:strict

Unused.

This parameter defaults to false.

General Parameters

Like with all checks, general params can be applied.

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