Skip to content

Commit

Permalink
[errors] Tweak the appearance of fatal error messages from 'errexit'.
Browse files Browse the repository at this point in the history
Addresses #168.
  • Loading branch information
Andy Chu committed May 1, 2019
1 parent 22a2a4b commit ebdb124
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
22 changes: 12 additions & 10 deletions osh/cmd_exec.py
Expand Up @@ -404,25 +404,27 @@ def _PushErrExit(self):
def _PopErrExit(self):
self.exec_opts.errexit.Pop()

def _CheckStatus(self, status, node, argv0=None):
def _CheckStatus(self, status, node):
"""ErrExitFailure with location info attached."""
if self.exec_opts.ErrExit() and status != 0:
# Add context based on node type
# TODO: only print location info for external commands? Any errors
# generated by builtins or the shell itself will already have location
# info printed (e.g. command not found)

if node.tag == command_e.SimpleCommand:
argv0 = argv0 or '<unknown>'
raise util.ErrExitFailure(
'[%d] %r command exited with status %d', posix.getpid(), argv0,
status, word=node.words[0], status=status)
'Exiting with status %d (command, PID %d)', status, posix.getpid(),
word=node.words[0], status=status)
elif node.tag == command_e.Assignment:
span_id = self._SpanIdForAssignment(node)
raise util.ErrExitFailure(
'[%d] assignment exited with status %d', posix.getpid(),
status, span_id=span_id, status=status)

'Exiting with status %d (assignment, PID %d)', status, posix.getpid(),
span_id=span_id, status=status)
else:
# No location info
raise util.ErrExitFailure(
'[%d] %r exited with status %d', posix.getpid(),
node.__class__.__name__, status, status=status)
'Exiting with status %d (in PID %d)', status, posix.getpid(),
status=status)

def _EvalLhs(self, node, spid, lookup_mode):
"""lhs_expr -> lvalue."""
Expand Down
9 changes: 8 additions & 1 deletion test/runtime-errors.sh
Expand Up @@ -67,6 +67,13 @@ failed_command() {
echo 'SHOULD NOT GET HERE'
}

errexit() {
set -o errexit
rm nonexistent/file

echo 'SHOULD NOT GET HERE'
}

pipefail() {
false | wc -l

Expand Down Expand Up @@ -394,7 +401,7 @@ all() {

for t in \
no_such_command no_such_command_commandsub no_such_command_heredoc \
failed_command \
failed_command errexit \
pipefail pipefail_group pipefail_subshell pipefail_func pipefail_while \
pipefail_multiple core_process \
nonexistent nounset bad_var_ref \
Expand Down

0 comments on commit ebdb124

Please sign in to comment.