-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Cant report an error encountered in a try closure from the catch closure #8402
Description
Describe the bug
I can write a try/catch which reports whatever error from the try block, or I can catch that error in the catch block and do some cleanup. But I can't do both in the same script.
I want to be able to do both of these in unit tests for (standard) library script modules. e.g, some tests create temporary files and I want to clean those up whether the tests succeed or fail. And I would also, very much, like to see an error if a test fails.
How to reproduce
Report the specific error that occurs in a try closure:
let t = try {
echo "1: top of try closure"
1 / 0
30
} catch { | error |
#echo $"2: in catch closure, returning 42"
echo $"3: in catch closure, error is ($error | debug) returning 42"
42
}
echo $"4: after try/catch, t is ($t | debug)"
echo ""Produces:
〉nu trycatch.nu
1: top of try closure
Error: nu::shell::division_by_zero
× Division by zero.
╭─[/home/bobhy/work/nushell/trycatch.nu:2:1]
2 │ echo "1: top of try closure"
3 │ 1 / 0
· ┬
· ╰── division by zero
4 │ 30
╰────Note that messages 2 and 4 are MIA.
Catch the error and clean up after it:
let t = try {
echo "1: top of try closure"
1 / 0
30
} catch { | error |
echo $"2: in catch closure, returning 42"
#echo $"3: in catch closure, error is ($error | debug) returning 42"
42
}
echo $"4: after try/catch, t is ($t | debug)"
echo ""Produces:
〉nu trycatch.nu
1: top of try closure
2: in catch closure, returning 42
4: after try/catch, t is 42Here, messages 2 and 4 both appear. I could put cleanup code in either place.
Expected behavior
I was hoping the error from the try closure would be passed into the catch closure as the closure argument (or perhaps in $in). From reading the code, it appears to be at least trying to do both of these things, but I doesn't seem fully functional, or I'm misunderstanding something (very likely).
Screenshots
No response
Configuration
〉version | transpose key value | to md --pretty
| key | value |
|---|---|
| version | 0.76.1 |
| branch | main |
| commit_hash | e435196 |
| build_os | linux-x86_64 |
| build_target | x86_64-unknown-linux-gnu |
| rust_version | rustc 1.66.1 (90743e729 2023-01-10) |
| rust_channel | 1.66.1-x86_64-unknown-linux-gnu |
| cargo_version | cargo 1.66.1 (ad779e08b 2023-01-10) |
| build_time | 2023-03-11 00:04:44 -05:00 |
| build_rust_channel | release |
| features | default, zip |
| installed_plugins |
Additional context
No response