Talking about node. js database Exception Handling, talking about node. js Exception Handling

Source: Internet
Author: User

Talking about node. js database Exception Handling, talking about node. js Exception Handling

This article describes how to handle node. js database exceptions:

NodeJs version: 4.4.4

Database Link error

It is the most troublesome to use nodejs to handle exceptions.domainAnd some third-party libraries. Database Operations are common functions. Through callback, we will see a lot of err here.

As follows:

var pool = require('../db.js');var runtimeLog = require('../log.js').getLogger('runlog');var Promise = require('bluebird');function queryPromise(queryString) {  return new Promise(function(resolve, reject) {    pool.getConnection(function(err, connection) {          //connection.query(queryString, function(err, rows, fields) {              // if (!err) {      //   resolve(rows);      //  } else {      //   runtimeLog.error(err)      //   reject(err)      //  }      // connection.release();      //});    })  })}module.exports = function() {  return new Promise(function(resolve, reject) {    queryPromise("select * from wb123_home_map GROUP BY onestair")      .then(function(results){        resolve(results);      })      .catch(function(err){        runtimeLog.error(err)      })  })}

We can see that errors may occur in the two callback locations.

This is a module. We have exported it.

Here, we intentionally enter the port number of the database link as 330666:

module.exports = {  host:   '192.168.6.101',  database: 'web123',  user:   'root',  password: 'passw0rd',  protocol: 'mysql',  port:   '330666',  query:  {pool: true}}

In this case, an error occurs when we connect to the database.

Here we use promise, so we use catch in the exported function, which will catch the error in the queryPromise function and print it to the log.

[2017-01-05 13:27:59.648] [ERROR] runlog - [err] [RangeError: port should be >= 0 and < 65536: 330666]RangeError: port should be >= 0 and < 65536: 330666...

Database SQL statement Error

Modify the code, correct the database link, and change the SQL statement to an incorrect statement.

var pool = require('../db.js');var runtimeLog = require('../log.js').getLogger('runlog');var Promise = require('bluebird');function queryPromise(queryString) {  return new Promise(function(resolve, reject) {    pool.getConnection(function(err, connection) {            connection.query(queryString, function(err, rows, fields) {        //if (err) throw err;        if (!err) {          resolve(rows);         } else {          reject(err)         }        connection.release();      });    })  })}module.exports = function() {  return new Promise(function(resolve, reject) {    queryPromise("select * from wb123_home_map GROUP BY onestairs")      .then(function(results){        resolve(results);      })      .catch(function(err){        runtimeLog.error('[err]',err)      })  })}

Error message:

[2017-01-05 14:40:14.518] [ERROR] runlog - [err] { [Error: ER_BAD_FIELD_ERROR: Unknown column 'onestairs' in 'group statement'] code: 'ER_BAD_FIELD_ERROR', errno: 1054, sqlState: '42S22', index: 0 }Error: ER_BAD_FIELD_ERROR: Unknown column 'onestairs' in 'group statement'  at Query.Sequence._packetToError (E:\zz\zz_wb123\manage\trunk\code\nod....

We can see that all errors are thrown through catch.

So if Promise is used, we can catch the exception thrown below in catch. An error occurred while logging in the queryPromise function.

Here is a very useful Q & A about stackoverflow error handling node-js-best-practice-exception-handling.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.