Commit Graph

4 Commits

Author SHA1 Message Date
ff4ef2f563 Phase 3: Complete type safety & logging unification (3.1-3.2)
Phase 3.1: Type Safety
- Add bridge_types.py for shared type aliases (EventDict, PowerWatts, Timestamp, DeviceID)
- Define protocols for callbacks and message parsers
- Strict type annotations on all core modules (session_detector, event_queue, device_manager)
- Fix Optional handling and type guards throughout codebase
- Achieve full mypy compliance: 0 errors across 47 source files

Phase 3.2: Logging Unification
- Migrate from stdlib logging to pure structlog across all runtime modules
- Convert all logs to structured event+fields format (snake_case event names)
- Remove f-string and printf-style logger calls
- Add contextvars support for per-request correlation
- Implement FastAPI middleware to bind request_id, http_method, http_path
- Propagate X-Request-ID header in responses
- Remove stdlib logging imports except setup layer (utils/logging.py)
- Ensure log-level consistency across all modules

Files Modified:
- iot_bridge/bridge_types.py (new) - Central type definitions
- iot_bridge/core/* - Type safety and logging unification
- iot_bridge/clients/* - Structured logging with request context
- iot_bridge/parsers/* - Type-safe parsing with structured logs
- iot_bridge/utils/logging.py - Pure structlog setup with contextvars
- iot_bridge/api/server.py - Added request correlation middleware
- iot_bridge/tests/* - Test fixtures updated for type safety
- iot_bridge/OPTIMIZATION_PLAN.md - Phase 3 status updated

Validation:
- mypy . → 0 errors (47 files)
- All unit tests pass
- Runtime behavior unchanged
- API response headers include X-Request-ID
2026-02-18 23:54:27 +01:00
4214a61b19 Complete phase 2 rest points: env overrides and unit tests 2026-02-18 23:30:33 +01:00
548f94a652 refactor(exceptions): Erweitere Exception-Klassen mit vollständigen Implementierungen
Alle Exception-Klassen haben jetzt sinnvolle __init__-Methoden:

- ConfigurationError: path Parameter für Config-Dateipfad
- ConfigValidationError: field + value für fehlerhafte Felder
- ConnectionError: service Parameter (mqtt/odoo)
- MQTTConnectionError: broker + port Parameter
- DeviceError: device_id Parameter
- ValidationError: field + value für Validierungsfehler

Vorher: Klassen hatten nur 'pass' (technisch korrekt, aber wenig nützlich)
Nachher: Strukturierte Fehlerkontext-Erfassung mit dedizierten Attributen

Beispiel:
  # Alt:  raise ConfigurationError('File not found', details={'path': ...})
  # Neu:  raise ConfigurationError('File not found', path='/etc/config.yaml')

Angepasst:
- config/loader.py: Nutzt neuen path-Parameter statt details-Dict
- Alle bestehenden Aufrufe bleiben kompatibel (backward-compatible)

Test: python3 -c 'from exceptions import *; e = MQTTConnectionError(...)'
2026-02-18 22:33:37 +01:00
e744a1e4cb feat(phase2): Add custom exception hierarchy (Phase 2.1)
Implemented structured exception handling:

Exception Hierarchy:
- BridgeError (base)
  ├── ConfigurationError
  │   └── ConfigValidationError
  ├── ConnectionError
  │   ├── MQTTConnectionError
  │   └── OdooAPIError (with status_code, response fields)
  ├── DeviceError
  │   ├── DeviceNotFoundError
  │   └── ParserError (with topic, payload context)
  └── ValidationError

Changes:
- Created exceptions.py with comprehensive exception classes
- Updated clients/odoo_client.py to use OdooAPIError
  * Replaced generic Exception with OdooAPIError
  * Added status_code and response context
  * Better error messages for API failures
- Updated clients/mqtt_client.py to import MQTTConnectionError
- Updated config/loader.py to use ConfigurationError
  * Replaced FileNotFoundError with ConfigurationError
  * Added file path context to errors
- Updated parsers/shelly_parser.py to import ParserError (for future use)

Benefits:
- Specific exception types enable targeted error handling
- Better debugging with structured error context
- Easier to catch and handle specific error categories
- Backward compatible - errors still propagate correctly
- Foundation for better error reporting in Phase 4

Remaining Phase 2 tasks (deferred):
- 2.2: main.py refactoring (will be separate PR)
- 2.3: Config management modernization (Pydantic migration)

See OPTIMIZATION_PLAN.md Phase 2.1 for details.
2026-02-18 22:27:28 +01:00