unison/unison-src/transcripts/top-level-exceptions.output.md

104 lines
2.0 KiB
Markdown
Raw Permalink Normal View History

2021-05-27 17:06:19 +03:00
A simple transcript to test the use of exceptions that bubble to the top level.
FYI, here are the `Exception` and `Failure` types:
``` ucm
scratch/main> view Exception Failure
2021-05-27 17:06:19 +03:00
2021-08-24 00:05:37 +03:00
structural ability builtin.Exception where
2021-05-27 17:06:19 +03:00
raise : Failure ->{builtin.Exception} x
type builtin.io2.Failure
2021-05-27 17:06:19 +03:00
= Failure Type Text Any
```
Here's a sample program just to verify that the typechecker allows `run` to throw exceptions:
``` unison
2021-05-27 17:06:19 +03:00
use builtin IO Exception Test.Result
main : '{IO, Exception} ()
main _ = ()
mytest : '{IO, Exception} [Test.Result]
mytest _ = [Ok "Great"]
```
``` ucm
2021-05-27 17:06:19 +03:00
Loading changes detected in scratch.u.
2021-05-27 17:06:19 +03:00
I found and typechecked these definitions in scratch.u. If you
do an `add` or `update`, here's how your codebase would
change:
⍟ These new definitions are ok to `add`:
main : '{IO, Exception} ()
mytest : '{IO, Exception} [Result]
```
``` ucm
scratch/main> run main
2021-05-27 17:06:19 +03:00
2022-08-15 21:30:14 +03:00
()
scratch/main> add
2021-05-27 17:06:19 +03:00
⍟ I've added these definitions:
main : '{IO, Exception} ()
mytest : '{IO, Exception} [Result]
scratch/main> io.test mytest
2021-05-27 17:06:19 +03:00
New test results:
1. mytest ◉ Great
2021-05-27 17:06:19 +03:00
✅ 1 test(s) passing
2024-06-26 23:01:36 +03:00
Tip: Use view 1 to view the source of a test.
2021-05-27 17:06:19 +03:00
```
Now a test to show the handling of uncaught exceptions:
``` unison
2021-05-27 17:06:19 +03:00
main2 = '(error "oh noes!" ())
error : Text -> a ->{Exception} x
error msg a =
builtin.Exception.raise (Failure (typeLink RuntimeError) msg (Any a))
unique type RuntimeError =
```
``` ucm
2021-05-27 17:06:19 +03:00
Loading changes detected in scratch.u.
2021-05-27 17:06:19 +03:00
I found and typechecked these definitions in scratch.u. If you
do an `add` or `update`, here's how your codebase would
change:
⍟ These new definitions are ok to `add`:
type RuntimeError
2021-05-27 17:06:19 +03:00
error : Text -> a ->{Exception} x
main2 : '{Exception} r
```
``` ucm
scratch/main> run main2
2021-05-27 17:06:19 +03:00
💔💥
2021-07-26 21:43:37 +03:00
The program halted with an unhandled exception:
2022-02-13 08:20:06 +03:00
Failure (typeLink RuntimeError) "oh noes!" (Any ())
Stack trace:
##raise
2021-05-27 17:06:19 +03:00
```