From ffd5064859ae6a99f08d4a1197c388266ec9336c Mon Sep 17 00:00:00 2001 From: Ricky Stewart Date: Wed, 29 Jul 2020 19:34:24 +0000 Subject: [PATCH] Bug 1646406 - Distinguish between user errors and actual reportable exceptions in `mach` r=mhentges,froydnj Add a new `UserError` class which when thrown doesn't get reported to Sentry. Differential Revision: https://phabricator.services.mozilla.com/D85026 --- python/mach/mach/main.py | 15 ++++++++++++++- python/mach/mach/util.py | 7 +++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/python/mach/mach/main.py b/python/mach/mach/main.py index 71ea1dcdfb4a..e5d3c8bffe2e 100644 --- a/python/mach/mach/main.py +++ b/python/mach/mach/main.py @@ -37,7 +37,7 @@ from .dispatcher import CommandAction from .logging import LoggingManager from .registrar import Registrar from .sentry import register_sentry, NoopErrorReporter -from .util import setenv +from .util import setenv, UserError SUGGEST_MACH_BUSTED_TEMPLATE = r''' You can invoke |./mach busted| to check if this issue is already on file. If it @@ -58,6 +58,10 @@ message. The details of the failure are as follows: '''.lstrip() +USER_ERROR = r''' +This is a user error and does not appear to be a bug in mach. +'''.lstrip() + COMMAND_ERROR_TEMPLATE = r''' The error occurred in the implementation of the invoked mach command. @@ -483,6 +487,15 @@ To see more help for a specific command, run: except FailedCommandError as e: print(e.message) return e.exit_code + except UserError: + # We explicitly don't report UserErrors to Sentry. + exc_type, exc_value, exc_tb = sys.exc_info() + # The first two frames are us and are never used. + stack = traceback.extract_tb(exc_tb)[2:] + self._print_error_header(argv, sys.stdout) + print(USER_ERROR) + self._print_exception(sys.stdout, exc_type, exc_value, stack) + return 1 except Exception: exc_type, exc_value, exc_tb = sys.exc_info() sentry.report_exception(exc_value) diff --git a/python/mach/mach/util.py b/python/mach/mach/util.py index 834a82081bd4..734f8d81e965 100644 --- a/python/mach/mach/util.py +++ b/python/mach/mach/util.py @@ -10,6 +10,13 @@ import sys from six import text_type +class UserError(Exception): + """Represents an error caused by something the user did wrong rather than + an internal `mach` failure. Exceptions that are subclasses of this class + will not be reported as failures to Sentry. + """ + + def setenv(key, value): """Compatibility shim to ensure the proper string type is used with os.environ for the version of Python being used.