import: Replace generic Exception with CommandError.

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 #32292.
This commit is contained in:
jitendra-ky 2024-11-16 18:58:23 +05:30 committed by Tim Abbott
parent ffa7e0ac08
commit ca14366e38
4 changed files with 20 additions and 18 deletions

View File

@ -13,6 +13,7 @@ import pyvips
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from django.conf import settings from django.conf import settings
from django.core.cache import cache from django.core.cache import cache
from django.core.management.base import CommandError
from django.core.validators import validate_email from django.core.validators import validate_email
from django.db import connection, transaction from django.db import connection, transaction
from django.db.backends.utils import CursorWrapper from django.db.backends.utils import CursorWrapper
@ -2122,10 +2123,10 @@ def check_migration_status(exported_migration_status: MigrationStatusJson) -> No
exported_primary_version = exported_migration_status["zulip_version"].split(".")[0] exported_primary_version = exported_migration_status["zulip_version"].split(".")[0]
local_primary_version = local_migration_status["zulip_version"].split(".")[0] local_primary_version = local_migration_status["zulip_version"].split(".")[0]
if exported_primary_version != local_primary_version: if exported_primary_version != local_primary_version:
raise Exception( raise CommandError(
"Export was generated on a different Zulip major version.\n" "Error: Export was generated on a different Zulip major version.\n"
f"Export={exported_migration_status['zulip_version']}\n" f"Export version: {exported_migration_status['zulip_version']}\n"
f"Server={local_migration_status['zulip_version']}" f"Server version: {local_migration_status['zulip_version']}"
) )
exported_migrations_by_app = exported_migration_status["migrations_by_app"] exported_migrations_by_app = exported_migration_status["migrations_by_app"]
local_migrations_by_app = local_migration_status["migrations_by_app"] local_migrations_by_app = local_migration_status["migrations_by_app"]
@ -2160,13 +2161,13 @@ def check_migration_status(exported_migration_status: MigrationStatusJson) -> No
] ]
error_message = ( error_message = (
"Export was generated on a different Zulip version.\n" "Error: Export was generated on a different Zulip version.\n"
f"Export={exported_migration_status['zulip_version']}\n" f"Export version: {exported_migration_status['zulip_version']}\n"
f"Server={local_migration_status['zulip_version']}\n" f"Server version: {local_migration_status['zulip_version']}\n"
"\n" "\n"
"Database formats differ between the exported realm and this server.\n" "Database formats differ between the exported realm and this server.\n"
"Printing migrations that differ between the versions:\n" "Printing migrations that differ between the versions:\n"
"--- exported realm\n" "--- exported realm\n"
"+++ this server" "+++ this server"
) + "\n".join(sorted_error_log) ) + "\n".join(sorted_error_log)
raise Exception(error_message) raise CommandError(error_message)

View File

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

View File

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

View File

@ -11,6 +11,7 @@ from unittest.mock import patch
import orjson import orjson
from django.conf import settings from django.conf import settings
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.management.base import CommandError
from django.db.models import Q, QuerySet from django.db.models import Q, QuerySet
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from typing_extensions import override from typing_extensions import override
@ -2199,7 +2200,7 @@ class RealmImportExportTest(ExportFile):
with ( with (
patch("zerver.lib.import_realm.ZULIP_VERSION", "8.0"), patch("zerver.lib.import_realm.ZULIP_VERSION", "8.0"),
self.assertRaises(Exception) as e, self.assertRaises(CommandError) as e,
self.assertLogs(level="INFO"), self.assertLogs(level="INFO"),
): ):
do_import_realm( do_import_realm(
@ -2207,9 +2208,9 @@ class RealmImportExportTest(ExportFile):
"test-zulip", "test-zulip",
) )
expected_error_message = ( expected_error_message = (
"Export was generated on a different Zulip major version.\n" "Error: Export was generated on a different Zulip major version.\n"
f"Export={ZULIP_VERSION}\n" f"Export version: {ZULIP_VERSION}\n"
"Server=8.0" "Server version: 8.0"
) )
self.assertEqual(expected_error_message, str(e.exception)) self.assertEqual(expected_error_message, str(e.exception))