Skip to content

Commit f08935f

Browse files
mbolivar-nordiccarlescufi
authored andcommitted
scripts: west_commands: improve run_common error handling
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]>
1 parent e6873b8 commit f08935f

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

scripts/west_commands/run_common.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77

88
import argparse
99
import logging
10-
from os import getcwd, path
10+
from os import close, getcwd, path
1111
from subprocess import CalledProcessError
12+
import tempfile
1213
import textwrap
14+
import traceback
1315

1416
from west import cmake
1517
from west import log
1618
from west import util
1719
from build_helpers import find_build_dir, is_zephyr_build, \
1820
FIND_BUILD_DIR_DESCRIPTION
19-
from west.commands import CommandContextError
21+
from west.commands import CommandError
2022

2123
from runners import get_runner_cls, ZephyrBinaryRunner, MissingProgram
2224

@@ -184,6 +186,13 @@ def _build_dir(args, die_if_none=True):
184186
else:
185187
return None
186188

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

188197
def do_run_common(command, args, runner_args, cached_runner_var):
189198
if args.context:
@@ -224,10 +233,9 @@ def do_run_common(command, args, runner_args, cached_runner_var):
224233
runner = args.runner or cache.get(cached_runner_var)
225234

226235
if runner is None:
227-
raise CommandContextError(textwrap.dedent("""
228-
No {} runner available for {}. Please either specify one
229-
manually, or check your board's documentation for
230-
alternative instructions.""".format(command_name, board)))
236+
log.die('No', command_name, 'runner available for board', board,
237+
'({} is not in the cache).'.format(cached_runner_var),
238+
"Check your board's documentation for instructions.")
231239

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

0 commit comments

Comments
 (0)