예제 #1
0
 def wrapper(*args, **kwargs):
     try:
         return f(*args, **kwargs)
     except BundleAuthException:
         raise
     except RestClientException as e:
         raise BundleServiceException(message + ': ' + str(e),
                                      e.client_error)
     except urllib.error.HTTPError as e:
         try:
             # Ensure the type of urllib.error.HTTPError response to be string
             client_error = ensure_str(e.read())
             if e.reason == 'invalid_grant':
                 raise BundleAuthException(
                     message + ': ' + http.client.responses[e.code] +
                     ' - ' + client_error,
                     True,
                 )
             else:
                 raise BundleServiceException(
                     message + ': ' + http.client.responses[e.code] +
                     ' - ' + client_error,
                     400 <= e.code < 500,
                 )
         except json.decoder.JSONDecodeError as e:
             raise BundleServiceException(message + ': ' + str(e),
                                          False)
     except (urllib.error.URLError, http.client.HTTPException,
             socket.error) as e:
         raise BundleServiceException(message + ': ' + str(e), False)
예제 #2
0
 def wrapper(*args, **kwargs):
     try:
         return f(*args, **kwargs)
     except urllib.error.HTTPError as e:
         # Translate known errors to the standard CodaLab errors
         error_body = ensure_str(e.read())
         exc = http_error_to_exception(e.code, error_body)
         # All standard CodaLab errors are subclasses of UsageError
         if isinstance(exc, UsageError):
             six.reraise(exc.__class__, exc, sys.exc_info()[2])
         else:
             # Shunt other exceptions into one class
             six.reraise(
                 JsonApiException,
                 JsonApiException(
                     message.format(*args, **kwargs) + ': ' +
                     http.client.responses[e.code] + ' - ' + error_body,
                     400 <= e.code < 500,
                 ),
                 sys.exc_info()[2],
             )
     except RestClientException as e:
         six.reraise(
             JsonApiException,
             JsonApiException(
                 message.format(*args, **kwargs) + ': ' + str(e),
                 e.client_error),
             sys.exc_info()[2],
         )
     except (urllib.error.URLError, http.client.HTTPException,
             socket.error) as e:
         six.reraise(
             JsonApiException,
             JsonApiException(
                 message.format(*args, **kwargs) + ': ' + str(e),
                 False),
             sys.exc_info()[2],
         )