Skip to content

Commit

Permalink
Better handling of HTTP errors
Browse files Browse the repository at this point in the history
  • Loading branch information
geirawsm committed Aug 26, 2024
1 parent 08294d7 commit 872b451
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
5 changes: 5 additions & 0 deletions sausage_bot/cogs/rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ async def rss_add(
'Urlen er ikke en RSS/XML feed', ephemeral=True
)
return
elif isinstance(valid_feed, int):
await interaction.followup.send(
f'Urlen gir feilkode: {valid_feed}', ephemeral=True
)
return
log.verbose('Adding feed to db')
await feeds_core.add_to_feed_db(
'spotify', str(feed_name), str(feed_link), channel.name, AUTHOR
Expand Down
20 changes: 8 additions & 12 deletions sausage_bot/util/feeds_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,6 @@ async def check_if_feed_name_exist(feed_name):
return True


async def check_url_validity(url):
'Make sure that `url` is a valid link'
log.verbose(f'Checking `{url}`')
req = await net_io.get_link(url)
log.debug(f'req is ({type(req)})')
if req is None:
return False
else:
return True


async def check_feed_validity(URL):
'Make sure that `URL` is a valid link with feed items'
sample_item = None
Expand All @@ -46,6 +35,8 @@ async def check_feed_validity(URL):
if req is None:
log.verbose('Returned None')
return None
elif isinstance(req, int):
return req
if 'open.spotify.com/show/' in URL:
log.verbose('Discovered Spotify branded link')
sample_item = await net_io.check_spotify_podcast(URL)
Expand Down Expand Up @@ -164,6 +155,9 @@ async def add_to_feed_db(
if test_link is None:
log.verbose(f'`test_link` is None')
return None
elif isinstance(test_link, int):
log.verbose(f'`test_link` returns code {test_link}')
return test_link
date_now = datetime_handling.get_dt(format='datetime')
if feed_type in ['rss', 'spotify']:
await db_helper.insert_many_some(
Expand Down Expand Up @@ -263,6 +257,8 @@ async def get_feed_links(feed_type, feed_info):
)
log.debug(f'Got {len(links_out)} items from `get_items_from_rss`')
return links_out
elif isinstance(req, int):
return req
else:
return

Expand Down Expand Up @@ -471,7 +467,7 @@ async def review_feeds_status(feed_type: str = None):
db_updates['status_url_counter'].append(
('uuid', UUID, 0)
)
elif not is_valid_feed:
elif not is_valid_feed or isinstance(is_valid_feed, int):
log.debug(
f'`URL_STATUS_COUNTER` ({URL_STATUS_COUNTER}) vs '
f'`envs.FEEDS_URL_ERROR_LIMIT` ({envs.FEEDS_URL_ERROR_LIMIT})'
Expand Down
11 changes: 6 additions & 5 deletions sausage_bot/util/net_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,30 @@
async def get_link(url):
'Get contents of requests object from a `url`'
content_out = None
url_status = 0
if type(url) is not str:
log.error(envs.RSS_INVALID_URL.format(url))
return None
if re.search(r'^http(s)?', url):
if re.search(r'^http(s)?\:', url):
log.debug('Found scheme in url')
elif re.match(r'^((http:\/\/|^https:\/\/))?((www\.))?', url) is not None:
elif re.match(r'^((http\:\/\/|^https\:\/\/))?((www\.))?', url) is not None:
log.debug('Did not found scheme, adding')
url = f'https://{url}'
try:
log.debug(f'Trying `url`: {url}')
session = aiohttp.ClientSession()
async with session.get(url) as resp:
url_status = resp.status
content_out = await resp.text()
log.debug(f'Got status: {url_status}')
content_out = await resp.text()
log.debug(f'Got content_out: {content_out[0:500]}...')
await session.close()
except Exception as e:
log.error(f'Error when getting `url`: {e}')
log.error(f'Error when getting `url`:({url_status}) {e}')
return None
if 399 < int(url_status) < 600:
log.error(f'Got error code {url_status}')
return None
return int(url_status)
if content_out is None:
return None
else:
Expand Down

0 comments on commit 872b451

Please sign in to comment.