def _ReportCrash(err): """Report the anonymous crash information to the Error Reporting service. Args: err: Exception, the error that caused the crash. """ stacktrace = traceback.format_exc(err) stacktrace = error_reporting_util.RemovePrivateInformationFromTraceback( stacktrace) command = properties.VALUES.metrics.command_name.Get() cid = metrics.GetCIDIfMetricsEnabled() client = _GetReportingClient() reporter = util.ErrorReporting(client) try: reporter.ReportEvent(error_message=stacktrace, service=CRASH_SERVICE, version=config.CLOUD_SDK_VERSION, project=CRASH_PROJECT, request_url=command, user=cid) except apitools_exceptions.HttpError as http_err: log.file_only_logger.error( 'Unable to report crash stacktrace:\n{0}'.format( console_attr.EncodeForConsole(http_err)))
def ReportError(err, is_crash): """Report the anonymous crash information to the Error Reporting service. Args: err: Exception, the error that caused the crash. is_crash: bool, True if this is a crash, False if it is a user error. """ if properties.VALUES.core.disable_usage_reporting.GetBool(): return stacktrace = traceback.format_exc(err) stacktrace = error_reporting_util.RemovePrivateInformationFromTraceback( stacktrace) command = properties.VALUES.metrics.command_name.Get() cid = metrics.GetCIDIfMetricsEnabled() client = _GetReportingClient(is_crash) reporter = util.ErrorReporting(client) try: method_config = client.projects_events.GetMethodConfig('Report') request = reporter.GenerateReportRequest( error_message=stacktrace, service=SERVICE, version=config.CLOUD_SDK_VERSION, project=CRASH_PROJECT if is_crash else ERROR_PROJECT, request_url=command, user=cid) http_request = client.projects_events.PrepareHttpRequest( method_config, request) metrics.CustomBeacon(http_request.url, http_request.http_method, http_request.body, http_request.headers) except apitools_exceptions.Error as e: log.file_only_logger.error( 'Unable to report crash stacktrace:\n{0}'.format( console_attr.SafeText(e)))
def testFormatTracebackIncorrecTracebackFormat(self): self.StartObjectPatch(os.path, 'sep', posixpath.sep) self.StartObjectPatch(os.path, 'dirname', posixpath.dirname) self.StartObjectPatch(os.path, 'commonprefix', posixpath.commonprefix) self.assertEqual( None, error_reporting_util.RemovePrivateInformationFromTraceback( self._EXAMPLE_WRONG_TRACEBACK))
def _ReportError(err): """Get the command and stacktrace and sends Error to Error Reporting. Args: err: Exception err. """ command = ' '.join(sys.argv[1:]) stacktrace = traceback.format_exc(err) stacktrace = error_reporting_util.RemovePrivateInformationFromTraceback( stacktrace) try: util.ErrorReporting().ReportEvent(error_message=stacktrace, service=command, version=config.CLOUD_SDK_VERSION) except apitools_exceptions.HttpError: pass
def testFormatTracebackRemoveInfo_UnixPathSept(self): self.StartObjectPatch(os.path, 'sep', posixpath.sep) self.StartObjectPatch(os.path, 'dirname', posixpath.dirname) self.StartObjectPatch(os.path, 'commonprefix', posixpath.commonprefix) expected_formatted_stacktrace = textwrap.dedent("""\ Traceback (most recent call last): File "google-cloud-sdk/lib/test.py", line 3, in <module> main() File "google-cloud-sdk/lib/test.py", line 2, in main example.method() File "google-cloud-sdk/lib/example.py", line 70, in method a = b + foo.Bar() File "google-cloud-sdk/lib/googlecloudsdk/foo.py", line 700, in bar c.function() File "google-cloud-sdk/lib/third_party/bread/toast.py", line 1, in function raise Exception('really really long message') Exception """) self.assertEqual( expected_formatted_stacktrace, error_reporting_util.RemovePrivateInformationFromTraceback( self._EXAMPLE_TRACEBACK_UNIX_USER_STACK))