Catching connection error in Autobahn Component

251 Views Asked by At

I'm using Autobahn Python to connect to a Crossbar router. I'm hoping to handle the case where the crossbar router isn't available on the network. When using a class based Component I'm able to handle connection error however when using the functional Component I'm not able to capture and handle these exceptions.

from autobahn.asyncio.component import Component

component = Component(transports="no_existent_url:3000",
                          realm=realm, authentication=authentication)

component.on('join', onJoinHandler)
# Called on exception when connected
component.on('leave', onLeaveHandler)
# Never called
component.on('connectfailure', onFailureHandler)
# Never called
component.on('disconnect', onDisconnectHandler)

component.start()

The errors are present in the console output:

Error in connection Multiple exceptions: [Errno 111] Connect call failed ('no_existent_url', 3000), [Errno 99] Cannot assign requested address
Connection failed: OSError: Multiple exceptions: [Errno 111] Connect call failed ('no_existent_url', 3000), [Errno 99] Cannot assign requested address
Component failed: Exhausted all transport connect attempts
1

There are 1 best solutions below

0
Linus On

Component.start() returns a future which resolves when the component is either done or exits (source code in component.py). Awaiting this future and checking the .exception() method allows you to catch the exception.

from autobahn.asyncio.component import Component

component = Component(transports="no_existent_url:3000",
                          realm=realm, authentication=authentication)

component.on('join', onJoinHandler)
# Called on exception when connected
component.on('leave', onLeaveHandler)
# Never called
component.on('connectfailure', onFailureHandler)
# Never called
component.on('disconnect', onDisconnectHandler)

future = component.start()

result = await future
if result.exception() is not None:
  # Handler exception