From 103967870e382205970d2d9333afec47e4c562ac Mon Sep 17 00:00:00 2001 From: goodboy Date: Tue, 3 Mar 2026 20:23:12 -0500 Subject: [PATCH] Handle `str`-errors in `.ib.broker` trade events Add `isinstance()` dispatch for the `'error'` event case in `deliver_trade_events()` to handle `ib_async` sometimes emitting plain `str` error items instead of the previously expected `dict`. Deats, - add `isinstance(err, dict)` branch for the standard case with `error_code`, `reason`, and `reqid` fields. - add `isinstance(err, str)` branch to parse error strings of the form `'[code 104] connection failed'` into `code` and `reason`. - set `reqid: str = ''` for string-form errors since there's no request ID available. - update `err` type annot to `dict|str`. (this commit msg was generated in some part by [`claude-code`][claude-code-gh]) [claude-code-gh]: https://github.com/anthropics/claude-code --- piker/brokers/ib/broker.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/piker/brokers/ib/broker.py b/piker/brokers/ib/broker.py index af6af5e8..0705fc21 100644 --- a/piker/brokers/ib/broker.py +++ b/piker/brokers/ib/broker.py @@ -1291,13 +1291,23 @@ async def deliver_trade_events( case 'error': # NOTE: see impl deats in # `Client.inline_errors()::push_err()` - err: dict = item + err: dict|str = item - # never relay errors for non-broker related issues + # std case, never relay errors for non-order-control + # related issues. # https://interactivebrokers.github.io/tws-api/message_codes.html - code: int = err['error_code'] - reason: str = err['reason'] - reqid: str = str(err['reqid']) + if isinstance(err, dict): + code: int = err['error_code'] + reason: str = err['reason'] + reqid: str = str(err['reqid']) + + # XXX, sometimes you'll get just a `str` of the form, + # '[code 104] connection failed' or something.. + elif isinstance(err, str): + code_part, _, reason = err.rpartition(']') + if code_part: + _, _, code = code_part.partition('[code') + reqid: str = '' # "Warning:" msg codes, # https://interactivebrokers.github.io/tws-api/message_codes.html#warning_codes