mirror of
https://github.com/sprockets/sprockets-postgres.git
synced 2024-11-14 11:19:27 +00:00
Add on_postgres_error and on_postgres_timing to public API of RequestHandlerMixin
The methods are intended to be overrideable, but the leading underscore convention conflicts with Python practice. See https://docs.python.org/3.8/tutorial/classes.html#private-variables
This commit is contained in:
parent
69b806f9b3
commit
fbb7b4dd9a
2 changed files with 25 additions and 25 deletions
|
@ -602,8 +602,8 @@ class RequestHandlerMixin:
|
|||
|
||||
"""
|
||||
async with self.application.postgres_connector(
|
||||
self._on_postgres_error,
|
||||
self._on_postgres_timing,
|
||||
self.on_postgres_error,
|
||||
self.on_postgres_timing,
|
||||
timeout) as connector:
|
||||
return await connector.callproc(
|
||||
name, parameters, metric_name, timeout=timeout)
|
||||
|
@ -641,8 +641,8 @@ class RequestHandlerMixin:
|
|||
|
||||
"""
|
||||
async with self.application.postgres_connector(
|
||||
self._on_postgres_error,
|
||||
self._on_postgres_timing,
|
||||
self.on_postgres_error,
|
||||
self.on_postgres_timing,
|
||||
timeout) as connector:
|
||||
return await connector.execute(
|
||||
sql, parameters, metric_name, timeout=timeout)
|
||||
|
@ -688,13 +688,13 @@ class RequestHandlerMixin:
|
|||
|
||||
"""
|
||||
async with self.application.postgres_connector(
|
||||
self._on_postgres_error,
|
||||
self._on_postgres_timing,
|
||||
self.on_postgres_error,
|
||||
self.on_postgres_timing,
|
||||
timeout) as connector:
|
||||
async with connector.transaction():
|
||||
yield connector
|
||||
|
||||
def _on_postgres_error(self,
|
||||
def on_postgres_error(self,
|
||||
metric_name: str,
|
||||
exc: Exception) -> typing.Optional[Exception]:
|
||||
"""Override for different error handling behaviors
|
||||
|
@ -716,7 +716,7 @@ class RequestHandlerMixin:
|
|||
raise web.HTTPError(500, reason='Database Error')
|
||||
return exc
|
||||
|
||||
def _on_postgres_timing(self,
|
||||
def on_postgres_timing(self,
|
||||
metric_name: str,
|
||||
duration: float) -> None:
|
||||
"""Override for custom metric recording. As a default behavior it will
|
||||
|
|
10
tests.py
10
tests.py
|
@ -69,7 +69,7 @@ class ErrorRequestHandler(RequestHandler):
|
|||
await self.postgres_execute(self.GET_SQL)
|
||||
self.set_status(204)
|
||||
|
||||
def _on_postgres_error(self,
|
||||
def on_postgres_error(self,
|
||||
metric_name: str,
|
||||
exc: Exception) -> typing.Optional[Exception]:
|
||||
return RuntimeError()
|
||||
|
@ -78,7 +78,7 @@ class ErrorRequestHandler(RequestHandler):
|
|||
class ErrorPassthroughRequestHandler(RequestHandler):
|
||||
|
||||
async def get(self):
|
||||
exc = self._on_postgres_error('test', RuntimeError())
|
||||
exc = self.on_postgres_error('test', RuntimeError())
|
||||
if isinstance(exc, RuntimeError):
|
||||
self.set_status(204)
|
||||
else:
|
||||
|
@ -156,7 +156,7 @@ class MultiRowRequestHandler(RequestHandler):
|
|||
|
||||
class NoErrorRequestHandler(ErrorRequestHandler):
|
||||
|
||||
def _on_postgres_error(self,
|
||||
def on_postgres_error(self,
|
||||
metric_name: str,
|
||||
exc: Exception) -> typing.Optional[Exception]:
|
||||
return None
|
||||
|
@ -228,7 +228,7 @@ class TimeoutErrorRequestHandler(RequestHandler):
|
|||
await self.postgres_execute(self.GET_SQL)
|
||||
raise web.HTTPError(500, 'This should have failed')
|
||||
|
||||
def _on_postgres_error(self,
|
||||
def on_postgres_error(self,
|
||||
metric_name: str,
|
||||
exc: Exception) -> typing.Optional[Exception]:
|
||||
"""Override for different error handling behaviors
|
||||
|
@ -253,7 +253,7 @@ class UnhandledExceptionRequestHandler(RequestHandler):
|
|||
raise web.HTTPError(422)
|
||||
raise web.HTTPError(500, 'This should have failed')
|
||||
|
||||
def _on_postgres_error(self,
|
||||
def on_postgres_error(self,
|
||||
metric_name: str,
|
||||
exc: Exception) -> typing.Optional[Exception]:
|
||||
"""Override for different error handling behaviors
|
||||
|
|
Loading…
Reference in a new issue