Skip to main content

如何处理错误?

¥How to handle errors?

本节详细介绍了 MySQL2 中的错误处理技术。它涵盖了 createConnectioncreatePoolcreatePoolClusterexecutequery 等方法的基本错误管理策略。

¥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
2Stable

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
2Stable

This solution has been tested.

通过回调的 err 参数处理连接错误:

¥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
2Stable

This solution has been tested.

通过回调的 err 参数处理连接错误:

¥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
2Stable

This solution has been tested.

通过回调的 err 参数处理 execute 错误:

¥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);
});
  • 它适用于 createConnection、createPool 和 createPoolCluster 连接。

    ¥It will work for both createConnection, createPool and createPoolCluster connections.

query
2Stable

This solution has been tested.

通过回调的 err 参数处理 query 错误:

¥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);
});
  • 它适用于 createConnection、createPool 和 createPoolCluster 连接。

    ¥It will work for both createConnection, createPool and createPoolCluster connections.

使用 promise

¥Using promises

createConnection
2Stable

This solution has been tested and confirmed as the correct answer.

通过 try-catch 块处理连接错误:

¥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
2Stable

This solution has been tested.

通过 try-catch 块处理连接错误:

¥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
2Stable

This solution has been tested.

通过 try-catch 块处理连接错误:

¥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
2Stable

This solution has been tested.

通过 try-catch 块处理 execute 错误:

¥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);
}
}
  • 它适用于 createConnection、createPool 和 createPoolCluster 连接。

    ¥It will work for both createConnection, createPool and createPoolCluster connections.

query
2Stable

This solution has been tested.

通过 try-catch 块处理 query 错误:

¥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);
}
}
  • 它适用于 createConnection、createPool 和 createPoolCluster 连接。

    ¥It will work for both createConnection, createPool and createPoolCluster connections.


¥Related Links