Skip to content

Commit 6383dfc

Browse files
committed
Extract storage_limit_fail and push_error_message helpers to avoid duplicated error handling logic
1 parent 362ad79 commit 6383dfc

File tree

2 files changed

+39
-58
lines changed

2 files changed

+39
-58
lines changed

Mergin/projects_manager.py

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
UnsavedChangesStrategy,
3636
write_project_variables,
3737
bytes_to_human_size,
38+
push_error_message,
3839
)
3940
from .utils_auth import get_stored_mergin_server_url
4041

@@ -192,34 +193,7 @@ def create_project(self, project_name, project_dir, is_public, namespace):
192193
dlg.exec() # blocks until success, failure or cancellation
193194

194195
if dlg.exception:
195-
# push failed for some reason
196-
if isinstance(dlg.exception, LoginError):
197-
login_error_message(dlg.exception)
198-
elif isinstance(dlg.exception, ClientError):
199-
exc = dlg.exception
200-
msg = str(exc)
201-
202-
if exc.server_code == ErrorCode.StorageLimitHit.value:
203-
data = exc.server_response
204-
if isinstance(data, str):
205-
try:
206-
data = json.loads(data)
207-
except json.JSONDecodeError:
208-
data = {}
209-
210-
storage_limit = data.get("storage_limit")
211-
human_limit = bytes_to_human_size(storage_limit) if storage_limit is not None else "unknown"
212-
213-
msg = f"{exc.detail}\nCurrent limit: {human_limit}"
214-
215-
QMessageBox.critical(None, "Project sync", "Client error: " + msg)
216-
else:
217-
unhandled_exception_message(
218-
dlg.exception_details(),
219-
"Project sync",
220-
f"Something went wrong while synchronising your project {project_name}.",
221-
self.mc,
222-
)
196+
push_error_message(dlg, project_name, self.plugin, self.mc)
223197
return True
224198

225199
if not dlg.is_complete:
@@ -481,35 +455,7 @@ def sync_project(self, project_dir, project_name=None):
481455
self.open_project(project_dir)
482456

483457
if dlg.exception:
484-
# push failed for some reason
485-
if isinstance(dlg.exception, LoginError):
486-
login_error_message(dlg.exception)
487-
elif isinstance(dlg.exception, ClientError):
488-
if dlg.exception.http_error == 400 and "Another process" in dlg.exception.detail:
489-
# To note we check for a string since error in flask doesn't return server error code
490-
msg = "Somebody else is syncing, please try again later"
491-
elif dlg.exception.server_code == ErrorCode.StorageLimitHit.value:
492-
data = dlg.exception.server_response
493-
if isinstance(data, str):
494-
try:
495-
data = json.loads(data)
496-
except json.JSONDecodeError:
497-
data = {}
498-
storage_limit = data.get("storage_limit")
499-
human_limit = bytes_to_human_size(storage_limit) if storage_limit is not None else "unknown"
500-
msg = f"{dlg.exception.detail}\nCurrent limit: {human_limit}"
501-
else:
502-
msg = str(dlg.exception)
503-
QMessageBox.critical(None, "Project sync", "Client error: \n" + msg)
504-
elif isinstance(dlg.exception, AuthTokenExpiredError):
505-
self.plugin.auth_token_expired()
506-
else:
507-
unhandled_exception_message(
508-
dlg.exception_details(),
509-
"Project sync",
510-
f"Something went wrong while synchronising your project {project_name}.",
511-
self.mc,
512-
)
458+
push_error_message(dlg, project_name, self.plugin, self.mc)
513459
return
514460

515461
if dlg.is_complete:

Mergin/utils.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777

7878
try:
7979
from .mergin.common import ClientError, ErrorCode, LoginError, InvalidProject
80-
from .mergin.client import MerginClient, ServerType
80+
from .mergin.client import MerginClient, ServerType, AuthTokenExpiredError
8181
from .mergin.client_pull import (
8282
download_project_async,
8383
download_project_is_running,
@@ -1733,3 +1733,38 @@ def sanitize_path(expr: str) -> str:
17331733
parts = expr.split("/")
17341734
cleaned = [p.rstrip() for p in parts]
17351735
return "/".join(cleaned)
1736+
1737+
def storage_limit_fail(exc):
1738+
data = exc.server_response
1739+
if isinstance(data, str):
1740+
try:
1741+
data = json.loads(data)
1742+
except json.JSONDecodeError:
1743+
data = {}
1744+
storage_limit = data.get("storage_limit")
1745+
human_limit = bytes_to_human_size(storage_limit) if storage_limit is not None else "unknown"
1746+
return f"{exc.detail}\nCurrent limit: {human_limit}"
1747+
1748+
def push_error_message(dlg, project_name, plugin, mc):
1749+
if isinstance(dlg.exception, LoginError):
1750+
login_error_message(dlg.exception)
1751+
elif isinstance(dlg.exception, ClientError):
1752+
exc = dlg.exception
1753+
1754+
if exc.http_error == 400 and "Another process" in exc.detail:
1755+
msg = "Somebody else is syncing, please try again later"
1756+
elif exc.server_code == ErrorCode.StorageLimitHit.value:
1757+
msg = storage_limit_fail(exc)
1758+
else:
1759+
msg = str(exc)
1760+
1761+
QMessageBox.critical(None, "Project sync", "Client error: \n" + msg)
1762+
elif isinstance(dlg.exception, AuthTokenExpiredError):
1763+
plugin.auth_token_expired()
1764+
else:
1765+
unhandled_exception_message(
1766+
dlg.exception_details(),
1767+
"Project sync",
1768+
f"Something went wrong while synchronising your project {project_name}.",
1769+
mc,
1770+
)

0 commit comments

Comments
 (0)