async def wrapped(*positional, **named): try: return await func(*positional, **named) except AMQPError: return response_error(ERROR_RABBITMQ_EXCEPTION, get_raised_error(), default_logger='rabbitmq') except: return response_error(ERROR_INTERNAL_EXCEPTION, get_raised_error())
async def gen_html(report_name, params, jwt, **options): logger = logging.getLogger('report') if report_name in pkg.env.list_templates(): template = pkg.env.get_template(report_name) report = ReportLoader(report_name, params, jwt) title = report.get_title() logger.info(f'Get data for report \'{report_name}\' ({title})') data = await report.get_data(**options) if 'error' in data: return response_error(data['error']['code'], data['error']['message']) rendered = await template.render_async(title=title, data=data) logger.info(f'Rendered HTML:\n{rendered}\n') return {'result': rendered, 'content-type': 'text/html'} else: return response_error(ERROR_REPORT_NOT_FOUND, f'Report \'{report_name}\' doesn\'t exists')
def process_rpc_result(rpc_result): resp = None send_resp_unknown = False if type(rpc_result) == dict: content_type = rpc_result[ 'content-type'] if 'content-type' in rpc_result else 'text/plain' body = rpc_result['result'] if 'result' in rpc_result else '' error = rpc_result['error'] if 'error' in rpc_result else None if error: resp = response_error(error['code'], error['message'], default_logger='rabbitmq') else: if content_type == 'text/html': resp = response.html(body) elif content_type == 'text/plain': resp = response.text(body) elif content_type == 'application/json': resp = response.json(body) elif content_type == 'application/pdf_base64': raw_pdf = None try: raw_pdf = b64decode(body, validate=True) except: pass resp = HTTPResponse(content_type='application/pdf', body_bytes=raw_pdf) else: send_resp_unknown = True elif type(rpc_result) == HTTPResponse: resp = rpc_result else: send_resp_unknown = True if send_resp_unknown: return response_error(ERROR_RABBITMQ_UNKNOWN_ANSWER_FORMAT, 'Неизвестный формат ответа от RabbitMQ', default_logger='rabbitmq') logger = logging.getLogger('rabbitmq') logger.info('RPC answer:\n<see http response>\n') return resp
async def rpc_call(self, method, args): if self._rpc: # TODO: добавить уникальный ID запроса logger = logging.getLogger('rabbitmq') a = {e: args[e] for e in args if e != 'jwt'} logger.info(f'Send RPC request:\nMETHOD:\t{method}\nARGS:\t{a}\n') return await self._rpc.call(method, kwargs=args) else: return response_error(ERROR_RABBITMQ_NOT_AVAIBLE, 'Генерация отчётов недоступна', default_logger='rabbitmq')
async def http_exceptions_handler(request: Request, exc: CustomException): return response_error(ERROR_CUSTOM_EXCEPTION, str(exc.detail), HTTP_400_BAD_REQUEST)
async def http_exceptions_handler(request: Request, exc: RequestValidationError): return response_error(ERROR_UNHANDLED_EXCEPTION, str(exc), HTTP_400_BAD_REQUEST, log_stacktrace=False)
async def custom_http_exceptions_handler(request: Request, exc: CustomHTTPException): return response_error(exc.error_code, exc.detail, exc.http_error_code, log_stacktrace=False)
async def http_exceptions_handler(request: Request, exc: HTTPException): return response_error(exc.status_code, str(exc.detail), exc.status_code, log_stacktrace=False)
async def unhandled_exceptions_handler(request: Request, exc: Exception): return response_error(ERROR_UNHANDLED_EXCEPTION, str(exc))
def error_handler(request, exception): return response_error(exception.status_code, str(exception), exception.status_code)
def validate(self, json_object, schema_name): try: self.validate_and_raise_error(json_object, schema_name) except ValidationError as e: return response_error(ERROR_SCHEMA_VALIDATION, e.message)