fix: add safe decimal conversion to AttachmentConfig.from_dict and better error logging

- AttachmentConfig.from_dict now uses safe_decimal() for all decimal fields
- Added traceback logging to _load_saved_loadouts to see exact error location
- Handles int/float/Decimal/string values for all numeric fields
This commit is contained in:
LemonNexus 2026-02-09 20:20:58 +00:00
parent c7c16f65db
commit 97af63cbf9
1 changed files with 17 additions and 5 deletions

View File

@ -75,15 +75,25 @@ class AttachmentConfig:
@classmethod @classmethod
def from_dict(cls, data: dict) -> "AttachmentConfig": def from_dict(cls, data: dict) -> "AttachmentConfig":
"""Create from dictionary with safe decimal conversion."""
def safe_decimal(val):
if isinstance(val, Decimal):
return val
if isinstance(val, (int, float)):
return Decimal(str(val))
if isinstance(val, str):
return Decimal(val) if val else Decimal('0')
return Decimal('0')
return cls( return cls(
name=data['name'], name=data['name'],
item_id=data['item_id'], item_id=data['item_id'],
attachment_type=data['attachment_type'], attachment_type=data['attachment_type'],
decay_pec=Decimal(data['decay_pec']), decay_pec=safe_decimal(data.get('decay_pec', '0')),
damage_bonus=Decimal(data.get('damage_bonus', '0')), damage_bonus=safe_decimal(data.get('damage_bonus', '0')),
range_bonus=Decimal(data.get('range_bonus', '0')), range_bonus=safe_decimal(data.get('range_bonus', '0')),
efficiency_bonus=Decimal(data.get('efficiency_bonus', '0')), efficiency_bonus=safe_decimal(data.get('efficiency_bonus', '0')),
protection_bonus={k: Decimal(v) for k, v in data.get('protection_bonus', {}).items()}, protection_bonus={k: safe_decimal(v) for k, v in data.get('protection_bonus', {}).items()},
) )
@ -2371,7 +2381,9 @@ class LoadoutManagerDialog(QDialog):
item.setToolTip(tooltip) item.setToolTip(tooltip)
self.saved_list.addItem(item) self.saved_list.addItem(item)
except Exception as e: except Exception as e:
import traceback
logger.error(f"Failed to load {filepath}: {e}") logger.error(f"Failed to load {filepath}: {e}")
logger.error(traceback.format_exc())
continue continue
except Exception as e: except Exception as e:
logger.error(f"Failed to list loadouts: {e}") logger.error(f"Failed to list loadouts: {e}")