Exception handling (#5622)

* get_setting_object: Handle ProgrammingError

* Improved exception handling

- Address a few cases on application startup if the database is not ready (migrations not applied)
This commit is contained in:
Oliver 2023-09-27 13:42:20 +10:00 committed by GitHub
parent 2b0d81fd81
commit 08e7190832
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 13 deletions

View File

@ -71,7 +71,10 @@ class InvenTreeConfig(AppConfig):
return
# Remove any existing obsolete tasks
Schedule.objects.filter(func__in=obsolete).delete()
try:
Schedule.objects.filter(func__in=obsolete).delete()
except Exception:
logger.error("Failed to remove obsolete tasks - database not ready")
def start_background_tasks(self):
"""Start all background tests for InvenTree."""

View File

@ -31,7 +31,7 @@ from django.core.validators import (MaxValueValidator, MinValueValidator,
URLValidator)
from django.db import models, transaction
from django.db.models.signals import post_delete, post_save
from django.db.utils import IntegrityError, OperationalError
from django.db.utils import IntegrityError, OperationalError, ProgrammingError
from django.dispatch.dispatcher import receiver
from django.urls import reverse
from django.utils.timezone import now
@ -487,7 +487,7 @@ class BaseInvenTreeSetting(models.Model):
setting = settings.filter(**filters).first()
except (ValueError, cls.DoesNotExist):
setting = None
except (IntegrityError, OperationalError):
except (IntegrityError, OperationalError, ProgrammingError):
setting = None
# Setting does not exist! (Try to create it)
@ -512,7 +512,7 @@ class BaseInvenTreeSetting(models.Model):
# Wrap this statement in "atomic", so it can be rolled back if it fails
with transaction.atomic():
setting.save(**kwargs)
except (IntegrityError, OperationalError):
except (IntegrityError, OperationalError, ProgrammingError):
# It might be the case that the database isn't created yet
pass
except ValidationError:

View File

@ -182,8 +182,11 @@ class LabelConfig(AppConfig):
shutil.copyfile(src_file, dst_file)
# Check if a label matching the template already exists
if model.objects.filter(label=filename).exists():
return # pragma: no cover
try:
if model.objects.filter(label=filename).exists():
return # pragma: no cover
except Exception:
logger.error(f"Failed to query label for '{filename}' - you should run 'invoke update' first!")
logger.info(f"Creating entry for {model} '{label['name']}'")

View File

@ -55,12 +55,15 @@ class PartConfig(AppConfig):
if isImportingData():
return
items = PartPricing.objects.filter(scheduled_for_update=True)
try:
items = PartPricing.objects.filter(scheduled_for_update=True)
if items.count() > 0:
# Find any pricing objects which have the 'scheduled_for_update' flag set
print(f"Resetting update flags for {items.count()} pricing objects...")
if items.count() > 0:
# Find any pricing objects which have the 'scheduled_for_update' flag set
logger.info(f"Resetting update flags for {items.count()} pricing objects...")
for pricing in items:
pricing.scheduled_for_update = False
pricing.save()
for pricing in items:
pricing.scheduled_for_update = False
pricing.save()
except Exception:
logger.error("Failed to reset pricing flags - database not ready")