Exception handling and logging improvement completion report
[!NOTE] For the latest implementation status, please refer to Functional Implementation Status (Remaining Functionality).
Implementation details
✅ 1. Introducing a custom exception class
File: exceptions.py
Implemented exception hierarchy```
EvoSpikeNetError (基底クラス) ├── ConfigurationError │ ├── InvalidConfigError │ └── MissingConfigError ├── ModelError │ ├── ModelNotFoundError │ ├── ModelLoadError │ ├── ModelSaveError │ ├── InvalidModelStateError │ └── IncompatibleModelError ├── DataError │ ├── DataLoadError │ ├── DataValidationError │ ├── InvalidDataFormatError │ └── DataNotFoundError ├── TrainingError │ ├── TrainingInterruptedError │ ├── ConvergenceError │ └── GradientError ├── CommunicationError │ ├── ZenohError │ ├── NodeConnectionError │ ├── SynchronizationError │ └── TimeoutError ├── ResourceError │ ├── GPUError │ ├── MemoryError │ └── DiskSpaceError ├── EncodingError / DecodingError ├── PluginError ├── SecurityError ├── APIError ├── EvolutionError ├── DatabaseError └── ValidationError
**Total**: 40+ specialized exception classes
#### Main features
- **Preserving detailed information**: Save additional context in `details` dictionary
- **Exception Chain**: Keep the original exception with the `from` keyword
- **Utilities**: Support reraising exceptions with `reraise_with_context()`
### ✅ 2. Introducing a logging utility
**File**: `logging_utils.py`
#### Implemented features
1. **StructuredFormatter**: Structured log in JSON format
2. **ColoredFormatter**: Colored console output
3. **setup_logger()**: Bulk setup of loggers
4. **get_logger()**: Get logger
5. **LogContext**: Adding context information
6. **log_exception()**: Detailed logging of exceptions
7. **log_performance()**: Record performance metrics
#### Usage example```python
<!-- from evospikenet.logging_utils import get_logger, log_exception -->
<!-- TODO: update or remove - import fail<!-- Remember: Automatic conversion not possible — please fix manually -->ModelLoadError -->
logger = get_logger(__name__)
try:
model = load_model(path)
except FileNotFoundError as e:
raise ModelLoadError(
f"Model file not found: {path}",
details={"path": path}
) from e
✅ 3. Modification of existing code
Modified file:
- api_gateway.py
- except: → except (ValueError, TypeError)
models.py-
Added logging to
except:in destructor -
security.py -
Logging to
except:and specific exception types -
compression.py - Changed the multi-stage fallback of
_deserialize_data()to a specific exception type. -
Add logging at each step
-
sdk.py - Changed
except:in JSON parsing toexcept (ValueError, AttributeError)
✅ 4. Update package initialization
File: __init__.py
# First import exceptions and logging_utils
from . import exceptions
from .exceptions import (
EvoSpikeNetError,
ConfigurationError,
ModelError,
DataError,
TrainingError,
CommunicationError,
)
from . import logging_utils
from .logging_utils import get_logger, setup_logger, log_exception
✅ 5. CI/CD integration
File: ci.yml
- name: Check exception handling
run: |
python scripts/check_exceptions.py
continue-on-error: true
✅ 6. Create check script
File: check_exceptions.py
- Bare except (
except:) detection - Extensive
except Exception:detection - Identification of allowed patterns (
__del__,cleanup)
✅ 7. Best Practice Guide
File: docs/EXCEPTION_HANDLING_GUIDE.md
Covering 10 best practices: 1. Using custom exceptions 2. Avoiding Bare Exceptions 3. Using exception chains 4. Unified logging 5. Context manager 6. Rethrowing exceptions 7. Handling multiple exception types 8. Early return in validation 9. Exception handling in asynchronous code 10. Common error handling using decorators
Residual issues detected
Check script execution results:``` ❌ Found 46 issues: - Bare excepts: ~18件 - Broad exceptions: ~28件
Main points:
- `evospikenet/compression.py` - multiple bare except
- `evospikenet/rag_milvus.py` - Extensive Exception catch
- `evospikenet/attention.py` - Extensive Exception catch
- many other files
## Future improvement plans
### Short-term (immediate implementation recommended)
- [ ] Replace remaining bare except with specific exception type
- [ ] Changed broad `except Exception` to a specific list of exceptions.
- [ ] Added logging to all catch blocks
### Mid-term (next 1-2 weeks)
- [ ] Start using custom exceptions in real code
- [ ] Unify log records into structured format
- [ ] Added exception handling unit test
### Long term (next month)
- [ ] Best practices applied in all modules
- [ ] Performance log integration
- [ ] Considering introduction of distributed error tracking system
## Advantages
### 1. Debugging improvements
- Specific exception types clarify the cause of the error
- Stack traces and detailed information for faster problem resolution
### 2. Improved error handling
- Distinguish between expected and unexpected errors
- Easy to implement appropriate recovery strategies
### 3. Improved maintainability
- Unified log format
- Clarify the intent of the code
### 4. Production environment stability
- Prevents erroneous capture of KeyboardInterrupt and SystemExit
- Proper error propagation
## Usage Guide
### Using custom exceptions```python
<!-- Module 'evospikenet' not found. Check moves/renames within the package -->
<!-<!-- Remember: Cannot convert automatically — please fix it manually -->
try:
import inspect
load_kwargs = {"map_location": "cpu"}
try:
sig = inspect.signature(torch.load)
if "weights_only" in sig.parameters:
load_kwargs["weights_only"] = True
except Exception:
pass
model = torch.load(path, **load_kwargs)
except FileNotFoundError as e:
raise ModelLoadError(
f"Model file not found: {path}",
details={"path": path, "cwd": os.getcwd()}
) from e
Logging```python
kenet.logging_utils import g risky_operation() except DataValidationError as e: log_exception( logger, e, message="Validation failed", extra_context={"input_size": len(data)} ) raise
### Check command```bash
# Detect exception handling issues
python scripts/check_exceptions.py
# Automated execution with CI/CD
# Already integrated in .github/workflows/ci.yml
Reference documents
exceptions.py- Custom exception definitionlogging_utils.py- Logging utilities- docs/EXCEPTION_HANDLING_GUIDE.md - Best practices
check_exceptions.py- Check tool
Implementation date: January 10, 2026 Status: ✅ Foundation completed (continued phased migration)