Skip to content

Catch/rescue errors proposal (functional equivalent to trap ERR) #3414

Closed

Description

@Rush

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"`
end

Imagine 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"

More info: http://aplawrence.com/Basics/trapping_errors.html

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?
end

Syntax 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
end

What do y'all think?

Activity

  1. chamini2 commented on Sep 27, 2016

    @chamini2

    I like the new keyword proposal, maybe it could introduce a variable in the rescue part like function introduces argv but with the status code

  2. floam commented on Sep 27, 2016

    @floam
    Member

    An initial two cents: "rescue" is funny.

  3. floam commented on Sep 27, 2016

    @floam
    Member

    I'd look to Python's "with" statement for some guidance.

  4. Rush commented on Sep 27, 2016

    @Rush
    Author
  5. changed the title [-]Catch/rescue errors proposal (functional equivalent of trap ERR)[/-] [+]Catch/rescue errors proposal (functional equivalent to trap ERR)[/+] on Sep 28, 2016
  6. kopischke commented on Sep 28, 2016

    @kopischke

    @Rush @floam Ruby allows a rescue section in in some other block scopes too, like function definitions,begin … end being 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 current begin … end, especially as the new Boolean binding rules in version 2 have somewhat reduced the utility of the construct.

  7. zanchey commented on Sep 30, 2016

    @zanchey
    Member

    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.

  8. kopischke commented on Sep 30, 2016

    @kopischke

    @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 error or anything else as the keyword makes no functional difference.

  9. ridiculousfish commented on Oct 1, 2016

    @ridiculousfish
    Member

    See also at least #510 and #805

  10. krader1961 commented on Oct 6, 2016

    @krader1961
    Contributor

    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.

  11. Rush commented on Oct 6, 2016

    @Rush
    Author

    @krader1961 Makes sense to close it but could you at least merge the comments together?

  12. krader1961 commented on Oct 6, 2016

    @krader1961
    Contributor

    but could you at least merge the comments together?

    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.

  13. added a commit that references this issue on Jan 9, 2017
    4223b3d
  14. locked as resolved and limited conversation to collaborators on Apr 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions