Skip to content

Commit fd89443

Browse files
committed
WIP: Catch and log decimal conversion issues
1 parent f868ca7 commit fd89443

File tree

1 file changed

+12
-2
lines changed
  • src/frequenz/client/electricity_trading

1 file changed

+12
-2
lines changed

src/frequenz/client/electricity_trading/_types.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,14 @@ def from_pb(cls, price: price_pb2.Price) -> Self:
100100
Returns:
101101
Price object corresponding to the protobuf message.
102102
"""
103+
price_value = price.amount.value
104+
try:
105+
amount = Decimal(price_value)
106+
except Exception as e:
107+
_logger.error("Error converting price amount `%s`: %s", price_value, e)
108+
amount = Decimal("NaN")
103109
return cls(
104-
amount=Decimal(price.amount.value),
110+
amount=amount,
105111
currency=Currency.from_pb(price.currency),
106112
)
107113

@@ -140,7 +146,11 @@ def from_pb(cls, power: power_pb2.Power) -> Self:
140146
Returns:
141147
Power object corresponding to the protobuf message.
142148
"""
143-
return cls(mw=Decimal(power.mw.value))
149+
try:
150+
return cls(mw=Decimal(power.mw.value))
151+
except Exception as e:
152+
_logger.error("Error converting power amount `%s`: %s", power.mw.value, e)
153+
return cls(mw=Decimal("NaN"))
144154

145155
def to_pb(self) -> power_pb2.Power:
146156
"""Convert a Power object to protobuf Power.

0 commit comments

Comments
 (0)