Run Black and use exc_info in logger.error
This commit is contained in:
@ -23,7 +23,13 @@ Exceptions:
|
|||||||
|
|
||||||
from discord_webhook import DiscordWebhook
|
from discord_webhook import DiscordWebhook
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from reader import FeedExistsError, FeedNotFoundError, InvalidFeedURLError, ParseError, StorageError
|
from reader import (
|
||||||
|
FeedExistsError,
|
||||||
|
FeedNotFoundError,
|
||||||
|
InvalidFeedURLError,
|
||||||
|
ParseError,
|
||||||
|
StorageError,
|
||||||
|
)
|
||||||
from requests import Response
|
from requests import Response
|
||||||
|
|
||||||
from discord_rss_bot.settings import logger, reader
|
from discord_rss_bot.settings import logger, reader
|
||||||
@ -39,6 +45,7 @@ class IfFeedError(BaseModel):
|
|||||||
err_msg: The error message, if any.
|
err_msg: The error message, if any.
|
||||||
exception: The exception, if any.
|
exception: The exception, if any.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
feed_url: str
|
feed_url: str
|
||||||
webhook: str
|
webhook: str
|
||||||
error: bool
|
error: bool
|
||||||
@ -75,13 +82,25 @@ def add_feed(feed_url: str, webhook: str, exist_ok=False, allow_invalid_url=Fals
|
|||||||
reader.add_feed(feed=feed_url, exist_ok=exist_ok, allow_invalid_url=allow_invalid_url)
|
reader.add_feed(feed=feed_url, exist_ok=exist_ok, allow_invalid_url=allow_invalid_url)
|
||||||
except FeedExistsError as error:
|
except FeedExistsError as error:
|
||||||
error_msg = "Feed already exists"
|
error_msg = "Feed already exists"
|
||||||
logger.error(f"{error_msg}: {error}")
|
logger.error(error_msg, exc_info=True)
|
||||||
return IfFeedError(error=True, err_msg=error_msg, feed_url=feed_url, webhook=webhook, exception=error.message)
|
return IfFeedError(
|
||||||
|
error=True,
|
||||||
|
err_msg=error_msg,
|
||||||
|
feed_url=feed_url,
|
||||||
|
webhook=webhook,
|
||||||
|
exception=error.message,
|
||||||
|
)
|
||||||
|
|
||||||
except InvalidFeedURLError as error:
|
except InvalidFeedURLError as error:
|
||||||
error_msg = "Invalid feed URL"
|
error_msg = "Invalid feed URL"
|
||||||
logger.error(f"{error_msg}: {error}")
|
logger.error(error_msg, exc_info=True)
|
||||||
return IfFeedError(error=True, err_msg=error_msg, feed_url=feed_url, webhook=webhook, exception=error.message)
|
return IfFeedError(
|
||||||
|
error=True,
|
||||||
|
err_msg=error_msg,
|
||||||
|
feed_url=feed_url,
|
||||||
|
webhook=webhook,
|
||||||
|
exception=error.message,
|
||||||
|
)
|
||||||
|
|
||||||
return IfFeedError(error=False, feed_url=feed_url, webhook=webhook)
|
return IfFeedError(error=False, feed_url=feed_url, webhook=webhook)
|
||||||
|
|
||||||
@ -132,14 +151,14 @@ def send_to_discord(entry) -> Response:
|
|||||||
reader.set_entry_read(entry)
|
reader.set_entry_read(entry)
|
||||||
logger.debug(f"New entry: {entry.title}")
|
logger.debug(f"New entry: {entry.title}")
|
||||||
|
|
||||||
webhook_url = reader.get_tag(entry.feed.url, "webhook")
|
webhook_url = str(reader.get_tag(entry.feed.url, "webhook"))
|
||||||
if not webhook_url:
|
if not webhook_url:
|
||||||
logger.error(f"No webhook found for feed: {entry.feed.url}")
|
logger.error(f"No webhook found for feed: {entry.feed.url}")
|
||||||
raise NoWebhookFoundError(f"No webhook found for feed: {entry.feed.url}")
|
raise NoWebhookFoundError(f"No webhook found for feed: {entry.feed.url}")
|
||||||
|
|
||||||
logger.debug(f"Sending to webhook: {webhook_url}")
|
logger.debug(f"Sending to webhook: {webhook_url}")
|
||||||
webhook = DiscordWebhook(url=str(webhook_url), content=f":robot: :mega: New entry: {entry.title}\n"
|
webhook_message = f":robot: :mega: {entry.title}\n{entry.link}"
|
||||||
f"{entry.link}", rate_limit_retry=True)
|
webhook = DiscordWebhook(url=webhook_url, content=webhook_message, rate_limit_retry=True)
|
||||||
response = webhook.execute()
|
response = webhook.execute()
|
||||||
if not response.ok:
|
if not response.ok:
|
||||||
logger.error(f"Error: {response.status_code} {response.reason}")
|
logger.error(f"Error: {response.status_code} {response.reason}")
|
||||||
@ -164,16 +183,34 @@ def update_feed(feed_url: str, webhook: str) -> IfFeedError:
|
|||||||
except FeedNotFoundError as error:
|
except FeedNotFoundError as error:
|
||||||
error_msg = "Feed not found"
|
error_msg = "Feed not found"
|
||||||
logger.error(error_msg, exc_info=True)
|
logger.error(error_msg, exc_info=True)
|
||||||
return IfFeedError(error=True, err_msg=error_msg, feed_url=feed_url, webhook=webhook, exception=error.message)
|
return IfFeedError(
|
||||||
|
error=True,
|
||||||
|
err_msg=error_msg,
|
||||||
|
feed_url=feed_url,
|
||||||
|
webhook=webhook,
|
||||||
|
exception=error.message,
|
||||||
|
)
|
||||||
|
|
||||||
except ParseError as error:
|
except ParseError as error:
|
||||||
error_msg = "An error occurred while getting/parsing feed"
|
error_msg = "An error occurred while getting/parsing feed"
|
||||||
logger.error(error_msg, exc_info=True)
|
logger.error(error_msg, exc_info=True)
|
||||||
return IfFeedError(error=True, err_msg=error_msg, feed_url=feed_url, webhook=webhook, exception=error.message)
|
return IfFeedError(
|
||||||
|
error=True,
|
||||||
|
err_msg=error_msg,
|
||||||
|
feed_url=feed_url,
|
||||||
|
webhook=webhook,
|
||||||
|
exception=error.message,
|
||||||
|
)
|
||||||
|
|
||||||
except StorageError as error:
|
except StorageError as error:
|
||||||
error_msg = "An exception was raised by the underlying storage"
|
error_msg = "An exception was raised by the underlying storage"
|
||||||
logger.error(error_msg, exc_info=True)
|
logger.error(error_msg, exc_info=True)
|
||||||
return IfFeedError(error=True, err_msg=error_msg, feed_url=feed_url, webhook=webhook, exception=error.message)
|
return IfFeedError(
|
||||||
|
error=True,
|
||||||
|
err_msg=error_msg,
|
||||||
|
feed_url=feed_url,
|
||||||
|
webhook=webhook,
|
||||||
|
exception=error.message,
|
||||||
|
)
|
||||||
|
|
||||||
return IfFeedError(error=False, feed_url=feed_url, webhook=webhook)
|
return IfFeedError(error=False, feed_url=feed_url, webhook=webhook)
|
||||||
|
@ -80,12 +80,19 @@ async def create_feed(feed_url: str = Form(), webhook_dropdown: str = Form()):
|
|||||||
added_feed = add_feed(feed_url, webhook_dropdown)
|
added_feed = add_feed(feed_url, webhook_dropdown)
|
||||||
|
|
||||||
if updated_feed.error or added_feed.error:
|
if updated_feed.error or added_feed.error:
|
||||||
error_dict = {"error": updated_feed.error, "feed": updated_feed.feed_url, "webhook": updated_feed.webhook,
|
error_dict = {
|
||||||
"exception": updated_feed.exception}
|
"error": updated_feed.error,
|
||||||
|
"feed": updated_feed.feed_url,
|
||||||
|
"webhook": updated_feed.webhook,
|
||||||
|
"exception": updated_feed.exception,
|
||||||
|
}
|
||||||
return HTTPException(status_code=500, detail=error_dict)
|
return HTTPException(status_code=500, detail=error_dict)
|
||||||
|
|
||||||
# Check if set_hook_by_name() was successful.
|
# Check if set_hook_by_name() was successful.
|
||||||
if isinstance(set_hook_by_name(name=webhook_dropdown, feed_url=feed_url), ResourceNotFoundError):
|
if isinstance(
|
||||||
|
set_hook_by_name(name=webhook_dropdown, feed_url=feed_url),
|
||||||
|
ResourceNotFoundError,
|
||||||
|
):
|
||||||
return set_hook_by_name(name=webhook_dropdown, feed_url=feed_url)
|
return set_hook_by_name(name=webhook_dropdown, feed_url=feed_url)
|
||||||
|
|
||||||
new_tag = reader.get_tag(feed_url, "webhook")
|
new_tag = reader.get_tag(feed_url, "webhook")
|
||||||
@ -110,7 +117,7 @@ def create_list_of_webhooks():
|
|||||||
@app.get("/favicon.ico", include_in_schema=False)
|
@app.get("/favicon.ico", include_in_schema=False)
|
||||||
async def favicon():
|
async def favicon():
|
||||||
"""Return favicon."""
|
"""Return favicon."""
|
||||||
return FileResponse('static/favicon.ico')
|
return FileResponse("static/favicon.ico")
|
||||||
|
|
||||||
|
|
||||||
@app.get("/add", response_class=HTMLResponse)
|
@app.get("/add", response_class=HTMLResponse)
|
||||||
@ -183,11 +190,13 @@ def make_context_index(request) -> dict:
|
|||||||
|
|
||||||
feed_count = reader.get_feed_counts()
|
feed_count = reader.get_feed_counts()
|
||||||
entry_count = reader.get_entry_counts()
|
entry_count = reader.get_entry_counts()
|
||||||
context = {"request": request,
|
context = {
|
||||||
|
"request": request,
|
||||||
"feeds": feed_list,
|
"feeds": feed_list,
|
||||||
"feed_count": feed_count,
|
"feed_count": feed_count,
|
||||||
"entry_count": entry_count,
|
"entry_count": entry_count,
|
||||||
"webhooks": hooks}
|
"webhooks": hooks,
|
||||||
|
}
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
@ -211,7 +220,7 @@ async def remove_feed(request: Request, feed_url: str = Form()):
|
|||||||
return templates.TemplateResponse("index.html", {"request": request, "feed": feed})
|
return templates.TemplateResponse("index.html", {"request": request, "feed": feed})
|
||||||
|
|
||||||
|
|
||||||
@app.on_event('startup')
|
@app.on_event("startup")
|
||||||
def startup():
|
def startup():
|
||||||
"""This is called when the server starts.
|
"""This is called when the server starts.
|
||||||
|
|
||||||
|
@ -39,7 +39,9 @@ def create_settings_file(settings_file) -> None:
|
|||||||
# Both options are commented out by default.
|
# Both options are commented out by default.
|
||||||
webhooks = table()
|
webhooks = table()
|
||||||
webhooks.add(comment('"First webhook" = "https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz"'))
|
webhooks.add(comment('"First webhook" = "https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz"'))
|
||||||
webhooks.add(comment('"Second webhook" = "https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz"'))
|
webhooks.add(
|
||||||
|
comment('"Second webhook" = "https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz"')
|
||||||
|
)
|
||||||
|
|
||||||
# [database]
|
# [database]
|
||||||
# Option is commented out by default.
|
# Option is commented out by default.
|
||||||
|
@ -19,6 +19,8 @@ def set_hook_by_name(name: str, feed_url: str) -> None or HTTPException:
|
|||||||
webhook_url = settings["webhooks"][name]
|
webhook_url = settings["webhooks"][name]
|
||||||
try:
|
try:
|
||||||
reader.set_tag(feed_url, "webhook", webhook_url)
|
reader.set_tag(feed_url, "webhook", webhook_url)
|
||||||
|
|
||||||
except ResourceNotFoundError as e:
|
except ResourceNotFoundError as e:
|
||||||
logger.error(f"ResourceNotFoundError: {e}")
|
error_msg = f"ResourceNotFoundError: Could not set webhook: {e}"
|
||||||
return HTTPException(status_code=500, detail=f"ResourceNotFoundError: Could not set webhook: {e}")
|
logger.error(error_msg, exc_info=True)
|
||||||
|
return HTTPException(status_code=500, detail=error_msg)
|
||||||
|
Reference in New Issue
Block a user