Handle errors in vendor stock updates and inactive vendors

Add error handling during the stock update process to log and skip problematic vendors without halting execution. Additionally, ensure `get_vendor_instance` only returns active vendors, raising an error if the vendor is inactive. This improves system resilience and data integrity.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-05-07 03:15:23 +03:00
parent 6e19d9f65f
commit 87e124f614
2 changed files with 8 additions and 2 deletions

View file

@ -42,7 +42,10 @@ def update_products_task():
for vendor_class in vendors_classes:
vendor = vendor_class()
try:
vendor.update_stock()
except Exception as e:
logger.warning(f"Skipping {vendor_class} due to error: {e!s}")
delete_stale()

View file

@ -148,7 +148,10 @@ class AbstractVendor:
def get_vendor_instance(self):
try:
return Vendor.objects.get(name=self.vendor_name)
vendor = Vendor.objects.get(name=self.vendor_name)
if vendor.is_active:
return vendor
raise VendorError(f"Vendor {self.vendor_name!r} is inactive...")
except Vendor.DoesNotExist:
raise Exception(f"No matching vendor found with name {self.vendor_name!r}...")