Avoiding long `with` blocks

One way to approach this is to avoid the else clause in with. This means that you need to introduce a somewhat generic error struct - it can be app-wide, or specific to some context. All the smaller functions will need to return the error struct on error or you’ll need to write local wrappers around them. So it would be something like this:

# Generic error.
defmodule MyApp.Error do
  defexception [:code, :message, :meta]

  @impl Exception
  def message(%__MODULE__{code: code, message: message, meta: meta}) do
    "Error (#{code}): #{message}, meta: #{inspect(meta)}"
  end

  def new(code, message, opts \\ []) do
    %__MODULE__{
      code: code,
      message: message,
      meta: opts[:meta] || %{}
    }
  end

  def auth_error() do
    new(:auth_error, "authorization error")
  end

  def validation_error(changeset) do
    new(:validation_error, "validation error", meta: changeset)
  end
end

# App logic.
defmodule MyApp.Foo do
  def bar(input) do
    with {:ok, result_a} <- function_a(input),
         {:ok, result_b} <- function_b(result_a),
         {:ok, result_c} <- function_c(result_b),
         :ok <- some_validation_function_d(result_c) do
      {:ok, result_c}
    end
  end

  # Example of wrapping.
  defp some_validation_function_d(result_c) do
    case run_validation(result_c) do
      :ok -> :ok
      {:error, changeset} -> {:error, MyApp.Error.validation_error(changeset)}
    end
  end
end

# Top-level code that runs the logic (e.g. controller, background job, etc.)
defmodule MyAppWeb.FooController do
  def create(conn, _params) do
    case MyApp.Foo.bar(params) do
      {:ok, result} -> #...
      {:error, %MyApp.Error{code: :validation_error}} -> # ...
    end
  end
end

See also Good and Bad Elixir.

As an added bonus, all the functions that return {:error, %MyApp.Error{}} now became nicely composable.