Error handling (#5923)

* More error handling when loading label app

* More error handling when loading report app
This commit is contained in:
Oliver 2023-11-15 23:11:38 +11:00 committed by GitHub
parent 928e00e19c
commit 538a01c500
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 16 deletions

View File

@ -10,10 +10,10 @@ from pathlib import Path
from django.apps import AppConfig from django.apps import AppConfig
from django.conf import settings from django.conf import settings
from django.core.exceptions import AppRegistryNotReady from django.core.exceptions import AppRegistryNotReady
from django.db.utils import OperationalError from django.db.utils import IntegrityError, OperationalError, ProgrammingError
from InvenTree.ready import (canAppAccessDatabase, isInMainThread, from InvenTree.ready import (canAppAccessDatabase, isImportingData,
isPluginRegistryLoaded) isInMainThread, isPluginRegistryLoaded)
logger = logging.getLogger("inventree") logger = logging.getLogger("inventree")
@ -40,11 +40,10 @@ class LabelConfig(AppConfig):
if not isPluginRegistryLoaded() or not isInMainThread(): if not isPluginRegistryLoaded() or not isInMainThread():
return return
if canAppAccessDatabase(allow_test=False): if canAppAccessDatabase(allow_test=False) and not isImportingData():
try: try:
self.create_labels() # pragma: no cover self.create_labels() # pragma: no cover
except (AppRegistryNotReady, OperationalError): except (AppRegistryNotReady, IntegrityError, OperationalError, ProgrammingError):
# Database might not yet be ready # Database might not yet be ready
warnings.warn('Database was not ready for creating labels', stacklevel=2) warnings.warn('Database was not ready for creating labels', stacklevel=2)

View File

@ -3,10 +3,13 @@
import logging import logging
import os import os
import shutil import shutil
import warnings
from pathlib import Path from pathlib import Path
from django.apps import AppConfig from django.apps import AppConfig
from django.conf import settings from django.conf import settings
from django.core.exceptions import AppRegistryNotReady
from django.db.utils import IntegrityError, OperationalError, ProgrammingError
logger = logging.getLogger("inventree") logger = logging.getLogger("inventree")
@ -17,8 +20,8 @@ class ReportConfig(AppConfig):
def ready(self): def ready(self):
"""This function is called whenever the report app is loaded.""" """This function is called whenever the report app is loaded."""
from InvenTree.ready import (canAppAccessDatabase, isInMainThread, from InvenTree.ready import (canAppAccessDatabase, isImportingData,
isPluginRegistryLoaded) isInMainThread, isPluginRegistryLoaded)
# skip loading if plugin registry is not loaded or we run in a background thread # skip loading if plugin registry is not loaded or we run in a background thread
if not isPluginRegistryLoaded() or not isInMainThread(): if not isPluginRegistryLoaded() or not isInMainThread():
@ -29,7 +32,9 @@ class ReportConfig(AppConfig):
logging.getLogger('weasyprint').setLevel(logging.WARNING) logging.getLogger('weasyprint').setLevel(logging.WARNING)
# Create entries for default report templates # Create entries for default report templates
if canAppAccessDatabase(allow_test=False): if canAppAccessDatabase(allow_test=False) and not isImportingData():
try:
self.create_default_test_reports() self.create_default_test_reports()
self.create_default_build_reports() self.create_default_build_reports()
self.create_default_bill_of_materials_reports() self.create_default_bill_of_materials_reports()
@ -37,6 +42,9 @@ class ReportConfig(AppConfig):
self.create_default_sales_order_reports() self.create_default_sales_order_reports()
self.create_default_return_order_reports() self.create_default_return_order_reports()
self.create_default_stock_location_reports() self.create_default_stock_location_reports()
except (AppRegistryNotReady, IntegrityError, OperationalError, ProgrammingError):
# Database might not yet be ready
warnings.warn('Database was not ready for creating reports', stacklevel=2)
def create_default_reports(self, model, reports): def create_default_reports(self, model, reports):
"""Copy default report files across to the media directory.""" """Copy default report files across to the media directory."""