Overview
This example demonstrates various error handling patterns in SemanticTest, including:- Detecting validation failures
- Handling API errors
- Retry logic with Loop blocks
- Graceful degradation
Testing Error Scenarios
test.json
Pattern 1: Validate Then Assert
Always validate data before making assertions:Pattern 2: Graceful Degradation
Test that your system handles missing data gracefully:Pattern 3: Retry Logic
Retry failed operations with exponential backoff:Pattern 4: Expected Failures
Test that your system correctly rejects invalid input:Pattern 5: Multiple Validation Layers
Implement defense in depth:Pattern 6: Error Messages
Validate error messages are helpful:Pattern 7: Timeout Handling
Test timeout scenarios:Real-World Example
Complete error handling for API integration:Best Practices
1. Always Validate Before Asserting
1. Always Validate Before Asserting
Use ValidateContent or ValidateTools before making assertions on data structure.
2. Test Both Success and Failure
2. Test Both Success and Failure
Write tests for both happy paths and error scenarios.
3. Use Specific Error Assertions
3. Use Specific Error Assertions
Assert on specific error codes, not just “something failed”.
4. Implement Retry Logic
4. Implement Retry Logic
Use Loop blocks to retry flaky operations automatically.
5. Validate Error Messages
5. Validate Error Messages
Ensure error messages are helpful and don’t leak sensitive info.