Repository navigation
Description
Let's add solid error handling to Fish.
- Ability to detect failed non-zero exit commands and treat them as error in systematic way.
- Manual checking each command for failure is error-prone and highly redundant
- Ability to report such errors along with a trace including line of code where the error occured
- Ability for developers to act on such errors in production environments
- With a proper handler, ability to report errors to Bugsnag
- The above gives complicated scripts which modify the environment the ability to rollback all changes should any error occur. Imagine a network bridge setup script which is invoked on remote on a server. This script interacts with network tools, shuts down interfaces, brings them up, checks connectivity. Any of these can fail and ultimately sever the server from outside world. Ability to rescue from arbitrary error gives a tool to bring back the original configs and restore connectivity.
Possible implementation:
Given a block of code which is "error rescue enabled" and pushed to the stack, any subsequent stack frame should inherit this property. If an external command is run, or one of a yet to be defined built in commands without prior checking of $status then execution context should jump to a special rescue function or block should be called. It's yet to be defined whether execution can be resumed or not.
If $status is checked or an ; or is invoked then the error should no longer be rescued from.
These shouldn't cause error rescue because $status is being checked:
# assume below code is in error rescue mode
which nonexistent-command; or echo "Warning: command does not exist"`# assume below code is in error rescue mode
which nonexistent-command
if test $status != 0
echo "Warning: command does not exist"`
endImagine a developer writing something along these lines (and 10x longer):
ifconfig non-existent-interface up # we want stacktrace here!
brctl addbr br0
brctl addif br0 non-existent-interface
Prior art - bash's ugly solution of trap ERR
Note: there is also set -e which silently kills the script in case of any errors. It effectively prevents
Way to get better trace http://stackoverflow.com/q/6928946/403571
#!/bin/bash
error_report() {
echo Got error in line ${@}
exit 1
}
trap 'error_report $LINENO' ERR
which ddddddddddddd
echo "Shouldn't get here"Syntax proposal with new keyword
begin
which ddddddddddd # this fails
echo "Should not get here" #running another command without checking `$status`
rescue
status --print-stack-trace
# Could we handle return 1 the same way as in the below proposal?
endSyntax proposal with minor changes to the parser:
Same changes needed for #1439 (comment)
function error_handler
status --print-stack-trace
return 1 # means not run anything more in the block, return 0 for ignoring error
end
begin --rescue error_handler
which ddddddddddd # this fails
echo "Should not get here" #running another command without checking `$status` should run our rescue handler
endWhat do y'all think?
Activity
chamini2 commentedon Sep 27, 2016
I like the new keyword proposal, maybe it could introduce a variable in the
rescuepart likefunctionintroducesargvbut with the status codefloam commentedon Sep 27, 2016
An initial two cents: "rescue" is funny.
floam commentedon Sep 27, 2016
I'd look to Python's "with" statement for some guidance.
Rush commentedon Sep 27, 2016
[-]Catch/rescue errors proposal (functional equivalent of trap ERR)[/-][+]Catch/rescue errors proposal (functional equivalent to trap ERR)[/+]kopischke commentedon Sep 28, 2016
@Rush @floam Ruby allows a
rescuesection in in some other block scopes too, like function definitions,begin … endbeing the one that has no other reason to exist but creating such a block scope. That would actually nicely match the semantics of fish’s currentbegin … end, especially as the new Boolean binding rules in version 2 have somewhat reduced the utility of the construct.zanchey commentedon Sep 30, 2016
I'm not sold on "rescue" either, but I definitely think something like this would be useful. The script that runs the nightly build is written in zsh (for shame!) because we don't have a simple error handling and recovery method yet.
kopischke commentedon Sep 30, 2016
@zanchey I’m not arguing for that particular term, just wanted to provide some context and indicate that Ruby’s example of extending block scopes, if not necessarily its syntax, would fit well into fish. As far as I am concerned, choosing
rescue,catch,trap,on erroror anything else as the keyword makes no functional difference.ridiculousfish commentedon Oct 1, 2016
See also at least #510 and #805
krader1961 commentedon Oct 6, 2016
As much as it pains me to close this issue as duplicate of #510 I'm going to do so. While the opening comment in this issue is considerably clearer and more helpful than that in #510 they are both making essentially the same proposal.
Rush commentedon Oct 6, 2016
@krader1961 Makes sense to close it but could you at least merge the comments together?
krader1961 commentedon Oct 6, 2016
I was hoping you would repost your opening comment in #510 so that it is clear you wrote it and you get updates when #510 is modified.
deal with BC_LINE_LENGTH not being honored