|
5 | 5 | from urllib.parse import urlparse |
6 | 6 | from pathlib import Path |
7 | 7 | import posixpath |
| 8 | +import json |
8 | 9 |
|
9 | 10 | from qgis.core import QgsProject, Qgis, QgsApplication |
10 | 11 | from qgis.utils import iface, OverrideCursor |
|
29 | 30 | login_error_message, |
30 | 31 | same_dir, |
31 | 32 | send_logs, |
| 33 | + storage_limit_fail, |
32 | 34 | unhandled_exception_message, |
33 | 35 | unsaved_project_check, |
34 | 36 | UnsavedChangesStrategy, |
|
37 | 39 | get_push_changes_batch, |
38 | 40 | SYNC_ATTEMPTS, |
39 | 41 | SYNC_ATTEMPT_WAIT, |
| 42 | + push_error_message, |
40 | 43 | ) |
41 | 44 | from .utils_auth import get_stored_mergin_server_url |
42 | 45 |
|
@@ -114,12 +117,31 @@ def create_project(self, project_name, project_dir, is_public, namespace): |
114 | 117 | "Please try renaming the project." |
115 | 118 | ) |
116 | 119 | elif e.server_code == ErrorCode.ProjectsLimitHit.value: |
| 120 | + data = e.server_response |
| 121 | + if isinstance(data, str): |
| 122 | + try: |
| 123 | + data = json.loads(data) # convert string to json |
| 124 | + except json.JSONDecodeError: |
| 125 | + data = {} |
| 126 | + |
| 127 | + quota = data.get("projects_quota", "unknown") |
| 128 | + |
117 | 129 | msg = ( |
118 | 130 | "Maximum number of projects reached. Please upgrade your subscription to create new projects.\n" |
119 | | - f"Projects quota: {e.server_response['projects_quota']}" |
| 131 | + f"Projects quota: {quota}" |
120 | 132 | ) |
121 | 133 | elif e.server_code == ErrorCode.StorageLimitHit.value: |
122 | | - msg = f"{e.detail}\nCurrent limit: {bytes_to_human_size(e.server_response['storage_limit'])}" |
| 134 | + data = e.server_response |
| 135 | + if isinstance(data, str): |
| 136 | + try: |
| 137 | + data = json.loads(data) |
| 138 | + except json.JSONDecodeError: |
| 139 | + data = {} |
| 140 | + |
| 141 | + storage_limit = data.get("storage_limit") |
| 142 | + human_limit = bytes_to_human_size(storage_limit) if storage_limit is not None else "unknown" |
| 143 | + |
| 144 | + msg = f"{e.detail}\nCurrent limit: {human_limit}" |
123 | 145 |
|
124 | 146 | QMessageBox.critical( |
125 | 147 | None, |
@@ -176,18 +198,7 @@ def create_project(self, project_name, project_dir, is_public, namespace): |
176 | 198 | dlg.exec() # blocks until success, failure or cancellation |
177 | 199 |
|
178 | 200 | if dlg.exception: |
179 | | - # push failed for some reason |
180 | | - if isinstance(dlg.exception, LoginError): |
181 | | - login_error_message(dlg.exception) |
182 | | - elif isinstance(dlg.exception, ClientError): |
183 | | - QMessageBox.critical(None, "Project sync", "Client error: " + str(dlg.exception)) |
184 | | - else: |
185 | | - unhandled_exception_message( |
186 | | - dlg.exception_details(), |
187 | | - "Project sync", |
188 | | - f"Something went wrong while synchronising your project {project_name}.", |
189 | | - self.mc, |
190 | | - ) |
| 201 | + push_error_message(dlg, project_name, self.plugin, self.mc) |
191 | 202 | return True |
192 | 203 |
|
193 | 204 | if not dlg.is_complete: |
@@ -302,32 +313,48 @@ def reset_local_changes(self, project_dir: str, files_to_reset=None): |
302 | 313 |
|
303 | 314 | current_project_filename = os.path.normpath(QgsProject.instance().fileName()) |
304 | 315 | current_project_path = os.path.normpath(QgsProject.instance().absolutePath()) |
| 316 | + |
| 317 | + # Windows-specific behavior: |
| 318 | + # When a project is opened from this directory, QGIS may keep GPKG file handles |
| 319 | + # for a short time after closing the project. The same workaround is used in |
| 320 | + # `close_project_and_fix_pull()` (unfinished pull handling). |
| 321 | + delay = 0 |
305 | 322 | if current_project_path == os.path.normpath(project_dir): |
306 | 323 | QgsProject.instance().clear() |
| 324 | + QApplication.processEvents() |
| 325 | + delay = 2500 # allow OS to release locked GPKG handles |
307 | 326 |
|
308 | | - try: |
309 | | - self.mc.reset_local_changes(project_dir, files_to_reset) |
310 | | - if files_to_reset: |
311 | | - msg = f"File {files_to_reset} was successfully reset" |
312 | | - else: |
313 | | - msg = "Project local changes were successfully reset" |
314 | | - QMessageBox.information( |
315 | | - None, |
316 | | - "Project reset local changes", |
317 | | - msg, |
318 | | - QMessageBox.StandardButton.Close, |
319 | | - ) |
| 327 | + def do_reset(): |
| 328 | + try: |
| 329 | + self.mc.reset_local_changes(project_dir, files_to_reset) |
320 | 330 |
|
321 | | - except Exception as e: |
322 | | - msg = f"Failed to reset local changes:\n\n{str(e)}" |
323 | | - QMessageBox.critical( |
324 | | - None, |
325 | | - "Project reset local changes", |
326 | | - msg, |
327 | | - QMessageBox.StandardButton.Close, |
328 | | - ) |
| 331 | + if files_to_reset: |
| 332 | + msg = f"File {files_to_reset} was successfully reset" |
| 333 | + else: |
| 334 | + msg = "Project local changes were successfully reset" |
| 335 | + |
| 336 | + QMessageBox.information( |
| 337 | + None, |
| 338 | + "Project reset local changes", |
| 339 | + msg, |
| 340 | + QMessageBox.StandardButton.Close, |
| 341 | + ) |
| 342 | + |
| 343 | + except Exception as e: |
| 344 | + msg = f"Failed to reset local changes:\n\n{str(e)}" |
| 345 | + QMessageBox.critical( |
| 346 | + None, |
| 347 | + "Project reset local changes", |
| 348 | + msg, |
| 349 | + QMessageBox.StandardButton.Close, |
| 350 | + ) |
| 351 | + |
| 352 | + # Reopen the project after successful or failed reset |
| 353 | + self.open_project(os.path.dirname(current_project_filename)) |
329 | 354 |
|
330 | | - self.open_project(os.path.dirname(current_project_filename)) |
| 355 | + # Run the reset after delay (0 ms on Linux/macOS, 2500 ms on Windows) |
| 356 | + # This mirrors the pattern from unfinished pull resolution. |
| 357 | + QTimer.singleShot(delay, do_reset) |
331 | 358 |
|
332 | 359 | def sync_project(self, project_dir, project_name=None): |
333 | 360 | if not project_dir: |
@@ -445,7 +472,7 @@ def sync_project(self, project_dir, project_name=None): |
445 | 472 | # To note we check for a string since error in flask doesn't return server error code |
446 | 473 | msg = "Somebody else is syncing, please try again later" |
447 | 474 | elif dlg.exception.server_code == ErrorCode.StorageLimitHit.value: |
448 | | - msg = f"{dlg.exception.detail}\nCurrent limit: {bytes_to_human_size(dlg.exception.server_response['storage_limit'])}" |
| 475 | + msg = storage_limit_fail(dlg.exception) |
449 | 476 | else: |
450 | 477 | msg = str(dlg.exception) |
451 | 478 | QMessageBox.critical(None, "Project sync", "Client error: \n" + msg) |
|
0 commit comments