Skip to content

Commit 8e47ed2

Browse files
committed
Show user-friendly error on storage limit during project create or sync
1 parent 4fc1a47 commit 8e47ed2

1 file changed

Lines changed: 50 additions & 3 deletions

File tree

Mergin/projects_manager.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,21 @@ def create_project(self, project_name, project_dir, is_public, namespace):
126126
f"Projects quota: {quota}"
127127
)
128128
elif e.server_code == ErrorCode.StorageLimitHit.value:
129-
msg = f"{e.detail}\nCurrent limit: {bytes_to_human_size(e.server_response['storage_limit'])}"
129+
data = e.server_response
130+
if isinstance(data, str):
131+
try:
132+
data = json.loads(data)
133+
except json.JSONDecodeError:
134+
data = {}
135+
136+
storage_limit = data.get("storage_limit")
137+
human_limit = (
138+
bytes_to_human_size(storage_limit)
139+
if storage_limit is not None
140+
else "unknown"
141+
)
142+
143+
msg = f"{e.detail}\nCurrent limit: {human_limit}"
130144

131145
QMessageBox.critical(
132146
None,
@@ -186,7 +200,27 @@ def create_project(self, project_name, project_dir, is_public, namespace):
186200
if isinstance(dlg.exception, LoginError):
187201
login_error_message(dlg.exception)
188202
elif isinstance(dlg.exception, ClientError):
189-
QMessageBox.critical(None, "Project sync", "Client error: " + str(dlg.exception))
203+
exc = dlg.exception
204+
msg = str(exc)
205+
206+
if exc.server_code == ErrorCode.StorageLimitHit.value:
207+
data = exc.server_response
208+
if isinstance(data, str):
209+
try:
210+
data = json.loads(data)
211+
except json.JSONDecodeError:
212+
data = {}
213+
214+
storage_limit = data.get("storage_limit")
215+
human_limit = (
216+
bytes_to_human_size(storage_limit)
217+
if storage_limit is not None
218+
else "unknown"
219+
)
220+
221+
msg = f"{exc.detail}\nCurrent limit: {human_limit}"
222+
223+
QMessageBox.critical(None, "Project sync", "Client error: " + msg)
190224
else:
191225
unhandled_exception_message(
192226
dlg.exception_details(),
@@ -219,6 +253,7 @@ def create_project(self, project_name, project_dir, is_public, namespace):
219253

220254
return True
221255

256+
222257
def project_status(self, project_dir):
223258
if project_dir is None:
224259
return
@@ -463,7 +498,19 @@ def sync_project(self, project_dir, project_name=None):
463498
# To note we check for a string since error in flask doesn't return server error code
464499
msg = "Somebody else is syncing, please try again later"
465500
elif dlg.exception.server_code == ErrorCode.StorageLimitHit.value:
466-
msg = f"{dlg.exception.detail}\nCurrent limit: {bytes_to_human_size(dlg.exception.server_response['storage_limit'])}"
501+
data = dlg.exception.server_response
502+
if isinstance(data, str):
503+
try:
504+
data = json.loads(data)
505+
except json.JSONDecodeError:
506+
data = {}
507+
storage_limit = data.get("storage_limit")
508+
human_limit = (
509+
bytes_to_human_size(storage_limit)
510+
if storage_limit is not None
511+
else "unknown"
512+
)
513+
msg = f"{dlg.exception.detail}\nCurrent limit: {human_limit}"
467514
else:
468515
msg = str(dlg.exception)
469516
QMessageBox.critical(None, "Project sync", "Client error: \n" + msg)

0 commit comments

Comments
 (0)