Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't handle db connection error #1

Open
sashsvamir opened this issue Jun 27, 2018 · 5 comments
Open

can't handle db connection error #1

sashsvamir opened this issue Jun 27, 2018 · 5 comments

Comments

@sashsvamir
Copy link

I'm using mysql-query-promise with koa. When mysql has failed to connect to db, nodejs has been exited.

Error: ER_DBACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' to database 'dbtest'
    at Handshake.Sequence._packetToError (...../koa/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
    at Handshake.ErrorPacket (...../koa/node_modules/mysql/lib/protocol/sequences/Handshake.js:130:18)
    at Protocol._parsePacket (...../koa/node_modules/mysql/lib/protocol/Protocol.js:279:23)
    at Parser.write (...../koa/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (...../koa/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (...../koa/node_modules/mysql/lib/Connection.js:103:28)
    at Socket.emit (events.js:180:13)
    at addChunk (_stream_readable.js:274:12)
    at readableAddChunk (_stream_readable.js:261:11)
    at Socket.Readable.push (_stream_readable.js:218:10)
    --------------------
    at Protocol._enqueue (...../koa/node_modules/mysql/lib/protocol/Protocol.js:145:48)
    at Protocol.handshake (...../koa/node_modules/mysql/lib/protocol/Protocol.js:52:23)
    at PoolConnection.connect (...../koa/node_modules/mysql/lib/Connection.js:130:18)
    at Pool.getConnection (...../koa/node_modules/mysql/lib/Pool.js:48:16)
    at PoolCluster._getConnection (...../koa/node_modules/mysql/lib/PoolCluster.js:229:13)
    at PoolNamespace.getConnection (...../koa/node_modules/mysql/lib/PoolNamespace.js:40:11)
    at ...../koa/node_modules/mysql/lib/PoolNamespace.js:45:17
    at ...../koa/node_modules/mysql/lib/PoolCluster.js:232:7
    at Handshake.onConnect [as _callback] (...../koa/node_modules/mysql/lib/Pool.js:58:9)
    at Handshake.Sequence.end (...../koa/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)
[nodemon] app crashed - waiting for file changes before starting...

I want to handle connection error, so i tried to handle error on koa middleware (see below), koa event handler and on mysql-query-promise promise catch handler (see below), but any of this both methods not executes on connection error and node has falled.

// koa middleware error handler
app.use((ctx, next) => {
  try {
    await next()
  } catch (err) {
    console.error(err)
  }
})

// koa event error handler
app.on('error', err => {
  console.log(err)
})

// mysql-query-promise error handler
const query = require('mysql-query-promise')
const q = query(`SELECT * from ${tableName}`, [String(token)])
await q
  .then((rows) => {
    // success
  }, (err) => {
    console.log('CATCH ERROR!')
  })

How can i handle mysql-query-promise connection error?

@ApelSYN
Copy link
Member

ApelSYN commented Jun 27, 2018

node-mysql-query-promise works together with the config module.
To setup connection please create folder "config" in root of your project and put file "default.js" to config folder.
Example of default.js:

module.exports = {
    database: {
        'master': {
            host: 'localhost',
            user: 'username',
            password: 'password',
            port: '3306',
            database: 'database',
            connectionLimit: 10
        },
        'slave': {
            host: 'localhost',
            user: 'username',
            password: 'password',
            port: '3306',
            database: 'database',
            connectionLimit: 10
        }
    }
};

Edit this file for your settings.

Вот пример использования:

    var query = require('mysql-query-promise'),
        qs = 'SELECT * FROM foo WHERE bar=?';
        
    query(qs, ['baz'], 'master')
        .then(function(rows){
            /* Do what you need with query results here */
        }, function(err){
            /* Errors handler */
        });

Note that the function has 3 arguments:
The query, the list of escaped fields, and the third argument of the section name in the configuration file.

Edit your example:

// ...
const q = query(`SELECT * from ${tableName}`, [], 'master')
// ...

or

// ...
const q = query(`SELECT * from ?`, [tableName], 'master')
// ...

@sashsvamir
Copy link
Author

I know how to configure mysql-query-promise, this issue was opened because now on failed db connection nodejs has falled. And i can't find way to catch this errors.

@ApelSYN
Copy link
Member

ApelSYN commented Jun 27, 2018

Try:

try {
    let rows = await q
} catch (err) {
    console.log("Error!")
}

@sashsvamir
Copy link
Author

no, error still not catching!

@ApelSYN
Copy link
Member

ApelSYN commented Jun 27, 2018

Unfortunately I do not have the opportunity to quickly fix this problem, because the error occurs in the mysql-query module, which is written in the callback style. That's why koa can not catch this error.

We can fix this error in the following releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants