raz / site / public / tour / errors.html

217 lines · 15.5 KiB · raw · history · last change de634aad

1<!doctype html>
2<html lang="en">
3<head>
4<meta charset="utf-8">
5<meta name="viewport" content="width=device-width, initial-scale=1">
6<title>Errors - Raz, Set, Go</title>
7<meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux.">
8<meta name="theme-color" content="#E07A5F">
9<meta property="og:title" content="Errors - Raz, Set, Go">
10<meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report.">
11<meta property="og:image" content="https://raz.lucela.org/social.png">
12<meta name="twitter:card" content="summary_large_image">
13<link rel="icon" href="../favicon.svg" type="image/svg+xml">
14<link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32">
15<link rel="apple-touch-icon" href="../apple-touch-icon.png">
16<link rel="stylesheet" href="../style.css">
17</head>
18<body>
19<header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header>
20<main>
21<div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
22<li><a href="hello.html">Hello, world</a></li>
23<li><a href="modules.html">Modules</a></li>
24<li><a href="integers.html">Integers</a></li>
25<li><a href="values.html">Values and conversions</a></li>
26<li><a href="procedures.html">Procedures and several results</a></li>
27</ol>
28<h3>Set</h3><ol start='6'>
29<li><a href="slices.html">Arrays, slices and strings</a></li>
30<li><a href="records.html">Records and pointers</a></li>
31<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
32<li><a href="memory.html">Memory and allocators</a></li>
33</ol>
34<h3>Go</h3><ol start='10'>
35<li><a href="errors.html" aria-current="page">Errors</a></li>
36<li><a href="defer.html">Defer</a></li>
37<li><a href="checks.html">When it goes wrong</a></li>
38<li><a href="generics.html">Generic modules</a></li>
39<li><a href="c.html">Talking to C</a></li>
40<li><a href="threads.html">Threads</a></li>
41</ol></aside><article><p class="kicker">Go &middot; lesson 10 of 15</p><h1>Errors</h1>
42<div class="file"><span>Errors.raz</span></div><pre class="code"><code><span class="kw">module</span> Errors;
43<span class="kw">import</span> Out, Str;
44
45<span class="kw">const</span>
46 Empty*: <span class="ty">ERROR</span> = <span class="string">&quot;nothing to read&quot;</span>;
47 TooBig*: <span class="ty">ERROR</span> = <span class="string">&quot;not a number up to 1000&quot;</span>;
48
49<span class="comment">(* the number s holds, up to 1000 *)</span>
50<span class="kw">proc</span> number(s: []<span class="kw">const</span> <span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
51<span class="kw">var</span> n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>;
52<span class="kw">begin</span>
53 <span class="kw">if</span> <span class="ty">LEN</span>(s) = <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, Empty; <span class="kw">end</span>;
54 n, ok := Str.toInt(s);
55 <span class="kw">if</span> <span class="kw">not</span> ok <span class="kw">or</span> (n &gt; <span class="number">1000</span>) <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, TooBig; <span class="kw">end</span>;
56 <span class="kw">return</span> <span class="ty">INT</span>(n), <span class="ty">nil</span>;
57<span class="kw">end</span>;
58
59<span class="comment">(* the sum of the numbers in words: the first error ends it *)</span>
60<span class="kw">proc</span> total(words: [][]<span class="kw">const</span> <span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
61<span class="kw">var</span> i, sum: <span class="ty">INT</span>;
62<span class="kw">begin</span>
63 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(words) - <span class="number">1</span> <span class="kw">do</span>
64 sum := sum + <span class="kw">try</span> number(words[i]);
65 <span class="kw">end</span>;
66 <span class="kw">return</span> sum, <span class="ty">nil</span>;
67<span class="kw">end</span>;
68
69<span class="kw">proc</span> main*;
70<span class="kw">var</span> words: [<span class="number">3</span>][]<span class="kw">const</span> <span class="ty">U8</span>; i, n: <span class="ty">INT</span>; err: <span class="ty">ERROR</span>;
71<span class="kw">begin</span>
72 words := [<span class="string">&quot;42&quot;</span>, <span class="string">&quot;&quot;</span>, <span class="string">&quot;12345&quot;</span>];
73 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">2</span> <span class="kw">do</span>
74 n, err := number(words[i]);
75 <span class="kw">if</span> err = <span class="ty">nil</span> <span class="kw">then</span> Out.int(n);
76 <span class="kw">elsif</span> err = Empty <span class="kw">then</span> Out.str(<span class="string">&quot;empty&quot;</span>);
77 <span class="kw">else</span> Out.str(<span class="ty">MSG</span>(err));
78 <span class="kw">end</span>;
79 Out.ln;
80 <span class="kw">end</span>;
81 n, err := total([<span class="string">&quot;1&quot;</span>, <span class="string">&quot;2&quot;</span>, <span class="string">&quot;3&quot;</span>]);
82 Out.int(n); Out.ln;
83 n, err := total(words);
84 <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="ty">MSG</span>(err)); Out.ln; <span class="kw">end</span>;
85<span class="kw">end</span>;
86
87<span class="kw">end</span> Errors.</code></pre>
88<pre class="output"><code>42
89empty
90not a number up to 1000
916
92nothing to read
93</code></pre>
94<p>A procedure that can fail returns an <code>ERROR</code> beside its result, and <code>nil</code> when it didn't. Each <code>ERROR</code> constant is an error of its own, compared with <code>=</code> by which declaration made it, never by its message; <code>MSG(err)</code> is the message. An exported one, as <code>Empty</code> 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.</p>
95<h2>Handling them by kind</h2>
96<p>Because an error is compared by identity, a caller can handle the cases it knows and pass on the rest.</p>
97<div class="file"><span>Parse.raz</span></div><pre class="code"><code><span class="kw">module</span> Parse;
98<span class="kw">import</span> Out, Str;
99
100<span class="kw">const</span>
101 Empty*: <span class="ty">ERROR</span> = <span class="string">&quot;nothing to read&quot;</span>;
102 Bad*: <span class="ty">ERROR</span> = <span class="string">&quot;not a number&quot;</span>;
103 Range*: <span class="ty">ERROR</span> = <span class="string">&quot;out of range&quot;</span>;
104
105<span class="comment">(* a percentage, 0 to 100 *)</span>
106<span class="kw">proc</span> percent(s: []<span class="kw">const</span> <span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
107<span class="kw">var</span> n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>;
108<span class="kw">begin</span>
109 <span class="kw">if</span> <span class="ty">LEN</span>(s) = <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, Empty; <span class="kw">end</span>;
110 n, ok := Str.toInt(s);
111 <span class="kw">if</span> <span class="kw">not</span> ok <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, Bad; <span class="kw">end</span>;
112 <span class="kw">if</span> (n &lt; <span class="number">0</span>) <span class="kw">or</span> (n &gt; <span class="number">100</span>) <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, Range; <span class="kw">end</span>;
113 <span class="kw">return</span> <span class="ty">INT</span>(n), <span class="ty">nil</span>;
114<span class="kw">end</span>;
115
116<span class="kw">proc</span> main*;
117<span class="kw">var</span> inputs: [<span class="number">4</span>][]<span class="kw">const</span> <span class="ty">U8</span>; i, n: <span class="ty">INT</span>; err: <span class="ty">ERROR</span>;
118<span class="kw">begin</span>
119 inputs := [<span class="string">&quot;42&quot;</span>, <span class="string">&quot;&quot;</span>, <span class="string">&quot;forty&quot;</span>, <span class="string">&quot;140&quot;</span>];
120 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">3</span> <span class="kw">do</span>
121 n, err := percent(inputs[i]);
122 <span class="kw">if</span> err = <span class="ty">nil</span> <span class="kw">then</span>
123 Out.int(n); Out.str(<span class="string">&quot;%&quot;</span>);
124 <span class="kw">elsif</span> err = Empty <span class="kw">then</span>
125 Out.str(<span class="string">&quot;(skipped)&quot;</span>);
126 <span class="kw">else</span>
127 Out.str(<span class="string">&quot;error: &quot;</span>); Out.str(<span class="ty">MSG</span>(err));
128 <span class="kw">end</span>;
129 Out.ln;
130 <span class="kw">end</span>;
131<span class="kw">end</span>;
132
133<span class="kw">end</span> Parse.</code></pre>
134<pre class="output"><code>42%
135(skipped)
136error: not a number
137error: out of range
138</code></pre>
139<h2>Passing errors up</h2>
140<p>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 <code>return</code> after each call:</p>
141<p>``<code>raz x, err := value(a); if err # nil then return 0, err; end; </code>``</p>
142<p><code>try</code> says the same in a word. <code>x := try value(a)</code> calls <code>value</code> and, if the error it returned isn't <code>nil</code>, returns that error from the procedure it stands in, with zero for that procedure's other results; otherwise it is <code>value</code>'s number. There is no hidden unwinding and no handler: <code>try</code> is a <code>return</code>, written where the call is, and a procedure that uses it must end in an <code>ERROR</code> itself, so its signature says it can fail. <code>main</code> returns none, so it tests its errors and deals with them.</p>
143<div class="file"><span>Layers.raz</span></div><pre class="code"><code><span class="kw">module</span> Layers;
144<span class="kw">import</span> Out, Str;
145
146<span class="kw">const</span> Missing*: <span class="ty">ERROR</span> = <span class="string">&quot;key missing&quot;</span>;
147
148<span class="comment">(* the number after &quot;n=&quot; in a line like &quot;n=17&quot; *)</span>
149<span class="kw">proc</span> value(line: []<span class="kw">const</span> <span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
150<span class="kw">var</span> n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>;
151<span class="kw">begin</span>
152 <span class="kw">if</span> <span class="kw">not</span> Str.hasPrefix(line, <span class="string">&quot;n=&quot;</span>) <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, Missing; <span class="kw">end</span>;
153 n, ok := Str.toInt(line[<span class="number">2</span>..<span class="ty">LEN</span>(line)]);
154 <span class="kw">if</span> <span class="kw">not</span> ok <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, Missing; <span class="kw">end</span>;
155 <span class="kw">return</span> <span class="ty">INT</span>(n), <span class="ty">nil</span>;
156<span class="kw">end</span>;
157
158<span class="comment">(* the sum of the values of two lines: the first error ends it *)</span>
159<span class="kw">proc</span> sum(a, b: []<span class="kw">const</span> <span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
160<span class="kw">begin</span>
161 <span class="kw">return</span> <span class="kw">try</span> value(a) + <span class="kw">try</span> value(b), <span class="ty">nil</span>;
162<span class="kw">end</span>;
163
164<span class="kw">proc</span> main*;
165<span class="kw">var</span> n: <span class="ty">INT</span>; err: <span class="ty">ERROR</span>;
166<span class="kw">begin</span>
167 n, err := sum(<span class="string">&quot;n=40&quot;</span>, <span class="string">&quot;n=2&quot;</span>);
168 <span class="kw">if</span> err = <span class="ty">nil</span> <span class="kw">then</span> Out.int(n); Out.ln; <span class="kw">end</span>;
169 n, err := sum(<span class="string">&quot;n=40&quot;</span>, <span class="string">&quot;x=2&quot;</span>);
170 <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="string">&quot;sum failed: &quot;</span>); Out.str(<span class="ty">MSG</span>(err)); Out.ln; <span class="kw">end</span>;
171<span class="kw">end</span>;
172
173<span class="kw">end</span> Layers.</code></pre>
174<pre class="output"><code>42
175sum failed: key missing
176</code></pre>
177<h2>When the library reports errors</h2>
178<p>Library procedures that can fail say so in the same way: <code>Thread.spawn</code> returns an <code>ERROR</code>; <code>Sys</code> calls return a negative <code>errno</code> as an <code>INT</code>, with <code>0</code> or a positive count for success; <code>Files.read</code> returns a <code>BOOL</code>. The first is the language's way; the others are the system's, left as they are.</p>
179<div class="file"><span>Probe.raz</span></div><pre class="code"><code><span class="kw">module</span> Probe;
180<span class="kw">import</span> Out, Sys;
181
182<span class="kw">proc</span> main*;
183<span class="kw">var</span> fd: <span class="ty">INT</span>;
184<span class="kw">begin</span>
185 fd := Sys.openRead(<span class="string">&quot;/no/such/file&quot;</span>);
186 <span class="kw">if</span> fd &lt; <span class="number">0</span> <span class="kw">then</span>
187 Out.str(<span class="string">&quot;open failed, errno &quot;</span>); Out.int(-fd); Out.ln; <span class="comment">(* 2: ENOENT *)</span>
188 <span class="kw">end</span>;
189 fd := Sys.openRead(<span class="string">&quot;/dev/null&quot;</span>);
190 <span class="kw">if</span> fd &gt;= <span class="number">0</span> <span class="kw">then</span>
191 Out.str(<span class="string">&quot;open worked\n&quot;</span>);
192 Sys.close(fd);
193 <span class="kw">end</span>;
194<span class="kw">end</span>;
195
196<span class="kw">end</span> Probe.</code></pre>
197<pre class="output"><code>open failed, errno 2
198open worked
199</code></pre>
200<h2>When there is nothing to do but stop</h2>
201<p>A program that has found a state it can't carry on from, such as a broken invariant, says so and ends with <code>Rt.stop</code>: the message goes to standard error and the status is 2, the same as a failed check.</p>
202<div class="file"><span>Giveup.raz</span></div><pre class="code"><code><span class="kw">module</span> Giveup;
203<span class="kw">import</span> Rt;
204
205<span class="kw">proc</span> main*;
206<span class="kw">begin</span>
207 Rt.stop(<span class="string">&quot;the table is corrupt&quot;</span>);
208<span class="kw">end</span>;
209
210<span class="kw">end</span> Giveup.</code></pre>
211<pre class="output term"><code><span class="cmd">$ raz run Giveup.raz; echo &quot;status $?&quot;</span>
212the table is corrupt
213status 2</code></pre><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="memory.html">&larr; Memory and allocators</a><a href="index.html">Contents</a><a class="next" href="defer.html">Defer &rarr;</a></div></article></div>
214</main>
215<footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>
216</body>
217</html>