Common errors

Four reports explain most of the first week of Elixir: a name the compiler never saw, a pattern the value does not fit, a function called with an argument no clause accepts, and a key a map does not have. Each section shows the report, the cause and the fix, and the exceptions lesson shows how to rescue one on purpose.

undefined variable

Elixir has no bare name that was never bound. The compiler stops with the line, the caret under the name and the file:

undefined.exs

IO.puts(missing_variable)

elixir undefined.exs

    error: undefined variable "missing_variable"
    │
  1 │ IO.puts(missing_variable)
    │ ^^^^^^^^^^^^^^^^
    │
    └─ undefined.exs:1:9

** (CompileError) undefined.exs: cannot compile file (errors have been logged)

The cause is a name that is not in scope: a typo, a binding made in another function, or a variable that was renamed. The fix is the binding before the line, or the full function call when a function was meant. The variables lesson shows the scope of a binding.

MatchError

The left side of = is a pattern, and a value that does not fit raises MatchError. The report shows the value it could not match, which is often enough to see the wrong side:

match.exs

{:ok, value} = {:error, :nope} IO.puts(value)

elixir match.exs

** (MatchError) no match of right hand side value:

    {:error, :nope}

    match.exs:1: (file)
    (elixir 1.19.7) lib/code.ex:1613: Code.require_file/2

The pattern wanted an ok tuple and the function answered an error tuple. The fix is a case that handles both shapes, or a match on the shape the function really returns; the pattern matching lesson takes the idea further.

FunctionClauseError

A function with guards accepts only the arguments the guards allow. The call that fits no clause raises FunctionClauseError and lists the arguments:

clause.exs

defmodule Num do def label(n) when n > 0, do: :positive end IO.puts(Num.label(-1))

elixir clause.exs

** (FunctionClauseError) no function clause matching in Num.label/1

    The following arguments were given to Num.label/1:

        # 1
        -1

    clause.exs:2: Num.label/1
    clause.exs:5: (file)

The fix is the clause for the remaining values, usually a second definition without the guard or a catch-all at the end. The guards lesson shows the tests a guard may use, and the clauses lesson shows the order they are tried in.

KeyError

A map read with Map.fetch!/2 or a struct field that is not there raises KeyError. The report shows the map and the key, and the key is the one to add or the one to guard:

key.exs

name = Map.fetch!(%{name: "Ada"}, :age) IO.puts(name)

elixir key.exs

** (KeyError) key :age not found in:

    %{name: "Ada"}

    :erlang.map_get(:age, %{name: "Ada"})
    key.exs:1: (file)

Map.get/3 answers a default instead of raising, and Map.fetch/2 answers :error; the bang version is for the key the program cannot continue without. The maps lesson shows the three calls side by side.

The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container. The four errors are deliberate.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does the compiler report for a name that was never bound?

    Answer

    error: undefined variable "name"

    An unbound name is a compile error, shown with the line and a caret.

  2. Which error does a value that does not fit the left side of = raise?

    Answer

    MatchError

    The match failed, so MatchError names the value it could not match.

  3. Which error does a call that no function clause accepts raise?

    Answer

    FunctionClauseError

    The function exists but no clause matches the arguments.

  4. Which error does Map.fetch!(%{a: 1}, :b) raise?

    Answer

    KeyError

    The key is not in the map, and the bang version raises KeyError.

  5. What does this program print? {:ok, value} = {:ok, 42} IO.inspect(value)

    Answer

    42

    The pattern fits the ok tuple and binds value to 42.

  6. What does this program do? {:ok, value} = {:error, :nope}

    Answer

    A MatchError, because the pattern wants an ok tuple

    The shape is an error tuple, so the ok pattern cannot match.

  7. What does this program do? defmodule N do def label(n) when n > 0, do: :positive end IO.puts(N.label(0))

    Answer

    A FunctionClauseError, because the guard rejects 0

    The guard rejects 0, and no other clause accepts it.

  8. Which call reads a map key and returns a default instead of raising?

    Answer

    Map.get/3

    Map.get/3 returns the third argument when the key is absent.

  9. What does Map.fetch/2 answer for a missing key?

    Answer

    :error

    fetch/2 uses an ok tuple or :error, without raising.

  10. Why does a MatchError report the right side?

    Answer

    Because the value is what the pattern could not fit

    The value that failed the match is the useful part of the report.

Article author: Arthur Isaev