How to handle errors?
This section details error handling techniques in MySQL2. It covers essential error management strategies for methods such as createConnection
, createPool
, createPoolCluster
, execute
and query
.
Using callbacks
createConnection
This solution has been tested and confirmed as the correct answer.
Handling connection errors by adding an error event listener:
const mysql = require('mysql2');
connection = mysql.createConnection({
host: '',
user: '',
database: '',
});
connection.addListener('error', (err) => {
if (err instanceof Error) {
console.log(`createConnection error:`, err);
}
});
createPool
This solution has been tested.
Handling connection errors through callback's err
parameter:
const mysql = require('mysql2');
const pool = mysql.createPool({
host: '',
user: '',
database: '',
});
pool.getConnection((err, connection) => {
if (err instanceof Error) {
console.log('pool.getConnection error:', err);
return;
}
});
createPoolCluster
This solution has been tested.
Handling connection errors through callback's err
parameter:
const mysql = require('mysql2');
const poolCluster = mysql.createPoolCluster();
poolCluster.add('NodeI', {
host: '',
user: '',
database: '',
});
poolCluster.getConnection('NodeI', (err, connection) => {
if (err instanceof Error) {
console.log('poolCluster.getConnection error:', err);
return;
}
});
execute
This solution has been tested.
Handling execute
errors through callback's err
parameter:
connection.execute('SELEC 1 + 1', (err, rows) => {
if (err instanceof Error) {
console.log('execute error:', err);
return;
}
console.log(rows);
});
- It will work for both createConnection, createPool and createPoolCluster connections.
query
This solution has been tested.
Handling query
errors through callback's err
parameter:
connection.query('SELEC 1 + 1', (err, rows) => {
if (err instanceof Error) {
console.log('query error:', err);
return;
}
console.log(rows);
});
- It will work for both createConnection, createPool and createPoolCluster connections.
Using promises
createConnection
This solution has been tested and confirmed as the correct answer.
Handling connection errors through try-catch
block:
import mysql from 'mysql2/promise';
try {
const connection = await mysql.createConnection({
host: '',
user: '',
database: '',
});
} catch (err) {
if (err instanceof Error) {
console.log(err);
}
}
createPool
This solution has been tested.
Handling connection errors through try-catch
block:
import mysql from 'mysql2/promise';
const pool = mysql.createPool({
host: '',
user: '',
database: '',
});
try {
const connection = await pool.getConnection();
} catch (err) {
if (err instanceof Error) {
console.log(err);
}
}
createPoolCluster
This solution has been tested.
Handling connection errors through try-catch
block:
import mysql from 'mysql2/promise';
const poolCluster = mysql.createPoolCluster();
poolCluster.add('NodeI', {
host: '',
user: '',
database: '',
});
try {
await poolCluster.getConnection('NodeI');
} catch (err) {
if (err instanceof Error) {
console.log('createConnection error:', err);
}
}
execute
This solution has been tested.
Handling execute
errors through try-catch
block:
try {
const [rows] = await connection.execute('SELEC 1 + 1');
console.log(rows);
} catch (err) {
if (err instanceof Error) {
console.log('execute error:', err);
}
}
- It will work for both createConnection, createPool and createPoolCluster connections.
query
This solution has been tested.
Handling query
errors through try-catch
block:
try {
const [rows] = await connection.query('SELEC 1 + 1');
console.log(rows);
} catch (err) {
if (err instanceof Error) {
console.log('query error:', err);
}
}
- It will work for both createConnection, createPool and createPoolCluster connections.