mirror of
https://codeberg.org/Freedium-cfd/web.git
synced 2026-03-11 09:04:37 +00:00
Refactor
This commit is contained in:
parent
8aafabead7
commit
c08f4eacdb
3 changed files with 18 additions and 16 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
from fastapi.responses import HTMLResponse
|
from fastapi.responses import HTMLResponse
|
||||||
|
|
||||||
import sentry_sdk
|
|
||||||
import pickle
|
import pickle
|
||||||
from html5lib.html5parser import parse
|
from html5lib.html5parser import parse
|
||||||
from html5lib import serialize
|
from html5lib import serialize
|
||||||
|
|
@ -13,6 +12,7 @@ from server.utils.logger_trace import trace
|
||||||
from server.utils.notify import send_message
|
from server.utils.notify import send_message
|
||||||
from server.utils.cache import aio_redis_cache
|
from server.utils.cache import aio_redis_cache
|
||||||
from server.utils.utils import correct_url, safe_check_redis_connection
|
from server.utils.utils import correct_url, safe_check_redis_connection
|
||||||
|
from server.utils.exceptions import handle_exception
|
||||||
|
|
||||||
from medium_parser import medium_parser_exceptions
|
from medium_parser import medium_parser_exceptions
|
||||||
from medium_parser import cache as medium_cache
|
from medium_parser import cache as medium_cache
|
||||||
|
|
@ -33,8 +33,7 @@ async def render_postleter(limit: int = 30, as_html: bool = False):
|
||||||
post_metadata = await post.generate_metadata(as_dict=True)
|
post_metadata = await post.generate_metadata(as_dict=True)
|
||||||
outlet_posts_list.append(post_metadata)
|
outlet_posts_list.append(post_metadata)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logger.error(f"Couldn't render post_id for postleter: {post_id}, ex: {ex}")
|
await handle_exception(ex, message=f"Couldn't render post_id for postleter: {post_id}")
|
||||||
send_message(f"Couldn't render post_id for postleter: {post_id}, ex: {ex}")
|
|
||||||
|
|
||||||
postleter_template_rendered = await postleter_template.render_async(post_list=outlet_posts_list)
|
postleter_template_rendered = await postleter_template.render_async(post_list=outlet_posts_list)
|
||||||
if as_html:
|
if as_html:
|
||||||
|
|
@ -63,29 +62,22 @@ async def render_medium_post_link(path: str, use_cache: bool = True, use_redis:
|
||||||
else:
|
else:
|
||||||
rendered_medium_post = pickle.loads(redis_result)
|
rendered_medium_post = pickle.loads(redis_result)
|
||||||
except medium_parser_exceptions.InvalidURL as ex:
|
except medium_parser_exceptions.InvalidURL as ex:
|
||||||
logger.exception(ex)
|
return await handle_exception(ex,
|
||||||
sentry_sdk.capture_exception(ex)
|
|
||||||
return await generate_error(
|
|
||||||
"Unable to identify the Medium article URL.",
|
"Unable to identify the Medium article URL.",
|
||||||
status_code=404,
|
status_code=404,
|
||||||
)
|
)
|
||||||
except (medium_parser_exceptions.InvalidMediumPostURL, medium_parser_exceptions.MediumPostQueryError, medium_parser_exceptions.PageLoadingError) as ex:
|
except (medium_parser_exceptions.InvalidMediumPostURL, medium_parser_exceptions.MediumPostQueryError, medium_parser_exceptions.PageLoadingError) as ex:
|
||||||
logger.exception(ex)
|
return await handle_exception(
|
||||||
sentry_sdk.capture_exception(ex)
|
ex,
|
||||||
return await generate_error(
|
|
||||||
"Unable to identify the link as a Medium.com article page. Please check the URL for any typing errors.",
|
"Unable to identify the link as a Medium.com article page. Please check the URL for any typing errors.",
|
||||||
status_code=404,
|
status_code=404,
|
||||||
)
|
)
|
||||||
except medium_parser_exceptions.InvalidMediumPostID as ex:
|
except medium_parser_exceptions.InvalidMediumPostID as ex:
|
||||||
logger.exception(ex)
|
return await handle_exception(ex, "Unable to identify the Medium article ID.", status_code=500)
|
||||||
sentry_sdk.capture_exception(ex)
|
|
||||||
return await generate_error("Unable to identify the Medium article ID.", status_code=500)
|
|
||||||
except medium_parser_exceptions.NotValidMediumURL as ex:
|
except medium_parser_exceptions.NotValidMediumURL as ex:
|
||||||
return await generate_error("You sure that this is a valid Medium.com URL?", status_code=404, quiet=True)
|
return await handle_exception("You sure that this is a valid Medium.com URL?", status_code=404, quiet=True)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logger.exception(ex)
|
return await handle_exception(ex, status_code=500)
|
||||||
sentry_sdk.capture_exception(ex)
|
|
||||||
return await generate_error(status_code=500)
|
|
||||||
else:
|
else:
|
||||||
base_context = {
|
base_context = {
|
||||||
"enable_ads_header": config.ENABLE_ADS_BANNER,
|
"enable_ads_header": config.ENABLE_ADS_BANNER,
|
||||||
|
|
|
||||||
0
server/utils/__init__.py
Normal file
0
server/utils/__init__.py
Normal file
10
server/utils/exceptions.py
Normal file
10
server/utils/exceptions.py
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
import sentry_sdk
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
|
from server.utils.error import generate_error
|
||||||
|
|
||||||
|
|
||||||
|
async def handle_exception(ex, message="An error occurred", status_code=500):
|
||||||
|
logger.exception(ex)
|
||||||
|
sentry_sdk.capture_exception(ex)
|
||||||
|
return await generate_error(message, status_code=status_code)
|
||||||
Loading…
Reference in a new issue