Code: Errors done right in Jennifer
Errors done right in Jennifer: the code
The programs behind the slides. They run with Jennifer 0.27.0 from this folder (the imports of stock.j and fakeapi.j are relative). They need no decks; the HTTP examples start their own server on a loopback port. Error messages print full paths where the slides shorten them to the file name.
value.j: a thrownErrorand its kind, line and message ("An error is a value"). Run:jennifer run value.jreceives.j: what a catch receives fromchannel.recvTimeout, a closed channel and an index out of range, and areturngoing straight through a try ("What a catch receives"). Run:jennifer run receives.jgeneric.j: onematchon the kind handles a cap, a timeout, a closed channel and a refused connection, and rethrows a runtime error ("Generic kinds: one handler for every library"). Run:jennifer run generic.j. Needs nothing listening on127.0.0.1:1.netfields.j:net.NetErrorfields from a refused connection ("net: the retry decision is in the fields"). Run:jennifer run netfields.jfakeapi.j: a module that serves a stand-in API on127.0.0.1:18404, answering everyGET /users/:idwith 404 and the bodyno such user. Used bymessage.jandrest404.j; the port must be free.message.j: the message rest puts in an error for a 404, and the check on kind and status ("Match the kind, never the message"). Run:jennifer run message.jstock.j: the stock module withStockErrorandreserve("Errors that carry data: a struct of your own"). Imported by the programs below.mixed.j: a plainErrorwith kind"stock"crashes the handler that reads$e.sku("One kind, one shape"). Run:jennifer run mixed.j. Ends with that runtime error on purpose, atmixed.j:17:43.rest404.j: a helper typedErrorrefuses the caughtrest.HttpError("The trap: a helper typedError"). Run:jennifer run rest404.j. Ends with the runtime error atrest404.j:25:12on purpose.typed.j: the helper typed withstock.StockError("Type the helper with the module's struct"). Run:jennifer run typed.juntyped.j:def plain as Errorinside a try tells a plainErrorfrom aStockError("Still untyped? Ask withdef x as Error"). Run:jennifer run untyped.jrewrap_all.j: a wrapper that turns the nesting cap into kindconfig("Rewrapping hides the generic kinds"). Run:jennifer run rewrap_all.jrewrap_mine.j: the wrapper that passes the generic kinds through, fed the same input and a malformed document ("Rewrap only what is yours"). Run:jennifer run rewrap_mine.jdeferlost.j: a deferred call that throws supersedes the real cause ("Cleanup: defer, errdefer, and the lost cause"). Run:jennifer run deferlost.jweberror.j: a web handler throws aStockError, web logs it to standard error, theonErrorhook sees anError, and the demo's own request gets 500 ("web.onError sees every error asError"). Run:jennifer run weberror.j. Listens on127.0.0.1:18405, which must be free.l104.sh: writesl104.j, which throws a string and a map, to a temporary directory, runs it and lints it for the two L104 warnings ("Lint L104: throw something a catch can read"). It is a script because every.jhere must lint clean. Run:sh l104.shstock_test.j: the white-box overlay ofstock.jwithassertThrowsandassertThrowsWith, a field checked in the test, andtestWrongKind, which fails on purpose with the slide's message ("Testing errors"). Run:jennifer test stock_test.j(exits 1 because of that test).
| File | Size | |
|---|---|---|
| LICENSE | 899 B | raw |
| README.md | 3.5 KiB | raw |
| deferlost.j | 386 B | raw |
| fakeapi.j | 768 B | raw |
| generic.j | 1.3 KiB | raw |
| l104.sh | 801 B | raw |
| message.j | 575 B | raw |
| mixed.j | 617 B | raw |
| netfields.j | 237 B | raw |
| receives.j | 799 B | raw |
| rest404.j | 829 B | raw |
| rewrap_all.j | 442 B | raw |
| rewrap_mine.j | 658 B | raw |
| stock.j | 422 B | raw |
| stock_test.j | 588 B | raw |
| typed.j | 262 B | raw |
| untyped.j | 853 B | raw |
| value.j | 185 B | raw |
| weberror.j | 1.1 KiB | raw |