Skip to content
This repository was archived by the owner on Mar 1, 2025. It is now read-only.

Commit d7bcd9b

Browse files
committed
Improve error handling
Use fancy_handle to report errors. There is no separate LocalWorkspaceModel any longer, only ServerWorkspaceModel with a base class. * Follow PEP 8 naming rules * Improve error handling bookmarks * Handle workspaces well during disconnect/reconnect * Support file browsing while disconnect * Update user menu correctly on disconnect * Improve disconnect while logging in * Remove unnecessary handle calls * Handle share link disconnect better * Handle versions while disconnected * Handle preferences while disconnected * Support deleting directories while disconnecting * Support deleting files while disconnecting * Make sharelink and active version disconnect aware * Handle models better * Improve handling disconnected wrt models Workspaces and workspace models are refreshed each time we get to disconnected state.
1 parent 678e0f5 commit d7bcd9b

7 files changed

+386
-322
lines changed

APIClient.py

+22-18
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ def setStatus(self, newStatus):
110110
self.parent.set_ui_connectionStatus()
111111

112112
def getStatus(self):
113-
"""gets the current connection status; this is an active check to see if really online"""
113+
"""
114+
Gets the current connection status;
115+
This is an active check to see if really online.
116+
"""
114117
self._confirm_online()
115118
return self.status
116119

@@ -193,9 +196,12 @@ def _set_content_type(self):
193196
return headers
194197

195198
def _confirm_online(self):
196-
"""calls lens api root to simply check if online. if not online, updates status"""
199+
"""
200+
Calls lens api root to simply check if online.
201+
If not online, updates status.
202+
"""
197203
try:
198-
response = requests.get(f"{self.base_url}/")
204+
requests.get(f"{self.base_url}/")
199205
if self.is_logged_in():
200206
self.setStatus(ConnStatus.CONNECTED)
201207
else:
@@ -207,13 +213,13 @@ def _confirm_online(self):
207213
def _confirm_online_after_exception(self):
208214
self._confirm_online()
209215
if self.status == ConnStatus.DISCONNECTED:
210-
raise APIClientOfflineException("Safely offline")
216+
raise APIClientOfflineException("Disconnected from service: logged out")
211217

212218
def _properly_throw_if_offline(self):
213219
if self.status == ConnStatus.DISCONNECTED:
214220
self._confirm_online() # try to connect again
215221
if self.status == ConnStatus.DISCONNECTED:
216-
raise APIClientOfflineException("Safely offline")
222+
raise APIClientOfflineException("Disconnected from service: logged out")
217223

218224
def _delete(self, endpoint, headers={}, params=None):
219225
headers = self._set_default_headers(headers)
@@ -919,7 +925,7 @@ def filterFilter(data):
919925
return data
920926

921927

922-
class API_Call_Result(Enum):
928+
class APICallResult(Enum):
923929
OK = 1 # all is good
924930
DISCONNECTED = 2 # all is good but not online
925931
NOT_LOGGED_IN = 3 # not logged in, so _this_ query is not possible
@@ -941,27 +947,25 @@ def fancy_handle(func):
941947
make sure you are doing "result = fancy_handle(joe)" not
942948
"result = fancy_handle(joe())"
943949
944-
Returns an API_Call_Result enum value. It is expected that the caller
950+
Returns an APICallResult enum value. It is expected that the caller
945951
handles all the possible return states.
946952
"""
947953
try:
948954
func()
949-
return API_Call_Result.OK
950-
except APIClientOfflineException as e:
951-
return API_Call_Result.DISCONNECTED
952-
except APIClientLoggedOutException as e:
953-
return API_Call_Result.NOT_LOGGED_IN
955+
return APICallResult.OK
956+
except APIClientOfflineException:
957+
return APICallResult.DISCONNECTED
958+
except APIClientLoggedOutException:
959+
return APICallResult.NOT_LOGGED_IN
954960
except APIClientRequestException as e:
955961
logger.error(e)
956-
return API_Call_Result.GENERAL_ERROR
962+
return APICallResult.GENERAL_ERROR
957963
except APIClientAuthenticationException as e:
958964
logger.error(e)
959-
return API_Call_Result.PERMISSION_ISSUE
965+
return APICallResult.PERMISSION_ISSUE
960966
except APIClientException as e:
961967
logger.error(e)
962-
return API_Call_Result.GENERAL_ERROR
968+
return APICallResult.GENERAL_ERROR
963969
except Exception as e:
964970
logger.error(e)
965-
return API_Call_Result.GENERAL_ERROR
966-
self.set_ui_connectionStatus()
967-
return True
971+
return APICallResult.GENERAL_ERROR

DataModels.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414

1515
import FreeCAD
1616

17+
from APIClient import fancy_handle, APICallResult
18+
1719

1820
CACHE_PATH = FreeCAD.getUserCachePath() + "Ondsel-Lens/"
19-
p = FreeCAD.ParamGet("User parameter:BaseApp/Ondsel")
2021

2122

2223
class WorkspaceListModel(QAbstractListModel):
@@ -46,12 +47,12 @@ def set_api(self, api):
4647
self.api = api
4748

4849
def refreshModel(self):
49-
# raises an APIClientException
50-
self.beginResetModel()
51-
if self.api is not None and self.api.is_logged_in():
52-
# the user may be disconnected
50+
def try_get_workspaces_connected():
5351
self.workspaces = self.api.getWorkspaces()
5452

53+
self.beginResetModel()
54+
api_result = fancy_handle(try_get_workspaces_connected)
55+
if api_result == APICallResult.OK:
5556
self.save()
5657
else:
5758
self.load()

Utils.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
NAME_COMMAND_START = "Start_Start"
3838
LENS_TOOLBARITEM_TEXT = "Ondsel Lens Addon"
3939

40+
SIZE_PIXMAP = 128
41+
4042

4143
class FreeCADHandler(logging.Handler):
4244
def __init__(self):
@@ -126,7 +128,9 @@ def extract_thumbnail(file_path):
126128
pixmap = QPixmap()
127129
pixmap.loadFromData(thumbnail_data)
128130

129-
return pixmap.scaled(128, 128, Qt.AspectRatioMode.KeepAspectRatio)
131+
return pixmap.scaled(
132+
SIZE_PIXMAP, SIZE_PIXMAP, Qt.AspectRatioMode.KeepAspectRatio
133+
)
130134

131135
except (zipfile.BadZipFile, KeyError):
132136
# Handle the case where the thumbnail file doesn't exist

0 commit comments

Comments
 (0)