← Back to lesson 18: Return success or failure as data

Return success or failure as data: full examples and further reading

The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.

Use tagged tuples so callers can handle every expected result.

Write one function that returns {ok, Value} or {error, Reason} without throwing.

Run this first

Run it once, change one input, and compare the new result.

Original full example
%% Make both outcomes easy to match
ParseAge = fun(Text) ->
  case string:to_integer(Text) of
    {Age, []} when Age >= 0 -> {ok, Age};
    _ -> {error, invalid_age}
  end
end,
{ParseAge("12"), ParseAge("old")}.

The result is {{ok,12},{error,invalid_age}}.

Steps for the full example

  1. Read {ok, Value}: Return a successful value.
  2. Read {error, Reason}: Return an expected failure reason.
  3. Read case ... of: Handle every result tag explicitly.

Original lesson notes

Run the example first. Predict one result, then change one input and run it again.

A tuple beginning with ok or error makes the outcome visible to callers.

A total function returns a documented result for every allowed input shape.

Symbols and terms
{ok, Value}
Return a successful value.
{error, Reason}
Return an expected failure reason.
case ... of
Handle every result tag explicitly.
tagged result
A tuple beginning with ok or error makes the outcome visible to callers.
total function
A total function returns a documented result for every allowed input shape.
exception boundary
Reserve exceptions for unexpected failures; return ordinary validation errors as data.

Original full exercise

Reject ages above 120.

Exercise starter
{Age, []} when Age >= 0, Age =< ____ -> {ok, Age};

Use 120 as the upper boundary.

Show hint

Add the number after =<.

Show original answer
Reference answer
{Age, []} when Age >= 0, Age =< 120 -> {ok, Age};
Should invalid user input crash the caller?

Usually no. Return a tagged error that the caller can display or recover from.

Original lesson takeaways
Design trade-offs and language comparisons
DESIGN DECISION

Why this shape?

Why return {:ok, value} or {ok, Value} so often?

Choice

Expected failure stays visible as ordinary data. A caller can list success, known failure, and fallback branches with patterns.

Cost and boundary

Unexpected failures that cannot be handled here may still raise and reach a supervision boundary. Turning every exception into a generic error discards evidence.

A FAMILIAR POINT OF VIEW

Coming from Java, Python, or JavaScript

Java

Familiar starting point
Checked or unchecked exceptions, plus Optional.
What BEAM changes
Tagged results keep expected business outcomes in normal control flow.
False friend
Not every error should crash, and not every exception should be caught here.

Python

Familiar starting point
Exceptions and None.
What BEAM changes
A tag names the failure class instead of asking one empty value to carry every meaning.
False friend
Translate third-party exceptions into a stable result at a boundary.

JavaScript

Familiar starting point
throw, rejected Promises, or null.
What BEAM changes
A tuple is not a Promise. It is data that has already arrived.
False friend
Read contract as acceptance criteria, not an asynchronous Promise.
Original course references