Skip to content

Conversation

@SoulPancake
Copy link
Owner

No description provided.

…methods

This commit significantly improves error handling in the Python SDK by adding:

1. Convenience Properties:
   - Direct access to error code via e.code (instead of e.parsed_exception.code)
   - Direct access to error message via e.error_message
   - Direct access to request ID via e.request_id
   - Direct access to store ID via e.store_id
   - Direct access to authorization model ID via e.authorization_model_id

2. Operation Context:
   - Added operation_name parameter to ApiException and all subclasses
   - Exceptions now track which operation failed (e.g., "Check", "Write")
   - operation_name propagated through api_client call stack

3. Helper Methods:
   - is_validation_error() - Check if error is a validation error
   - is_not_found_error() - Check if error is a not found error
   - is_authentication_error() - Check if error is an authentication error
   - is_authorization_error() - Check if error is an authorization error
   - is_rate_limit_error() - Check if error is a rate limit error
   - is_server_error() - Check if error is a server error
   - is_retryable() - Check if error should be retried

4. Enhanced Error Messages:
   - __str__ method now includes operation name, error code, message,
     request ID, store ID, and authorization model ID
   - More readable and informative error output

5. Testing:
   - Added comprehensive unit tests (17 test cases)
   - Added integration tests against real OpenFGA server
   - Docker Compose setup for integration testing
   - Test documentation and run script

Changes are fully backwards compatible - existing code continues to work.

Files modified:
- openfga_sdk/exceptions.py: Enhanced ApiException and subclasses
- openfga_sdk/api_client.py: Added operation_name parameter propagation
- openfga_sdk/sync/api_client.py: Added operation_name parameter propagation

Files added:
- test/error_handling_improvements_test.py: Unit tests
- test/integration_error_handling_test.py: Integration tests
- docker-compose.integration-test.yml: Test infrastructure
- run_integration_tests.sh: Helper script
- test/README_INTEGRATION_TESTS.md: Test documentation
Previously, operation_name parameter was added but never populated.
This commit fixes that by extracting the operation name from telemetry
attributes that are already passed to every API call.

Changes:
- api_client.py: Extract operation_name from telemetry if not provided
- sync/api_client.py: Same for synchronous client
- Integration tests: Verify operation names are set correctly
- New tests: Verify telemetry extraction logic

How it works:
1. Generated open_fga_api.py already sets telemetry attributes with method name
2. api_client.__call_api() extracts fga_client_request_method from telemetry
3. Operation name is automatically set on all exceptions
4. No changes needed to generated code

Examples:
- check() call -> exception.operation_name = "check"
- write() call -> exception.operation_name = "write"
- batch_check() call -> exception.operation_name = "batch_check"

This matches the Java SDK implementation pattern where operation names
are automatically tracked without modifying generated API code.

Tests: 21 unit tests passing
…perties

Based on user feedback, direct access to parsed_exception has been hidden
to provide a cleaner, more intentional API design.

Changes:
- exceptions.py: parsed_exception getter now raises AttributeError with helpful message
- exceptions.py: parsed_exception setter kept for internal use by api_client
- Convenience properties (code, error_message, etc.) remain fully functional
- Tests updated to use convenience properties instead of parsed_exception
- Documentation updated to remove parsed_exception examples

Design rationale:
- Encourages using cleaner convenience properties (e.code, e.error_message)
- Prevents users from accessing internal parsed response object
- Setter still works for api_client.py to populate error details internally
- More intentional API surface with better user experience

User Impact:
- Code using e.parsed_exception.code will need to change to e.code
- Code using e.header.get('fga-request-id') continues to work
- All convenience properties work as before

Tests: 17 unit tests passing
Reverted breaking change - parsed_exception is now accessible again.
This makes the changes purely additive with zero breaking changes.

What works (backward compatible):
- e.parsed_exception.code - Still works!
- e.parsed_exception.message - Still works!
- e.header.get('fga-request-id') - Still works!

What's new (additive):
- e.code - Convenience property
- e.error_message - Convenience property
- e.request_id - Convenience property
- e.store_id - Convenience property
- e.authorization_model_id - Convenience property
- e.operation_name - Auto-extracted from telemetry
- e.is_validation_error() - Helper method
- e.is_retryable() - Helper method
- etc.

Tests: 21 unit tests passing
- Removed inner function imports of TelemetryAttributes
- Top-level import already exists, use it directly
- Fixes ruff F401 linting errors

Linting: All ruff checks pass
Tests: 21 unit tests passing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants