| 1 | title: Errors |
| 2 | part: Go |
| 3 | summary: A second result, compared by identity; passing errors up; and no exceptions. |
| 4 | --- |
| 5 | ```raz Errors run |
| 6 | ``` |
| 7 | |
| 8 | 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. |
| 9 | |
| 10 | ## Handling them by kind |
| 11 | |
| 12 | Because an error is compared by identity, a caller can handle the cases it knows and pass on the rest. |
| 13 | |
| 14 | ```raz Parse run |
| 15 | module Parse; |
| 16 | import Out, Str; |
| 17 | |
| 18 | const |
| 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 *) |
| 24 | proc percent(s: []const U8): (INT, ERROR); |
| 25 | var n: I64; ok: BOOL; |
| 26 | begin |
| 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; |
| 32 | end; |
| 33 | |
| 34 | proc main*; |
| 35 | var inputs: [4][]const U8; i, n: INT; err: ERROR; |
| 36 | begin |
| 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; |
| 49 | end; |
| 50 | |
| 51 | end Parse. |
| 52 | ``` |
| 53 | |
| 54 | ## Passing errors up |
| 55 | |
| 56 | 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: |
| 57 | |
| 58 | ```raz |
| 59 | x, err := value(a); |
| 60 | if 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 |
| 66 | module Layers; |
| 67 | import Out, Str; |
| 68 | |
| 69 | const Missing*: ERROR = "key missing"; |
| 70 | |
| 71 | (* the number after "n=" in a line like "n=17" *) |
| 72 | proc value(line: []const U8): (INT, ERROR); |
| 73 | var n: I64; ok: BOOL; |
| 74 | begin |
| 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; |
| 79 | end; |
| 80 | |
| 81 | (* the sum of the values of two lines: the first error ends it *) |
| 82 | proc sum(a, b: []const U8): (INT, ERROR); |
| 83 | begin |
| 84 | return try value(a) + try value(b), nil; |
| 85 | end; |
| 86 | |
| 87 | proc main*; |
| 88 | var n: INT; err: ERROR; |
| 89 | begin |
| 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; |
| 94 | end; |
| 95 | |
| 96 | end Layers. |
| 97 | ``` |
| 98 | |
| 99 | ## When the library reports errors |
| 100 | |
| 101 | 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. |
| 102 | |
| 103 | ```raz Probe run |
| 104 | module Probe; |
| 105 | import Out, Sys; |
| 106 | |
| 107 | proc main*; |
| 108 | var fd: INT; |
| 109 | begin |
| 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; |
| 119 | end; |
| 120 | |
| 121 | end Probe. |
| 122 | ``` |
| 123 | |
| 124 | ## When there is nothing to do but stop |
| 125 | |
| 126 | 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. |
| 127 | |
| 128 | ```raz Giveup |
| 129 | module Giveup; |
| 130 | import Rt; |
| 131 | |
| 132 | proc main*; |
| 133 | begin |
| 134 | Rt.stop("the table is corrupt"); |
| 135 | end; |
| 136 | |
| 137 | end Giveup. |
| 138 | ``` |
| 139 | |
| 140 | ```sh |
| 141 | $ raz run Giveup.raz; echo "status $?" |
| 142 | ``` |