From 8c0c4d45468c20e418680f3cac4ce92878ef5c78 Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Fri, 12 Feb 2021 14:46:21 -0300 Subject: [PATCH] [appimage] fix -> crashing when trying to upgrade and the internet connection is off --- CHANGELOG.md | 6 ++++++ bauh/__init__.py | 2 +- bauh/api/http.py | 18 +++++++++++------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30f5426b..a09e650f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). +## [0.9.15] 2021 +### Fixes +- AppImage + - crashing when trying to upgrade and the internet connection is off + + ## [0.9.14] 2021-02-03 ### Improvements - AppImage diff --git a/bauh/__init__.py b/bauh/__init__.py index ce9e3ca1..7f151822 100644 --- a/bauh/__init__.py +++ b/bauh/__init__.py @@ -1,4 +1,4 @@ -__version__ = '0.9.14' +__version__ = '0.9.15' __app_name__ = 'bauh' import os diff --git a/bauh/api/http.py b/bauh/api/http.py index fc65d165..1c459f2e 100644 --- a/bauh/api/http.py +++ b/bauh/api/http.py @@ -68,12 +68,17 @@ class HttpClient: res = self.get(url, params=params, headers=headers, allow_redirects=allow_redirects, session=session) return yaml.safe_load(res.text) if res else None - def get_content_length_in_bytes(self, url: str, session: bool = True) -> int: + def get_content_length_in_bytes(self, url: str, session: bool = True) -> Optional[int]: params = {'url': url, 'allow_redirects': True, 'stream': True} - if session: - res = self.session.get(**params) - else: - res = requests.get(**params) + + try: + if session: + res = self.session.get(**params) + else: + res = requests.get(**params) + except requests.exceptions.ConnectionError: + self.logger.info("Internet seems to be off. Could not reach '{}'".format(url)) + return if res.status_code == 200: size = res.headers.get('Content-Length') @@ -84,7 +89,7 @@ class HttpClient: except: pass - def get_content_length(self, url: str, session: bool = True) -> str: + def get_content_length(self, url: str, session: bool = True) -> Optional[str]: size = self.get_content_length_in_bytes(url, session) if size: @@ -98,4 +103,3 @@ class HttpClient: res = self.session.get(**params) return res.status_code in (200, 403) - return False