def on_new_request(event): request = event.request # Save the time the request was received by the server. event.request._received_at = utils.msec_time() try: # Pyramid fails if the URL contains invalid UTF-8 characters. request_path = event.request.path except UnicodeDecodeError: raise errors.http_error( HTTPBadRequest(), errno=errors.ERRORS.INVALID_PARAMETERS, message="Invalid URL path.", ) request.log_context( agent=request.headers.get("User-Agent"), path=request_path, method=request.method, lang=request.headers.get("Accept-Language"), errno=0, ) qs = dict(errors.request_GET(request)) if qs: request.log_context(querystring=qs) if summary_logger.level == logging.DEBUG: request.log_context(headers=dict(request.headers), body=request.body)
def on_new_request(event): request = event.request # Save the time the request was received by the server. event.request._received_at = utils.msec_time() try: # Pyramid fails if the URL contains invalid UTF-8 characters. request_path = event.request.path except UnicodeDecodeError: raise errors.http_error( HTTPBadRequest(), errno=errors.ERRORS.INVALID_PARAMETERS, message='Invalid URL path.') request.log_context(agent=request.headers.get('User-Agent'), path=request_path, method=request.method, lang=request.headers.get('Accept-Language'), errno=0) qs = dict(errors.request_GET(request)) if qs: request.log_context(querystring=qs) if summary_logger.level == logging.DEBUG: request.log_context(headers=dict(request.headers), body=request.body)
def _next_page_url(self, sorting, limit, last_record, offset): """Build the Next-Page header from where we stopped.""" token = self._build_pagination_token(sorting, last_record, offset) params = {**request_GET(self.request), '_limit': limit, '_token': token} service = self.request.current_service next_page_url = self.request.route_url(service.name, _query=params, **self.request.matchdict) return next_page_url
def error(context, request): """Catch server errors and trace them.""" if isinstance(context, httpexceptions.Response): return reapply_cors(request, context) if isinstance(context, storage_exceptions.IntegrityError): error_msg = 'Integrity constraint violated, please retry.' response = http_error(httpexceptions.HTTPConflict(), errno=ERRORS.CONSTRAINT_VIOLATED, message=error_msg) retry_after = request.registry.settings['retry_after_seconds'] response.headers['Retry-After'] = str(retry_after) return reapply_cors(request, response) # Log some information about current request. extra = { 'path': request.path, 'method': request.method, } qs = dict(request_GET(request)) if qs: extra['querystring'] = qs # Take errno from original exception, or undefined if unknown/unhandled. try: extra['errno'] = context.errno.value except AttributeError: extra['errno'] = ERRORS.UNDEFINED.value if isinstance(context, storage_exceptions.BackendError): logger.critical(context.original, extra=extra, exc_info=context) response = httpexceptions.HTTPServiceUnavailable() return service_unavailable(response, request) # Within the exception view, sys.exc_info() will return null. # see https://github.com/python/cpython/blob/ce9e62544/Lib/logging/__init__.py#L1460-L1462 logger.error(context, extra=extra, exc_info=context) error_msg = 'A programmatic error occured, developers have been informed.' info = request.registry.settings['error_info_link'] response = http_error(httpexceptions.HTTPInternalServerError(), message=error_msg, info=info) return reapply_cors(request, response)
def on_new_request(event): request = event.request # Save the time the request was received by the server. event.request._received_at = utils.msec_time() try: # Pyramid fails if the URL contains invalid UTF-8 characters. request_path = event.request.path except UnicodeDecodeError: raise errors.http_error(HTTPBadRequest(), errno=errors.ERRORS.INVALID_PARAMETERS, message='Invalid URL path.') request.log_context(agent=request.headers.get('User-Agent'), path=request_path, method=request.method, querystring=dict(errors.request_GET(request)), lang=request.headers.get('Accept-Language'), uid=None, authn_type=None, errno=None)