exc-mw
All checks were successful
Deploy on push / deploy (push) Successful in 1m8s

This commit is contained in:
Untone 2024-06-04 08:10:57 +03:00
parent 231de135ca
commit d53256bcd7
4 changed files with 23 additions and 12 deletions

View File

@ -21,7 +21,6 @@ def query_shouts():
select(Shout) select(Shout)
.options(joinedload(Shout.authors), joinedload(Shout.topics)) .options(joinedload(Shout.authors), joinedload(Shout.topics))
.where(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None))) .where(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None)))
) )
@ -314,7 +313,9 @@ async def load_shouts_search(_, _info, text, limit=50, offset=0):
if result: if result:
logger.debug(result) logger.debug(result)
logger.debug(len(result)) logger.debug(len(result))
for [shout,] in result: for [
shout,
] in result:
# logger.debug(shout) # logger.debug(shout)
shout.score = scores[f"{shout.id}"] shout.score = scores[f"{shout.id}"]
shouts.append(shout) shouts.append(shout)

17
services/exception.py Normal file
View File

@ -0,0 +1,17 @@
import logging
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import JSONResponse
logger = logging.getLogger("exception")
logging.basicConfig(level=logging.DEBUG)
class ExceptionHandlerMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request, call_next):
try:
response = await call_next(request)
return response
except Exception as exc:
logger.exception(exc)
return JSONResponse({"detail": "An error occurred. Please try again later."}, status_code=500)

View File

@ -66,15 +66,7 @@ root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG) root_logger.setLevel(logging.DEBUG)
root_logger.addHandler(stream) root_logger.addHandler(stream)
ignore_logs = [ ignore_logs = ["_trace", "httpx", "_client", "_trace.atrace", "aiohttp", "_client", "base"]
"_trace",
"httpx",
"_client",
"_trace.atrace",
"aiohttp",
"_client",
"base"
]
for lgr in ignore_logs: for lgr in ignore_logs:
loggr = logging.getLogger(lgr) loggr = logging.getLogger(lgr)
loggr.setLevel(logging.INFO) loggr.setLevel(logging.INFO)

View File

@ -180,7 +180,8 @@ class SearchService:
size=limit, size=limit,
from_=offset, from_=offset,
_source=False, _source=False,
_source_excludes=["title", "body", "subtitle", "media", "lead", "_index"]) _source_excludes=["title", "body", "subtitle", "media", "lead", "_index"],
)
hits = search_response["hits"]["hits"] hits = search_response["hits"]["hits"]
results = [{"id": hit["_id"], "score": hit["_score"]} for hit in hits] results = [{"id": hit["_id"], "score": hit["_score"]} for hit in hits]