Beispiel #1
0
    def testConvertKnownError(self):
        err = http_error.MakeHttpError(400)
        new_err, print_exc = exceptions.ConvertKnownError(err)
        self.assertIsInstance(new_err, exceptions.HttpException)
        self.assertTrue(print_exc)

        err = socket.error(errno.ECONNREFUSED, 'error')
        new_err, print_exc = exceptions.ConvertKnownError(err)
        self.assertIsInstance(new_err, core_exceptions.NetworkIssueError)
        self.assertTrue(print_exc)
    def testConvertKnownErrorNetworkIssue(self):
        err = socket.error(errno.ECONNREFUSED, 'error')
        new_err, print_exc = exceptions.ConvertKnownError(err)
        self.assertIsInstance(new_err, core_exceptions.NetworkIssueError)
        self.assertTrue(print_exc)

        err = socket.timeout('timed out')
        new_err, print_exc = exceptions.ConvertKnownError(err)
        self.assertIsInstance(new_err, core_exceptions.NetworkIssueError)
        self.assertTrue(print_exc)
Beispiel #3
0
    def _should_retry_resumable_download(exc_type, exc_value, exc_traceback,
                                         state):
        converted_error, _ = calliope_errors.ConvertKnownError(exc_value)
        if isinstance(exc_value,
                      oauth2client.client.HttpAccessTokenRefreshError):
            if exc_value.status < 500 and exc_value.status != 429:
                # Not server error or too many requests error.
                return False
        elif not (isinstance(converted_error,
                             core_exceptions.NetworkIssueError) or
                  isinstance(converted_error, cloud_errors.RetryableApiError)):
            # Not known transient network error.
            return False

        start_byte = download_stream.tell()
        if start_byte > progress_state['start_byte']:
            # We've made progress, so allow a fresh set of retries.
            progress_state['start_byte'] = start_byte
            state.retrial = 0
        log.debug('Retrying download from byte {} after exception: {}.'
                  ' Trace: {}'.format(start_byte, exc_type, exc_traceback))

        apitools_http_wrapper.RebuildHttpConnections(
            apitools_download.bytes_http)
        return True
Beispiel #4
0
    def _HandleAllErrors(self, exc, command_path_string, flag_collection):
        """Handle all errors.

    Args:
      exc: Exception, The exception that was raised.
      command_path_string: str, The '.' separated command path.
      flag_collection: str, The flag collection name for metrics.

    Raises:
      exc or a core.exceptions variant that does not produce a stack trace.
    """
        if isinstance(exc, exceptions.ExitCodeNoError):
            self._HandleKnownError(exc,
                                   command_path_string,
                                   flag_collection,
                                   print_error=False)
        elif isinstance(exc, core_exceptions.Error):
            self._HandleKnownError(exc,
                                   command_path_string,
                                   flag_collection,
                                   print_error=True)
        else:
            known_exc = exceptions.ConvertKnownError(exc)
            if known_exc:
                self._HandleKnownError(known_exc,
                                       command_path_string,
                                       flag_collection,
                                       print_error=True,
                                       orig_exc=exc)
            else:
                # Make sure any uncaught exceptions still make it into the log file.
                log.debug(console_attr.EncodeForOutput(exc),
                          exc_info=sys.exc_info())
                metrics.Error(exc)
                raise
 def testConvertKnownErrorNonAscii(self):
     err = socket.error(errno.ECONNREFUSED, b'\xce\x94')
     try:
         new_err, print_exc = exceptions.ConvertKnownError(err)
     except UnicodeError as e:
         self.fail(e)
     self.assertIsInstance(new_err, core_exceptions.NetworkIssueError)
     self.assertTrue(print_exc)
    def testConvertKnownErrorFromSubclass(self):
        class SSLErrorExt(ssl.SSLError):
            pass

        err = SSLErrorExt('Test SSL Error Extension')
        new_err, print_exc = exceptions.ConvertKnownError(err)
        self.assertIsInstance(new_err, core_exceptions.NetworkIssueError)
        self.assertTrue(print_exc)
    def testConvertKnownErrorFromClassHierarchy(self):
        class SSLErrorExt1(ssl.SSLError):
            pass

        class SSLErrorExt2(SSLErrorExt1):
            pass

        err = SSLErrorExt2('Test SSL Error Sub-Extension')
        new_err, print_exc = exceptions.ConvertKnownError(err)
        self.assertIsInstance(new_err, core_exceptions.NetworkIssueError)
        self.assertTrue(print_exc)
