From 1636d600ee511aeb51477beecfcc5a3766dba5cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Moreira?= Date: Fri, 27 Dec 2019 13:14:16 -0300 Subject: [PATCH] [fix][web] not handling HTTP connection issues --- CHANGELOG.md | 2 ++ bauh/gems/web/controller.py | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54e73cc7..eb805732 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [0.8.1] ### Fixes - not verifying if an icon path is a file +- Web: + - not handling HTTP connection issues ## [0.8.0] 2019-12-24 ### Features diff --git a/bauh/gems/web/controller.py b/bauh/gems/web/controller.py index 0a922658..e45f9df7 100644 --- a/bauh/gems/web/controller.py +++ b/bauh/gems/web/controller.py @@ -11,6 +11,7 @@ from typing import List, Type, Set, Tuple import yaml from colorama import Fore +from requests import exceptions from bauh.api.abstract.context import ApplicationContext from bauh.api.abstract.controller import SoftwareManager, SearchResult @@ -136,10 +137,14 @@ class WebApplicationManager(SoftwareManager): return description def _get_fix_for(self, url_no_protocol: str) -> str: - res = self.http_client.get(URL_FIX_PATTERN.format(url=url_no_protocol)) + try: + fix_url = URL_FIX_PATTERN.format(url=url_no_protocol) + res = self.http_client.get(fix_url) - if res: - return res.text + if res: + return res.text + except Exception as e: + self.logger.warning("Error when trying to retrieve a fix for {}: {}".format(fix_url, e.__class__.__name__)) def _strip_url_protocol(self, url: str) -> str: return RE_PROTOCOL_STRIP.split(url)[1].strip().lower() @@ -149,10 +154,14 @@ class WebApplicationManager(SoftwareManager): def _map_url(self, url: str) -> "BeautifulSoup": headers = {'Accept-language': self._get_lang_header(), 'User-Agent': UA_CHROME} - url_res = self.http_client.get(url, headers=headers, ignore_ssl=True, single_call=True) - if url_res: - return BeautifulSoup(url_res.text, 'lxml', parse_only=SoupStrainer('head')) + try: + url_res = self.http_client.get(url, headers=headers, ignore_ssl=True, single_call=True) + + if url_res: + return BeautifulSoup(url_res.text, 'lxml', parse_only=SoupStrainer('head')) + except exceptions.ConnectionError as e: + self.logger.warning("Could not get {}: {}".format(url, e.__class__.__name__)) def search(self, words: str, disk_loader: DiskCacheLoader, limit: int = -1, is_url: bool = False) -> SearchResult: local_config = {}