Bug 1258497: wrap mach Commands in try/except; r?gps draft
authorDustin J. Mitchell <dustin@mozilla.com>
Mon, 16 May 2016 17:44:24 +0000
changeset 367419 f1c435dedcc22700942f5dbbf457ce2654bc42a7
parent 367418 927ae951edbbfeec6fd371a62ff958eee581679f
child 521009 76a394a9793c4bb44127e93c40162699efd4ddd4
push id18238
push userdmitchell@mozilla.com
push dateMon, 16 May 2016 17:47:55 +0000
reviewersgps
bugs1258497
milestone49.0a1
Bug 1258497: wrap mach Commands in try/except; r?gps MozReview-Commit-ID: FTPDTjw4gR
taskcluster/mach_commands.py
taskcluster/taskgraph/parameters.py
--- a/taskcluster/mach_commands.py
+++ b/taskcluster/mach_commands.py
@@ -2,17 +2,17 @@
 
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 from __future__ import absolute_import, print_function, unicode_literals
 
 import sys
-import textwrap
+import traceback
 
 from mach.decorators import (
     CommandArgument,
     CommandProvider,
     Command,
     SubCommand,
 )
 
@@ -131,30 +131,37 @@ class MachCommands(MachCommandBase):
     def taskgraph_decision(self, **options):
         """Run the decision task: generate a task graph and submit to
         TaskCluster.  This is only meant to be called within decision tasks,
         and requires a great many arguments.  Commands like `mach taskgraph
         optimized` are better suited to use on the command line, and can take
         the parameters file generated by a decision task.  """
 
         import taskgraph.decision
-        return taskgraph.decision.taskgraph_decision(self.log, options)
+        try:
+            return taskgraph.decision.taskgraph_decision(self.log, options)
+        except Exception as e:
+            traceback.print_exc()
+            sys.exit(1)
 
     def show_taskgraph(self, graph_attr, options):
         import taskgraph.parameters
         import taskgraph.target_tasks
         import taskgraph.generator
 
-        parameters = taskgraph.parameters.load_parameters_file(options)
+        try:
+            parameters = taskgraph.parameters.load_parameters_file(options)
 
-        target_tasks_method = parameters.get('target_tasks_method', 'all_tasks')
-        target_tasks_method = taskgraph.target_tasks.get_method(target_tasks_method)
-        tgg = taskgraph.generator.TaskGraphGenerator(
-            root_dir=options['root'],
-            log=self.log,
-            parameters=parameters,
-            target_tasks_method=target_tasks_method)
+            target_tasks_method = parameters.get('target_tasks_method', 'all_tasks')
+            target_tasks_method = taskgraph.target_tasks.get_method(target_tasks_method)
+            tgg = taskgraph.generator.TaskGraphGenerator(
+                root_dir=options['root'],
+                log=self.log,
+                parameters=parameters,
+                target_tasks_method=target_tasks_method)
 
-        tg = getattr(tgg, graph_attr)
+            tg = getattr(tgg, graph_attr)
 
-        for label in tg.graph.visit_postorder():
-            print(tg.tasks[label])
-
+            for label in tg.graph.visit_postorder():
+                print(tg.tasks[label])
+        except Exception as e:
+            traceback.print_exc()
+            sys.exit(1)
--- a/taskcluster/taskgraph/parameters.py
+++ b/taskcluster/taskgraph/parameters.py
@@ -30,18 +30,17 @@ def load_parameters_file(options):
     if not filename:
         return Parameters()
     with open(filename) as f:
         if filename.endswith('.yml'):
             return Parameters(**yaml.safe_load(f))
         elif filename.endswith('.json'):
             return Parameters(**json.load(f))
         else:
-            print("Parameters file `{}` is not JSON or YAML".format(filename))
-            sys.exit(1)
+            raise TypeError("Parameters file `{}` is not JSON or YAML".format(filename))
 
 def get_decision_parameters(options):
     """
     Load parameters from the command-line options for 'taskgraph decision'.
     """
     return Parameters({n: options[n] for n in [
         'base_repository',
         'head_repository',