Skip to content

Python SDK error handling fails to report underlying OpenFGA exception #184

Open
@aryehklein

Description

@aryehklein

Checklist

  • I have looked into the README and have not found a suitable solution or answer.
  • I have looked into the documentation and have not found a suitable solution or answer.
  • I have searched the issues and have not found a suitable solution or answer.
  • I have upgraded to the latest version of OpenFGA and the issue still persists.
  • I have searched the Slack community and have not found a suitable solution or answer.
  • I agree to the terms within the OpenFGA Code of Conduct.

Description

Using the sample code:

class OpenFgaFacade:
    """
    Facade class for interacting with OpenFGA authorization service.
    """

    def __init__(self, api_url=None):
        """
        Initialize the OpenFGA facade.

        Args:
            api_url: The OpenFGA API URL. If not provided, it will be read from FGA_API_URL environment variable.
        """
        self.api_url = api_url or os.environ.get("FGA_API_URL", "http://localhost:8085/")
        # Defer client creation until needed
        self.client = None

    async def initialize(self):
        """
        Asynchronously initialize the OpenFGA client with store and authorization model.
        This method must be called before using the facade.
        """
        configuration = openfga_sdk.ClientConfiguration(
            api_url=self.api_url,
            store_id=os.environ.get("FGA_STORE_ID"),
            authorization_model_id=os.environ.get("FGA_AUTHORIZATION_MODEL_ID"),
        )
        # Defer client creation until needed
        self.client = AsyncOpenFgaClient(configuration)
        await self.client.read_authorization_models()

    async def write_tuple(self):
        """
        Write a tuple to the OpenFGA store.
        
        This is a sample method that writes a test tuple.
        In production, this would be replaced with actual authorization logic.
        """

        body = ClientWriteRequest(
            writes=[
                ClientTuple(
                    user="user:anne",
                    relation="viewer",
                    object="document:Z",
                ),
            ],
        )
        response = await self.client.write(body)

I am getting an error:

AttributeError: 'ClientResponse' object has no attribute 'data'

I expect an error to be thrown here as my OpenFGA model doesnt have a document object, but the actual error thrown isnt an OpenFGA one. The error seems to be coming from https://github.com/openfga/python-sdk/blame/172cd2671d6d54c1ed46f86eb1c00229c3704f60/openfga_sdk/exceptions.py#L126. It looks like the aiohttp clientresponse doesnt have a data field, hence the error.

When I use the cli instead of the python SDK I get the expected error message

Expectation

The python SDK should use the correct field from the aiohttp clientresponse so that the underlying OpenFGA error is returned to the caller.

Reproduction

  1. Create a python client
  2. Call write using a tuple that isnt defined in the relationship model

OpenFGA SDK version

0.9.2

OpenFGA version

0.9.2

SDK Configuration

See vode above

Logs

No response

References

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

Status

Ready

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions