pythonclickhousepython-db-api

Can't catch connection error clickhouse-driver db api


I'm trying to catch a connection error when connecting to kx using the clickhouse-driver db api in python. But for some reason, the try: block passes without errors, and I don't get exception

def __enter__(self):
try:
        self.connector = db.connect(dsn=self.connection_string)
    except Error as e:  # pass
        self.error = str(e)[:250]
        self.connector = None
    return self 

And the error only happens when execute sql is executed. Is there any way to get a connection error at the moment of connection?


Solution

  • I added execute('select 1'). Because only after execute we are creating instance of class, so after that I can get connection exception.

    def __enter__(self):
        try:
            self.connector = dbp.connect(dsn=self.connection_string)
            self.cur = self.connector.cursor()
            self.cur.execute('select 1')
        except dbp.Error as e:
            self.error = '\nConnection error occurred: ' + '\n' + str(e)[:250]
            self.connector = None
        return self