Skip to content

Commit

Permalink
Catch the correct exception
Browse files Browse the repository at this point in the history
Catch the correct exception to fill the response code.
Added test case to test this
Also fixed a minor sandesh file issue.

Change-Id: Id4147ee2034231946f8d1aa051b8c6a7b6064da0
Closes-Bug: 1639079
  • Loading branch information
Sachin Bansal committed Nov 4, 2016
1 parent a8c2c5a commit 4369c87
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/config/api-server/tests/test_crud_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4064,6 +4064,30 @@ def test_exclude_types_not_cached(self):
# end class TestCacheWithMetadataExcludeTypes


class TestVncApiStats(test_case.ApiServerTestCase):
from cfgm_common.vnc_api_stats import log_api_stats
_sandesh = None
@log_api_stats
def _sample_function(self, obj_type):
raise cfgm_common.exceptions.HttpError(409, '')

def _check_sendwith(self, sandesh, stats, *args):
self.assertEqual(stats.response_code, 409)
self.assertEqual(stats.obj_type, 'abc')

def test_response_code_on_exception(self):
from cfgm_common.vnc_api_stats import VncApiStatistics
try:
with test_common.patch(VncApiStatistics, 'sendwith', self._check_sendwith):
self._sample_function('abc')
except cfgm_common.exceptions.HttpError:
pass
else:
self.assertThat(0, 'Expecting HttpError to be raised, but was not raised')
# end test_response_code_on_exception
# end TestVncApiStats


if __name__ == '__main__':
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
Expand Down
6 changes: 3 additions & 3 deletions src/config/common/vnc_api_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from uve.vnc_api.ttypes import VncApiStats, VncApiStatsLog
from pysandesh.gen_py.sandesh.ttypes import SandeshLevel

from exceptions import HttpError

def log_api_stats(func):
def wrapper(api_server_obj, resource_type, *args, **kwargs):
Expand All @@ -18,8 +18,8 @@ def wrapper(api_server_obj, resource_type, *args, **kwargs):
statistics.response_size = len(str(response))
statistics.response_code = bottle.response.status_code
return response
except bottle.HTTPError as err_response:
statistics.response_size = len(err_response.body)
except HttpError as err_response:
statistics.response_size = len(str(err_response))
statistics.response_code = err_response.status_code
raise
finally:
Expand Down
2 changes: 1 addition & 1 deletion src/config/schema-transformer/st_introspect.sandesh
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ request sandesh StObjectReq {
}

struct RefList {
1: string object_type (link="StObject");
1: string object_type (link="StObjectReq");
2: list<string> object_fq_name;
}

Expand Down

0 comments on commit 4369c87

Please sign in to comment.