raz / site / content / tour / 10-errors.txt

142 lines · 4.1 KiB · raw · history · last change a5145f64

1title: Errors
2part: Go
3summary: A second result, compared by identity; passing errors up; and no exceptions.
4---
5```raz Errors run
6```
7
8A 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.
9
10## Handling them by kind
11
12Because an error is compared by identity, a caller can handle the cases it knows and pass on the rest.
13
14```raz Parse run
15module Parse;
16import Out, Str;
17
18const
19 Empty*: ERROR = "nothing to read";
20 Bad*: ERROR = "not a number";
21 Range*: ERROR = "out of range";
22
23(* a percentage, 0 to 100 *)
24proc percent(s: []const U8): (INT, ERROR);
25var n: I64; ok: BOOL;
26begin
27 if LEN(s) = 0 then return 0, Empty; end;
28 n, ok := Str.toInt(s);
29 if not ok then return 0, Bad; end;
30 if (n < 0) or (n > 100) then return 0, Range; end;
31 return INT(n), nil;
32end;
33
34proc main*;
35var inputs: [4][]const U8; i, n: INT; err: ERROR;
36begin
37 inputs := ["42", "", "forty", "140"];
38 for i := 0 to 3 do
39 n, err := percent(inputs[i]);
40 if err = nil then
41 Out.int(n); Out.str("%");
42 elsif err = Empty then
43 Out.str("(skipped)");
44 else
45 Out.str("error: "); Out.str(MSG(err));
46 end;
47 Out.ln;
48 end;
49end;
50
51end Parse.
52```
53
54## Passing errors up
55
56When 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:
57
58```raz
59x, err := value(a);
60if err # nil then return 0, err; end;
61```
62
63`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.
64
65```raz Layers run
66module Layers;
67import Out, Str;
68
69const Missing*: ERROR = "key missing";
70
71(* the number after "n=" in a line like "n=17" *)
72proc value(line: []const U8): (INT, ERROR);
73var n: I64; ok: BOOL;
74begin
75 if not Str.hasPrefix(line, "n=") then return 0, Missing; end;
76 n, ok := Str.toInt(line[2..LEN(line)]);
77 if not ok then return 0, Missing; end;
78 return INT(n), nil;
79end;
80
81(* the sum of the values of two lines: the first error ends it *)
82proc sum(a, b: []const U8): (INT, ERROR);
83begin
84 return try value(a) + try value(b), nil;
85end;
86
87proc main*;
88var n: INT; err: ERROR;
89begin
90 n, err := sum("n=40", "n=2");
91 if err = nil then Out.int(n); Out.ln; end;
92 n, err := sum("n=40", "x=2");
93 if err # nil then Out.str("sum failed: "); Out.str(MSG(err)); Out.ln; end;
94end;
95
96end Layers.
97```
98
99## When the library reports errors
100
101Library 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.
102
103```raz Probe run
104module Probe;
105import Out, Sys;
106
107proc main*;
108var fd: INT;
109begin
110 fd := Sys.openRead("/no/such/file");
111 if fd < 0 then
112 Out.str("open failed, errno "); Out.int(-fd); Out.ln; (* 2: ENOENT *)
113 end;
114 fd := Sys.openRead("/dev/null");
115 if fd >= 0 then
116 Out.str("open worked\n");
117 Sys.close(fd);
118 end;
119end;
120
121end Probe.
122```
123
124## When there is nothing to do but stop
125
126A 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.
127
128```raz Giveup
129module Giveup;
130import Rt;
131
132proc main*;
133begin
134 Rt.stop("the table is corrupt");
135end;
136
137end Giveup.
138```
139
140```sh
141$ raz run Giveup.raz; echo "status $?"
142```