Beispiel #8
0
    def _HandleAllErrors(self, exc, command_path_string, specified_arg_names):
        """Handle all errors.

    Args:
      exc: Exception, The exception that was raised.
      command_path_string: str, The '.' separated command path.
      specified_arg_names: [str], The specified arg named scrubbed for metrics.

    Raises:
      exc or a core.exceptions variant that does not produce a stack trace.
    """
        if session_capturer.SessionCapturer.capturer:
            session_capturer.SessionCapturer.capturer.CaptureException(exc)
        known_exc, print_error = exceptions.ConvertKnownError(exc)
        error_extra_info = {'error_code': getattr(exc, 'exit_code', 1)}
        if isinstance(exc, exceptions.HttpException):
            error_extra_info['http_status_code'] = exc.payload.status_code

        metrics.Commands(command_path_string,
                         config.CLOUD_SDK_VERSION,
                         specified_arg_names,
                         error=exc.__class__,
                         error_extra_info=error_extra_info)
        metrics.Error(command_path_string,
                      exc.__class__,
                      specified_arg_names,
                      error_extra_info=error_extra_info)

        if known_exc:
            msg = u'({0}) {1}'.format(
                console_attr.EncodeForConsole(command_path_string),
                console_attr.EncodeForConsole(known_exc))
            log.debug(msg, exc_info=sys.exc_info())
            if print_error:
                log.error(msg)
            # Uncaught errors will be handled in gcloud_main.
            if self.__known_error_handler:
                self.__known_error_handler(exc)
            if properties.VALUES.core.print_handled_tracebacks.GetBool():
                raise
            self._Exit(known_exc)
        else:
            # Make sure any uncaught exceptions still make it into the log file.
            log.debug(console_attr.EncodeForConsole(exc),
                      exc_info=sys.exc_info())
            raise
Beispiel #9
0
    def _HandleAllErrors(self, exc, command_path_string, specified_arg_names):
        """Handle all errors.

    Args:
      exc: Exception, The exception that was raised.
      command_path_string: str, The '.' separated command path.
      specified_arg_names: [str], The specified arg named scrubbed for metrics.

    Raises:
      exc or a core.exceptions variant that does not produce a stack trace.
    """
        if isinstance(exc, exceptions.ExitCodeNoError):
            self._HandleKnownError(exc,
                                   command_path_string,
                                   specified_arg_names,
                                   print_error=False)
        elif isinstance(exc, core_exceptions.Error):
            self._HandleKnownError(exc,
                                   command_path_string,
                                   specified_arg_names,
                                   print_error=True)
        else:
            known_exc = exceptions.ConvertKnownError(exc)
            if known_exc:
                self._HandleKnownError(known_exc,
                                       command_path_string,
                                       specified_arg_names,
                                       print_error=True,
                                       orig_exc=exc)
            else:
                # Make sure any uncaught exceptions still make it into the log file.
                log.debug(console_attr.EncodeForOutput(exc),
                          exc_info=sys.exc_info())
                metrics.Error(command_path_string,
                              exc.__class__,
                              specified_arg_names,
                              error_extra_info={'error_code': 1})
                raise
 def testConvertKnownError(self):
     err = http_error.MakeHttpError(400)
     new_err, print_exc = exceptions.ConvertKnownError(err)
     self.assertIsInstance(new_err, exceptions.HttpException)
     self.assertTrue(print_exc)
 def testConvertNoPrint(self):
     err = exceptions.ExitCodeNoError('Error')
     new_err, print_exc = exceptions.ConvertKnownError(err)
     self.assertIsInstance(new_err, exceptions.ExitCodeNoError)
     self.assertFalse(print_exc)
 def testConvertUnknownError(self):
     err = ValueError()
     new_err, print_exc = exceptions.ConvertKnownError(err)
     self.assertIsNone(new_err)
     self.assertTrue(print_exc)