RabbitMQ pika: SelectConnection catching all errors and hiding their details

Viewed 22

For some reason SelectConnection seems to catch all errors and the only way to debug them is to use add_on_close_callback, then the callback gets the exception instance:

The callback will be passed the connection and an exception instance. The exception will either be an instance of exceptions.ConnectionClosed if a fully-open connection was closed by user or broker or exception of another type that describes the cause of connection closure/failure.

Except that exception instance is always a StreamLostError which hides the original cause of the exception. This is annoying for debugging because it hides the context.

Is there a way to just make it raise the exception to the top like BlockingConnection does or at least get the full traceback information?

Example:

import pika

class AsyncServer():
    def __init__(self):
        self._channel = None

    def run(self):
        self._connection = self._connect()
        self._connection.ioloop.start()

    def _connect(self) -> pika.SelectConnection:
        return pika.SelectConnection(
            parameters=pika.ConnectionParameters(host='localhost'),
            on_open_callback=self._on_connection_open,
            on_close_callback=self._on_connection_closed
        )

    def _on_connection_open(self, connection):
        # Cause an error
        x = 1/0

    def _on_connection_closed(self, connection, reason):
        print("Reason for closing: ",reason)
        print("Exception class: ",reason.__class__)
        self._connection.ioloop.stop()


if __name__ == '__main__':
    recv = AsyncServer()
    recv.run()

It gives this output instead of showing a stack trace from the x = 1/0 line:

Reason for closing:  Stream connection lost: ZeroDivisionError('division by zero')
Exception class:  <class 'pika.exceptions.StreamLostError'>
0 Answers
Related