mariadb-corporation / mariadb-connector-nodejs

MariaDB Connector/Node.js is used to connect applications developed on Node.js to MariaDB and MySQL databases. MariaDB Connector/Node.js is LGPL licensed.
GNU Lesser General Public License v2.1
366 stars 91 forks source link

What is the best way to handle a connection failure when using createPool. ? #200

Closed alexporto2200 closed 2 years ago

alexporto2200 commented 2 years ago

Today I'm dealing with the following way, but I can't capture the expectation. Sorry for my lack of experience.

export class MariadbMananger {
    public pool: Pool;
    constructor(parametersConnection: ParametersMariadbMananger | null) {
        try {
            if (parametersConnection) {
                this.pool = mariadb.createPool(parametersConnection);
            } else {
                this.pool = mariadb.createPool({
                    host: Config.mariadb.host,
                    user: Config.mariadb.user,
                    password: Config.mariadb.password,
                    port: Config.mariadb.port,
                    database: Config.mariadb.database,
                    connectionLimit: 20,
                    ssl: {
                        ca: Config.mariadb.cacert,
                        cert: Config.mariadb.clientcert,
                        key: Config.mariadb.clientkey,
                        rejectUnauthorized: false
                    }
                })
            }
        } catch (e) {
            this.pool = {} as Pool
            console.log('Error connection mariadb')
        }
    }
}

And I always get the error.

Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1146:16)
 From event:
    at _registerHandshakeCmd (/nsource/nsource/node_modules/mariadb/lib/connection.js:745:11)
    at /nsource/nsource/node_modules/mariadb/lib/connection.js:57:11
    at new Promise (<anonymous>)
    at Connection.connect (/nsource/nsource/node_modules/mariadb/lib/connection.js:56:16)
    at createConnectionPoolPromise (/nsource/nsource/node_modules/mariadb/lib/pool-promise.js:31:8)
    at creationTryout (/nsource/nsource/node_modules/mariadb/lib/pool-base.js:373:9)
    at listOnTimeout (node:internal/timers:557:17)
    at processTimers (node:internal/timers:500:7) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3306,
  fatal: true
}

I would like to handle the error, log a specific file and ignore it.

rusher commented 2 years ago

please use stackoverflow for question like this. ECONNREFUSED => port is not open. you have no mariadb server on 127.0.0.1:3306

alexporto2200 commented 2 years ago

@rusher I think I didn't explain it right. I know the bank is not working. I stopped him. I would just like to capture the error it generates, from connection ECONNREFUSED in the api, so this error is printed directly on the console and does not enter the exception. would there be any way to capture this through the exception?

whitebyte commented 2 years ago

Could be related to https://github.com/mariadb-corporation/mariadb-connector-nodejs/issues/180