Go · lesson 10 of 15

Errors

Errors.raz
module Errors;
import Out, Str;

const
    Empty*: ERROR = "nothing to read";
    TooBig*: ERROR = "not a number up to 1000";

(* the number s holds, up to 1000 *)
proc number(s: []U8): (INT, ERROR);
var n: I64; ok: BOOL;
begin
    if LEN(s) = 0 then return 0, Empty; end;
    n, ok := Str.toInt(s);
    if not ok or (n > 1000) then return 0, TooBig; end;
    return INT(n), nil;
end;

(* the sum of the numbers in words: the first error ends it *)
proc total(words: [][]U8): (INT, ERROR);
var i, sum: INT;
begin
    for i := 0 to LEN(words) - 1 do
        sum := sum + try number(words[i]);
    end;
    return sum, nil;
end;

proc main*;
var words: [3][]U8; i, n: INT; err: ERROR;
begin
    words := ["42", "", "12345"];
    for i := 0 to 2 do
        n, err := number(words[i]);
        if err = nil then Out.int(n);
        elsif err = Empty then Out.str("empty");
        else Out.str(MSG(err));
        end;
        Out.ln;
    end;
    n, err := total(["1", "2", "3"]);
    Out.int(n); Out.ln;
    n, err := total(words);
    if err # nil then Out.str(MSG(err)); Out.ln; end;
end;

end Errors.
42
empty
not a number up to 1000
6
nothing to read

A procedure that can fail returns an ERROR beside its result, and nil when it didn't. Each ERROR constant is an error of its own, compared with = by which declaration made it, never by its message; MSG(err) is the message. An exported one, as Empty is here, lets the callers tell it from the others. raz has no exceptions, so every failure is visible in the signature and handled where the call is written.

Handling them by kind

Because an error is compared by identity, a caller can handle the cases it knows and pass on the rest.

Parse.raz
module Parse;
import Out, Str;

const
    Empty*: ERROR = "nothing to read";
    Bad*: ERROR = "not a number";
    Range*: ERROR = "out of range";

(* a percentage, 0 to 100 *)
proc percent(s: []U8): (INT, ERROR);
var n: I64; ok: BOOL;
begin
    if LEN(s) = 0 then return 0, Empty; end;
    n, ok := Str.toInt(s);
    if not ok then return 0, Bad; end;
    if (n < 0) or (n > 100) then return 0, Range; end;
    return INT(n), nil;
end;

proc main*;
var inputs: [4][]U8; i, n: INT; err: ERROR;
begin
    inputs := ["42", "", "forty", "140"];
    for i := 0 to 3 do
        n, err := percent(inputs[i]);
        if err = nil then
            Out.int(n); Out.str("%");
        elsif err = Empty then
            Out.str("(skipped)");
        else
            Out.str("error: "); Out.str(MSG(err));
        end;
        Out.ln;
    end;
end;

end Parse.
42%
(skipped)
error: not a number
error: out of range

Passing errors up

When a procedure can't do anything useful with an error, it returns it, so the first caller that can decide gets it. Written out, that is a test and a return after each call:

``raz x, err := value(a); if err # nil then return 0, err; end; ``

try says the same in a word. x := try value(a) calls value and, if the error it returned isn't nil, returns that error from the procedure it stands in, with zero for that procedure's other results; otherwise it is value's number. There is no hidden unwinding and no handler: try is a return, written where the call is, and a procedure that uses it must end in an ERROR itself, so its signature says it can fail. main returns none, so it tests its errors and deals with them.

Layers.raz
module Layers;
import Out, Str;

const Missing*: ERROR = "key missing";

(* the number after "n=" in a line like "n=17" *)
proc value(line: []U8): (INT, ERROR);
var n: I64; ok: BOOL;
begin
    if not Str.hasPrefix(line, "n=") then return 0, Missing; end;
    n, ok := Str.toInt(line[2..LEN(line)]);
    if not ok then return 0, Missing; end;
    return INT(n), nil;
end;

(* the sum of the values of two lines: the first error ends it *)
proc sum(a, b: []U8): (INT, ERROR);
begin
    return try value(a) + try value(b), nil;
end;

proc main*;
var n: INT; err: ERROR;
begin
    n, err := sum("n=40", "n=2");
    if err = nil then Out.int(n); Out.ln; end;
    n, err := sum("n=40", "x=2");
    if err # nil then Out.str("sum failed: "); Out.str(MSG(err)); Out.ln; end;
end;

end Layers.
42
sum failed: key missing

When the library reports errors

Library procedures that can fail say so in the same way: Thread.spawn returns an ERROR; Sys calls return a negative errno as an INT, with 0 or a positive count for success; Files.read returns a BOOL. The first is the language's way; the others are the system's, left as they are.

Probe.raz
module Probe;
import Out, Sys;

proc main*;
var fd: INT;
begin
    fd := Sys.openRead("/no/such/file");
    if fd < 0 then
        Out.str("open failed, errno "); Out.int(-fd); Out.ln;     (* 2: ENOENT *)
    end;
    fd := Sys.openRead("/dev/null");
    if fd >= 0 then
        Out.str("open worked\n");
        Sys.close(fd);
    end;
end;

end Probe.
open failed, errno 2
open worked

When there is nothing to do but stop

A program that has found a state it can't carry on from, such as a broken invariant, says so and ends with Rt.stop: the message goes to standard error and the status is 2, the same as a failed check.

Giveup.raz
module Giveup;
import Rt;

proc main*;
begin
    Rt.stop("the table is corrupt");
end;

end Giveup.
$ raz run Giveup.raz; echo "status $?"
the table is corrupt
status 2

Save a program under its module’s name and run it with raz run Name.raz.