| Total Complexity | 4 |
| Total Lines | 45 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import logging |
||
| 2 | import typing |
||
| 3 | |||
| 4 | from . import errors, protocol |
||
| 5 | |||
| 6 | |||
| 7 | __all__ = ( |
||
| 8 | 'exception_middleware', |
||
| 9 | 'extra_args_middleware', |
||
| 10 | 'DEFAULT_MIDDLEWARES', |
||
| 11 | ) |
||
| 12 | |||
| 13 | logger = logging.getLogger(__name__) |
||
| 14 | |||
| 15 | |||
| 16 | async def extra_args_middleware(request: protocol.JsonRpcRequest, handler: typing.Callable) -> protocol.JsonRpcResponse: |
||
| 17 | request.extra_args['rpc_request'] = request |
||
| 18 | return await handler(request) |
||
| 19 | |||
| 20 | |||
| 21 | async def exception_middleware(request: protocol.JsonRpcRequest, handler: typing.Callable) -> protocol.JsonRpcResponse: |
||
| 22 | try: |
||
| 23 | response = await handler(request) |
||
| 24 | except errors.JsonRpcError as e: |
||
| 25 | logging.warning('Unprocessed errors.JsonRpcError', exc_info=True) |
||
| 26 | response = protocol.JsonRpcResponse( |
||
| 27 | msg_id=request.msg_id, |
||
| 28 | jsonrpc=request.jsonrpc, |
||
| 29 | error=e, |
||
| 30 | ) |
||
| 31 | except Exception as e: |
||
| 32 | logger.exception(e) |
||
| 33 | response = protocol.JsonRpcResponse( |
||
| 34 | msg_id=request.msg_id, |
||
| 35 | jsonrpc=request.jsonrpc, |
||
| 36 | error=errors.InternalError().with_traceback(), |
||
| 37 | ) |
||
| 38 | |||
| 39 | return response |
||
| 40 | |||
| 41 | |||
| 42 | DEFAULT_MIDDLEWARES = ( |
||
| 43 | exception_middleware, |
||
| 44 | extra_args_middleware, |
||
| 45 | ) |
||
| 46 |