[fix][web] not handling HTTP connection issues

This commit is contained in:
Vinícius Moreira
2019-12-27 13:14:16 -03:00
parent 113402cebd
commit 1636d600ee
2 changed files with 17 additions and 6 deletions

View File

@@ -7,6 +7,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [0.8.1] ## [0.8.1]
### Fixes ### Fixes
- not verifying if an icon path is a file - not verifying if an icon path is a file
- Web:
- not handling HTTP connection issues
## [0.8.0] 2019-12-24 ## [0.8.0] 2019-12-24
### Features ### Features

View File

@@ -11,6 +11,7 @@ from typing import List, Type, Set, Tuple
import yaml import yaml
from colorama import Fore from colorama import Fore
from requests import exceptions
from bauh.api.abstract.context import ApplicationContext from bauh.api.abstract.context import ApplicationContext
from bauh.api.abstract.controller import SoftwareManager, SearchResult from bauh.api.abstract.controller import SoftwareManager, SearchResult
@@ -136,10 +137,14 @@ class WebApplicationManager(SoftwareManager):
return description return description
def _get_fix_for(self, url_no_protocol: str) -> str: 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: if res:
return res.text 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: def _strip_url_protocol(self, url: str) -> str:
return RE_PROTOCOL_STRIP.split(url)[1].strip().lower() return RE_PROTOCOL_STRIP.split(url)[1].strip().lower()
@@ -149,10 +154,14 @@ class WebApplicationManager(SoftwareManager):
def _map_url(self, url: str) -> "BeautifulSoup": def _map_url(self, url: str) -> "BeautifulSoup":
headers = {'Accept-language': self._get_lang_header(), 'User-Agent': UA_CHROME} 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: try:
return BeautifulSoup(url_res.text, 'lxml', parse_only=SoupStrainer('head')) 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: def search(self, words: str, disk_loader: DiskCacheLoader, limit: int = -1, is_url: bool = False) -> SearchResult:
local_config = {} local_config = {}