def testErrorWithMessage(self): content = '{"error": {"message": "message"}}' error = testutil.CreateTestHttpError(10, 'reason', content) result = util.GetHttpErrorMessage(error) self.assertEqual( 'ResponseError: status=[10], code=[reason], message=[message]', result)
def testErrorWithViolationsWithoutDescription(self): content = """{"error": {"details": [{"field": "value", "fieldViolations": [{"key": "value"}]}]}}""" error = testutil.CreateTestHttpError(10, 'reason', content) result = util.GetHttpErrorMessage(error) self.assertEqual( 'ResponseError: status=[10], code=[reason], message=[]', result)
def testErrorInvalidJsonWithError(self): # Test that 'error' word doesn't break our functions. error = testutil.CreateTestHttpError(10, 'reason', 'json error') result = util.GetHttpErrorMessage(error) self.assertEqual( 'ResponseError: status=[10], code=[reason], message=[json error]', result)
def testErrorWithStringError(self): content = '{"error": "message"}' error = testutil.CreateTestHttpError(10, 'reason', content) result = util.GetHttpErrorMessage(error) expected = 'ResponseError: status=[{0}], code=[{1}], message=[{2}]'.format( 10, 'reason', '{"error": "message"}') self.assertEqual(result, expected)
def Run(self, args): """This is what gets called when the user runs this command. Args: args: an argparse namespace. All the arguments that were provided to this command invocation. Yields: Objects representing user functions. """ client = self.context['functions_client'] list_generator = list_pager.YieldFromList( service=client.projects_locations_functions, request=self.BuildRequest(args), limit=args.limit, field='functions', batch_size_attribute='pageSize') # Decorators (e.g. util.CatchHTTPErrorRaiseHTTPException) don't work # for generators. We have to catch the exception above the iteration loop, # but inside the function. try: for item in list_generator: yield item except exceptions.HttpError as error: msg = util.GetHttpErrorMessage(error) unused_type, unused_value, traceback = sys.exc_info() raise base_exceptions.HttpException, msg, traceback
def Run(self, args): client = util.GetApiClientInstance() list_generator = list_pager.YieldFromList( service=client.projects_locations, request=self._BuildRequest(), field='locations', batch_size_attribute='pageSize') try: for item in list_generator: yield item except api_exceptions.HttpError as error: msg = util.GetHttpErrorMessage(error) exceptions.reraise(base_exceptions.HttpException(msg))
def Run(self, args): client = self.context['functions_client'] list_generator = list_pager.YieldFromList( service=client.projects_locations, request=self.BuildRequest(args), field='locations', batch_size_attribute='pageSize') try: for item in list_generator: yield item except exceptions.HttpError as error: msg = util.GetHttpErrorMessage(error) unused_type, unused_value, traceback = sys.exc_info() raise base_exceptions.HttpException, msg, traceback
def _YieldFromLocation(self, location_ref, limit, messages, client): list_generator = list_pager.YieldFromList( service=client.projects_locations_functions, request=self.BuildRequest(location_ref, messages), limit=limit, field='functions', batch_size_attribute='pageSize') # Decorators (e.g. util.CatchHTTPErrorRaiseHTTPException) don't work # for generators. We have to catch the exception above the iteration loop, # but inside the function. try: for item in list_generator: yield item except api_exceptions.HttpError as error: msg = util.GetHttpErrorMessage(error) exceptions.reraise(base_exceptions.HttpException(msg))
def testErrorSuccessful(self): content = """{"error": {"details": [{"field": "some.field.name", "fieldViolations": [ {"description": "value 1"}, {"description": "value 2"}]}, {"field": "outer.field.name", "fieldViolations": [{"field": "inner.field.name", "description": "value 3"}]}]}}""" error = testutil.CreateTestHttpError(10, 'reason', content) result = util.GetHttpErrorMessage(error) # When multiple overlapping field names are provided at different depths: # one at the same level as the "fieldViolations" key (outer) and # one at the same level as a violation in the "fieldViolations" list (inner) # then the outer field name is used. self.assertEqual( result, '\n'.join([ 'ResponseError: status=[10], code=[reason], message=[', 'Problems:', 'some.field.name:', 'value 1', 'value 2', 'outer.field.name:', 'value 3', ']' ]))
def testGetHttpErrorMessage403(self): message = """\ { "error": { "code": 403, "message": "The caller does not have permission", "status": "PERMISSION_DENIED", "details": [ { "@type": "type.googleapis.com/google.rpc.DebugInfo", "detail": "[ORIGINAL ERROR] generic::permission_denied: More Details" } ] } } """ error = _MockHttpException(403, content=message) actual = util.GetHttpErrorMessage(error) expected = ('The caller does not have permission\nPermission Details:' '\n[ORIGINAL ERROR] generic::permission_denied: More Details') self.assertTrue(expected in actual)
def testErrorWithDetailsWithoutInnerDictionary(self): content = '{"error": {"details": {"field": "value"}}}' error = testutil.CreateTestHttpError(10, 'reason', content) result = util.GetHttpErrorMessage(error) self.assertEqual( 'ResponseError: status=[10], code=[reason], message=[]', result)
def testErrorWithStringDetails(self): content = '{"error": {"details": "field"}}' error = testutil.CreateTestHttpError(10, 'reason', content) result = util.GetHttpErrorMessage(error) self.assertEqual( 'ResponseError: status=[10], code=[reason], message=[]', result)
def testErrorWithoutJson(self): error = testutil.CreateTestHttpError(10, 'reason', 'invalid json') result = util.GetHttpErrorMessage(error) self.assertEqual( 'ResponseError: status=[10], code=[reason], message=[invalid json]', result)
def testGetHttpErrorMessageUtf8(self): message = _UNICODE_MESSAGE error = _MockHttpException(400, content=message.encode('utf8')) actual = util.GetHttpErrorMessage(error) self.assertTrue(message in actual)
def testGetHttpErrorMessageAscii(self): message = _ASCII_MESSAGE error = _MockHttpException(400, content=message) actual = util.GetHttpErrorMessage(error) self.assertTrue(message in actual)