Skip to content

Commit

Permalink
Merge pull request #26 from fzxt/add_request_to_on_error
Browse files Browse the repository at this point in the history
[breaking] Pass pyramid request to on_error lifecycle hook
  • Loading branch information
fahad ahmad authored Sep 24, 2019
2 parents 54edbc3 + 38dedb5 commit 1734c03
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
6 changes: 3 additions & 3 deletions pyramid_hypernova/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _parse_response(self, response_json):
message=result['error']['message'],
stack=result['error']['stack'],
)
self.plugin_controller.on_error(error, {identifier: job})
self.plugin_controller.on_error(error, {identifier: job}, self.pyramid_request)

html = result['html']
if not html:
Expand Down Expand Up @@ -117,7 +117,7 @@ def process_responses(self, query, jobs):
stack=response_json['error']['stack'],
)
pyramid_response = create_fallback_response(jobs, True, self.json_encoder, error)
self.plugin_controller.on_error(error, jobs)
self.plugin_controller.on_error(error, jobs, self.pyramid_request)
else:
pyramid_response = self._parse_response(response_json)
self.plugin_controller.on_success(pyramid_response, jobs)
Expand All @@ -131,7 +131,7 @@ def process_responses(self, query, jobs):
str(e),
[line.rstrip('\n') for line in traceback.format_tb(exc_traceback)],
)
self.plugin_controller.on_error(error, jobs)
self.plugin_controller.on_error(error, jobs, self.pyramid_request)
pyramid_response = create_fallback_response(jobs, True, self.json_encoder, error)

return pyramid_response
Expand Down
8 changes: 5 additions & 3 deletions pyramid_hypernova/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,16 @@ def on_success(self, response, jobs):
for plugin in self.plugins:
plugin.on_success(response, jobs)

def on_error(self, err, jobs):
def on_error(self, err, jobs, request):
"""An event type function that is called whenever any error is
encountered
:type err: dict
:type jobs: Dict[str, Job]
:type request: a Pyramid request object
"""
for plugin in self.plugins:
plugin.on_error(err, jobs)
plugin.on_error(err, jobs, request)


class BasePlugin(object):
Expand Down Expand Up @@ -191,10 +192,11 @@ def on_success(self, response, jobs):
:type jobs: Dict[str, Job]
"""

def on_error(self, err, jobs):
def on_error(self, err, jobs, request):
"""An event type function that is called whenever any error is
encountered
:type err: dict
:type jobs: Dict[str, Job]
:type request: a Pyramid request object
"""
2 changes: 2 additions & 0 deletions tests/batch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ def test_calls_on_error(self, spy_plugin_controller, test_data, batch_request, m
stack=[],
),
batch_request.jobs,
batch_request.pyramid_request
)

def test_calls_on_error_on_unhealthy_service(
Expand Down Expand Up @@ -495,4 +496,5 @@ def test_calls_on_error_on_unhealthy_service(
stack=['Traceback:', ' foo:'],
),
batch_request.jobs,
batch_request.pyramid_request
)
8 changes: 5 additions & 3 deletions tests/plugins_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@ def test_on_success(self, plugins, plugin_controller):
def test_on_error(self, plugins, plugin_controller):
err = mock.Mock()
jobs = mock.Mock()
plugin_controller.on_error(err, jobs)
plugins[0].on_error.assert_called_once_with(err, jobs)
plugins[1].on_error.assert_called_once_with(err, jobs)
pyramid_request = mock.Mock()

plugin_controller.on_error(err, jobs, pyramid_request)
plugins[0].on_error.assert_called_once_with(err, jobs, pyramid_request)
plugins[1].on_error.assert_called_once_with(err, jobs, pyramid_request)


class TestBasePlugin(object):
Expand Down

0 comments on commit 1734c03

Please sign in to comment.