How to display a nice Error stack when receiving an error event?

Dear PyTango experts and users,

I’m still a newbee in PyTango, so sorry if I missed something obvious. :stuck_out_tongue:
I was trying to execute an old piece of code I found in some old training slides and I was trying to update the code to make it work with latest pytango and with python3.
I have been using latest itango conda package to try out the code.

I encountered a problem when trying to use the tango.Except.print_error_stack method with the errors received from an EventData object as parameter.

Here is the code:

import tango
import time
dev = DeviceProxy("sys/tg_test/1")
eve_id=dev.subscribe_event("double_scalar",tango.EventType.CHANGE_EVENT,1)
i=0
while (i < 60):
    i=i+1
    if not dev.is_event_queue_empty(eve_id):
        event_list=dev.get_events(eve_id)
        event_data=event_list[0]
        print("Event from device",event_data.device.dev_name())
        print("for attribute",event_data.attr_name)
        if event_data.err:
            print("Error sent with event:")
            tango.Except.print_error_stack(event_data.errors)
        else:
            if event_data.attr_value.quality == tango.AttrQuality.ATTR_INVALID:
                print ("Attribute data is invalid")
            else:
                print("Attribute value =", event_data.attr_value.value)
    time.sleep(1)

If I stop the TangoTest device server to generate an error event, here is what is displayed:

Event from device sys/tg_test/1
for attribute tango://mytangohost.esrf.fr:10000/sys/tg_test/1/double_scalar
Error sent with event:
ArgumentError: Python argument types in
    Except.print_error_stack(tuple)
did not match C++ signature:
    print_error_stack(Tango::DevErrorList)
(For more detailed information type: python_error)

It looks like tango.DevErrorList type does not exist in pytango so we cannot really use tango.Except.print_error_stack method.
Is there any alternative method to display nicely the errors received in an event?

Hi Reynald

Yes, I see the tango.DevErrorList type isn’t exposed in Python. I don’t see an easy way to use tango.Except.print_error_stack(). I didn’t even know it existed!

The standard string representation of the error objects will give you a nice way to see the errors:


def handler(event):
     if event.err:
         for error in event.errors:
             print(error)
     else:
         print("event OK!")

Example of usage and error. Here the device isn’t configured correctly, so we get errors (using stateless=True to keep getting errors via the event system).


>>> eid = dp.subscribe_event("double_scalar", tango.EventType.CHANGE_EVENT, handler, stateless=True)

DevError[
    desc = The polling (necessary to send events) for the attribute double_scalar is not started
  origin = DServer::event_subscription
  reason = API_AttributePollingNotStarted
severity = ERR]

DevError[
    desc = Failed to execute command_inout on device dserver/tangotest/test, command ZmqEventSubscriptionChange
  origin = Connection::command_inout()
  reason = API_CommandFailed
severity = ERR]

DevError[
    desc = Device server send exception while trying to register event
  origin = EventConsumer::connect_event()
  reason = API_DSFailedRegisteringEvent
severity = ERR]

If you are just doing simple testing, there is a built-in class that handles all types of events nicely. It just prints out the first error message in the list:


>>> handler = tango.utils.EventCallback()
>>> eid = dp.subscribe_event("double_scalar", tango.EventType.CHANGE_EVENT, handler, stateless=True)
2021-10-26 13:00:37.968679 SYS/TG_TEST/1 DOUBLE_SCALAR CHANGE [API_AttributePollingNotStarted] The polling (necessary to send events) for the attribute double_scalar is not started


Regards,
Anton

1 Like