frequenz-floss / frequenz-api-electricity-trading

gRPC+protobuf specification and Python bindings for the Frequenz Electricity Trading API
MIT License
0 stars 6 forks source link

Error in API Response Handling #51

Closed matthias-wende-frequenz closed 1 month ago

matthias-wende-frequenz commented 4 months ago

What happened?

We have encountered an issue with our Python code where it is attempting to perform a decimal conversion on a field from the response of an API call, but the field is None. This has resulted in a decimal.InvalidOperation error.

Error Message:

amount=Decimal(price.amount.value),
^^^^^^^^^^^^^^^^^^^^^^^^^^
decimal.InvalidOperation: <class 'decimal.ConversionSyntax'>

What did you expect instead?

Handle the case that not all necessary fields are present in the API response before attempting any conversions or manipulations.

Affected version(s)

No response

Affected part(s)

I don't know (part:❓)

Extra information

No response

matthias-wende-frequenz commented 4 months ago

Here is a full traceback from a call to create_gridpool_order which returns a None for the Order field.

Traceback (most recent call last):
  File "/home/matthias/workspace/tests/python/etrading/test.py", line 41, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/matthias/workspace/tests/python/etrading/test.py", line 28, in main
    order_detail = await client.create_gridpool_order(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matthias/workspace/frequenz/api/frequenz-api-electricity-trading/py/frequenz/client/electricity_trading/_client.py", line 276, in create_gridpool_order
    return OrderDetail.from_pb(response.order_detail)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matthias/workspace/frequenz/api/frequenz-api-electricity-trading/py/frequenz/client/electricity_trading/_types.py", line 1099, in from_pb
    order=Order.from_pb(order_detail.order),
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matthias/workspace/frequenz/api/frequenz-api-electricity-trading/py/frequenz/client/electricity_trading/_types.py", line 887, in from_pb
    price=Price.from_pb(order.price),
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matthias/workspace/frequenz/api/frequenz-api-electricity-trading/py/frequenz/client/electricity_trading/_types.py", line 104, in from_pb
    amount=Decimal(price.amount.value),
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]