Skip to content

Commit

Permalink
scripts: west_commands: improve run_common error handling
Browse files Browse the repository at this point in the history
Print a friendlier error message on ValueError, but don't throw away
the stack trace.

Move another call to log.die().

Signed-off-by: Marti Bolivar <[email protected]>
  • Loading branch information
mbolivar-nordic authored and carlescufi committed Jun 25, 2019
1 parent e6873b8 commit f08935f
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions scripts/west_commands/run_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@

import argparse
import logging
from os import getcwd, path
from os import close, getcwd, path
from subprocess import CalledProcessError
import tempfile
import textwrap
import traceback

from west import cmake
from west import log
from west import util
from build_helpers import find_build_dir, is_zephyr_build, \
FIND_BUILD_DIR_DESCRIPTION
from west.commands import CommandContextError
from west.commands import CommandError

from runners import get_runner_cls, ZephyrBinaryRunner, MissingProgram

Expand Down Expand Up @@ -184,6 +186,13 @@ def _build_dir(args, die_if_none=True):
else:
return None

def dump_traceback():
# Save the current exception to a file and return its path.
fd, name = tempfile.mkstemp(prefix='west-exc-', suffix='.txt')
close(fd) # traceback has no use for the fd
with open(name, 'w') as f:
traceback.print_exc(file=f)
log.inf("An exception trace has been saved in", name)

def do_run_common(command, args, runner_args, cached_runner_var):
if args.context:
Expand Down Expand Up @@ -224,10 +233,9 @@ def do_run_common(command, args, runner_args, cached_runner_var):
runner = args.runner or cache.get(cached_runner_var)

if runner is None:
raise CommandContextError(textwrap.dedent("""
No {} runner available for {}. Please either specify one
manually, or check your board's documentation for
alternative instructions.""".format(command_name, board)))
log.die('No', command_name, 'runner available for board', board,
'({} is not in the cache).'.format(cached_runner_var),
"Check your board's documentation for instructions.")

_banner('west {}: using runner {}'.format(command_name, runner))
if runner not in available:
Expand Down Expand Up @@ -277,6 +285,10 @@ def do_run_common(command, args, runner_args, cached_runner_var):
runner = runner_cls.create(cfg, parsed_args)
try:
runner.run(command_name)
except ValueError as ve:
log.err(str(ve), fatal=True)
dump_traceback()
raise CommandError(1)
except MissingProgram as e:
log.die('required program', e.filename,
'not found; install it or add its location to PATH')
Expand Down

0 comments on commit f08935f

Please sign in to comment.