Skip to content

Commit

Permalink
import: Replace generic Exception with CommandError.
Browse files Browse the repository at this point in the history
This change improves error handling in `do_import_realm` by replacing
the use of a generic Exception with CommandError. The updated approach
provides clearer, user-friendly error messages when there is a version
mismatch between the exported data and the Zulip server.

Fixes zulip#32292.
  • Loading branch information
jitendra-ky authored and timabbott committed Nov 19, 2024
1 parent ffa7e0a commit ca14366
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
17 changes: 9 additions & 8 deletions zerver/lib/import_realm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from bs4 import BeautifulSoup
from django.conf import settings
from django.core.cache import cache
from django.core.management.base import CommandError
from django.core.validators import validate_email
from django.db import connection, transaction
from django.db.backends.utils import CursorWrapper
Expand Down Expand Up @@ -2122,10 +2123,10 @@ def check_migration_status(exported_migration_status: MigrationStatusJson) -> No
exported_primary_version = exported_migration_status["zulip_version"].split(".")[0]
local_primary_version = local_migration_status["zulip_version"].split(".")[0]
if exported_primary_version != local_primary_version:
raise Exception(
"Export was generated on a different Zulip major version.\n"
f"Export={exported_migration_status['zulip_version']}\n"
f"Server={local_migration_status['zulip_version']}"
raise CommandError(
"Error: Export was generated on a different Zulip major version.\n"
f"Export version: {exported_migration_status['zulip_version']}\n"
f"Server version: {local_migration_status['zulip_version']}"
)
exported_migrations_by_app = exported_migration_status["migrations_by_app"]
local_migrations_by_app = local_migration_status["migrations_by_app"]
Expand Down Expand Up @@ -2160,13 +2161,13 @@ def check_migration_status(exported_migration_status: MigrationStatusJson) -> No
]

error_message = (
"Export was generated on a different Zulip version.\n"
f"Export={exported_migration_status['zulip_version']}\n"
f"Server={local_migration_status['zulip_version']}\n"
"Error: Export was generated on a different Zulip version.\n"
f"Export version: {exported_migration_status['zulip_version']}\n"
f"Server version: {local_migration_status['zulip_version']}\n"
"\n"
"Database formats differ between the exported realm and this server.\n"
"Printing migrations that differ between the versions:\n"
"--- exported realm\n"
"+++ this server"
) + "\n".join(sorted_error_log)
raise Exception(error_message)
raise CommandError(error_message)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Export was generated on a different Zulip version.
Export={version_placeholder}
Server={version_placeholder}
Error: Export was generated on a different Zulip version.
Export version: {version_placeholder}
Server version: {version_placeholder}

Database formats differ between the exported realm and this server.
Printing migrations that differ between the versions:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Export was generated on a different Zulip version.
Export={version_placeholder}
Server={version_placeholder}
Error: Export was generated on a different Zulip version.
Export version: {version_placeholder}
Server version: {version_placeholder}

Database formats differ between the exported realm and this server.
Printing migrations that differ between the versions:
Expand Down
9 changes: 5 additions & 4 deletions zerver/tests/test_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import orjson
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.management.base import CommandError
from django.db.models import Q, QuerySet
from django.utils.timezone import now as timezone_now
from typing_extensions import override
Expand Down Expand Up @@ -2199,17 +2200,17 @@ def test_import_realm_with_different_stated_zulip_version(self) -> None:

with (
patch("zerver.lib.import_realm.ZULIP_VERSION", "8.0"),
self.assertRaises(Exception) as e,
self.assertRaises(CommandError) as e,
self.assertLogs(level="INFO"),
):
do_import_realm(
get_output_dir(),
"test-zulip",
)
expected_error_message = (
"Export was generated on a different Zulip major version.\n"
f"Export={ZULIP_VERSION}\n"
"Server=8.0"
"Error: Export was generated on a different Zulip major version.\n"
f"Export version: {ZULIP_VERSION}\n"
"Server version: 8.0"
)
self.assertEqual(expected_error_message, str(e.exception))

Expand Down

0 comments on commit ca14366

Please sign in to comment.