Errors done right in Jennifer

Jennifer

Kinds, error structs, and the handful of rules that keep a catch honest.

November 2026, by @mplx.
Based on Jennifer 0.27.0

Powered by Jennifer's Cantrip - Markdown into slide decks, https://github.com/jennifer-language/cantrip
1 / 20

An error is a value

use io;

func inner() {
    throw Error{kind: "parse", message: "bad header"};
}

try {
    inner();
} catch (e) {
    io.printf("%s at line %d: %s\n", $e.kind, $e.line, $e.message);
}
parse at line 4: bad header
2 / 20

What a catch receives

channel.recvTimeout($ch, 50);   # nothing arrives
channel.recv($ch);              # after channel.close
$xs[3];                         # a list of one element
timeout | channel.recvTimeout: timed out after 50 ms
closed | channel.recv: receive on a closed channel
runtime | list index 3 out of bounds (len 1)

Not caught: exit, return, break, continue. There is no finally.

3 / 20

Generic kinds: one handler for every library

} catch (e) {
    match ($e.kind) {
        when "limit" { io.printf("input too large\n"); }
        when "timeout", "closed" { io.printf("retry later\n"); }
        when "net" { io.printf("unreachable: %s\n", $e.reason); }
        when "cancelled" { throw $e; }   # we are being stopped
        else { throw $e; }
    }
}

limit a cap was reached, timeout a deadline passed, closed the other side went away, net the network failed, cancelled the task was stopped, value a bad input value (some modules).

4 / 20

net: the retry decision is in the fields

use io;
use net;

try {
    def c as net.Conn init net.connect("127.0.0.1:1");
} catch (e) {
    if ($e.kind == "net") {
        def ne as net.NetError init $e;
        io.printf("%s/%s sent=%t\n", $ne.op, $ne.reason, $ne.sent);
    }
}
connect/refused sent=false
5 / 20

Match the kind, never the message

# wrong: the message is for people, and may be reworded
if (strings.contains($e.message, "HTTP 404")) { ... }

# right: the kind is the contract, the status is a field
if ($e.kind == "rest" and $e.status == 404) { ... }
rest.getJson: HTTP 404: no such user
6 / 20

Errors that carry data: a struct of your own

export def struct StockError {
    kind as string, message as string, sku as string, available as int
};

export func reserve(sku as string, want as int) {
    if ($want > 3) {
        throw StockError{kind: "stock", message: "sold out",
            sku: $sku, available: 3};
    }
    return $want;
}
7 / 20

One kind, one shape

if ($sku == "") {
    throw Error{kind: "stock", message: "no sku"};   # odd one out
}
throw StockError{kind: "stock", message: "sold out", sku: $sku};
# ...
if ($e.kind == "stock") { io.printf("sold out: %s\n", $e.sku); }
mixed.j: runtime error at mixed.j:17:43: struct "Error" has no field "sku"

Every throw of kind "stock" must be a StockError.

8 / 20

The bundled clients do the same

StructKindFields beyond kind and message
rest.HttpErrorreststatus, headers, body
smtp.SmtpErrorsmtpcode, enhancedCode, command, temporary
imap.ImapError, redis.RedisError, s3.S3Error, ...their module'swhat that server reports

A generic kind stays a plain Error: a "limit" from imap is not an ImapError.

9 / 20

The trap: a helper typed Error

func report(e as Error) {
    io.printf("%s: %s\n", $e.kind, $e.message);
}
try {
    rest.getJson($api, "/users/7", {});
} catch (e) {
    report($e);
}
rest404.j: runtime error at rest404.j:25:12: argument 1 to "report" must be Error, got struct
10 / 20

Type the helper with the module's struct

use io;
import "./stock.j" as stock;

func offerRest(e as stock.StockError) {
    io.printf("%s: take the %d left?\n", $e.sku, $e.available);
}

try {
    stock.reserve("tea", 5);
} catch (e) {
    if ($e.kind == "stock") { offerRest($e); } else { throw $e; }
}
tea: take the 3 left?
11 / 20

Still untyped? Ask with def x as Error

} catch (e) {
    try {
        def plain as Error init $e;    # throws for any other struct
        plainOnly($plain);
    } catch (notPlain) {
        io.printf("(%s)\n", $notPlain.message);
        stockOnly($e);
    }
}
(cannot initialize Error variable "plain" with value of type struct)
StockError stock, sku T-42
12 / 20

Rewrapping hides the generic kinds

func loadConfig(text as string) {
    try {
        return json.decode($text);
    } catch (e) {
        throw Error{kind: "config", message: $e.message};
    }
}
loadConfig(strings.replicate("[", 5000));
config | json.decode: nesting exceeds 1000 levels at line 1, column 1001
13 / 20

Rewrap only what is yours

func loadConfig(text as string) {
    try {
        return json.decode($text);
    } catch (e) {
        match ($e.kind) {
            when "limit", "timeout", "closed", "cancelled", "net" {
                throw $e;
            }
        }
        throw Error{kind: "config", message: $e.message};
    }
}
limit | json.decode: nesting exceeds 1000 levels at line 1, column 1001
14 / 20

Cleanup: defer, errdefer, and the lost cause

use io;
func explode() { throw Error{kind: "close", message: "failed"}; }
func risky() {
    defer explode();
    throw Error{kind: "first", message: "the real cause"};
}
try {
    risky();
} catch (e) {
    io.printf("caught %s: %s\n", $e.kind, $e.message);
}
caught close: failed
15 / 20

web.onError sees every error as Error

func buy(ctx as web.Context) {
    def sku as string init web.param($ctx, "sku");
    throw StockError{kind: "stock", message: "sold out", sku: $sku};
}
func logError(err as Error) {
    io.printf("onError: %s: %s\n", $err.kind, $err.message);
}
$app = web.onError($app, logError);
web: unhandled handler error: StockError{kind: "stock", message: "sold out", sku: "T-42"}
onError: stock: sold out
status 500
16 / 20

Lint L104: throw something a catch can read

throw "out of stock";
throw {"kind": "stock", "message": "sold out"};
l104.j:6:11: warning: throw of a non-Error value; throw an `Error` (or a struct
  with `kind` and `message` string fields) so catch handlers can rely on them [L104]

Accepted: an Error, a struct with string kind and message, a rethrown catch binding.

17 / 20

Testing errors

func soldOut() { reserve("tea", 5); }

func testSoldOut() {
    testing.assertThrows("soldOut", "stock");   # matched by kind
    testing.assertThrowsArgs("reserve", ["tea", 5], "stock");
}
FAIL testWrongKind (0 ms)
     [assertion] stock_test.j:10:5 assertThrows: "soldOut" threw kind "stock", expected "rest"

For a field, catch it yourself: testing.assertEqual($left, 3);

18 / 20

Checklist

19 / 20

Read more

Based on Jennifer 0.27.0, November 2026.

20 / 20