Quantcast
Channel: All About ASP.NET and ASP.NET Core 2 Hosting BLOG
Viewing all articles
Browse latest Browse all 427

Cheap Node.JS Hosting :: How to Solve Error Handling Callback in Node.JS

$
0
0

Error handling can be a drag, but it’s essential for the stability of your app. Naturally, I’m interested in ways to streamline the error handling process to make it as stable as it can be for the app whilst also being convenient for me to write. The async callback standard in Node.js suggests that the first parameter of the callback is an error object. If that's null, you can move along. If it's not, or you have an error thrown elsewhere, you have to figure out what to do. Let's take a look at our options!

Callback can carry on assuming the operation succeeded. Otherwise, it can deal with the error in an appropriate way, such as logging it along with any contextual information. It can then decide whether or not to carry on depending on the severity of the error or whether or not the resultant data is required to continue operation.

Let’s implement some error handling for our query error:

var log = console.log;
// We misspell 'SELECT' in this query so it fails
var query = 'SLECT 1 + 1';
con.query(query, function(err){
  if (err) return log("Query failed. Error: %s. Query: %s", err, query);
});

Our favourite library for asynchronous flow control is async. Both async.parallel and async.series accept a collection of operations, and if any of them pass an error to its callback, async will immediately invoke your completion callback with the error:

var async = require('async');
var log = console.log;
var op1 = function(cb) {
  // We misspell 'SELECT' in this query so it fails
  var query = 'SLECT 1 + 1';
  con.query(query, cb);
}

var op2 = function(cb) {
  // This query is fine
  con.query('SELECT 1 + 1', cb);
}

var ops = [op1, op2];

async.parallel(ops, function(err, results) {
  if (err) return log("Something went wrong in one of our ops. Err: %s", err);

  // Otherwise, process results
});


async.parallel will execute both op1 and op2 in parallel but if either or both fail it will invoke our completion callback with the error that occurred first.

Standard callbacks are all well and good when we’re following Node’s convention, but it’s a little bit laborious to check the result of every operation, and this can quickly get messy when there are many nested callbacks each with their own error handling code.


Best, Fast, and Reliable Node.js Web Hosting with ASPHostPortal.com

Business Ready Technology Solutions
We understand that IT isn’t a one-size-fits-all scenario. That’s why all of our enterprise hardware, software and cloud-based technology solutions can be customized to fit the needs of your business, while being flexible enough to grow as you grow.

Reliability - 99.9% uptime
That is the promise we make to you. Our proprietary software and expert system administrators monitor our servers 24 hours a day, 7 days a week.

Highly Secured
We are independent, and have our own infrastructure. Every application runs on HTTPS protocol by default.


Viewing all articles
Browse latest Browse all 427

Trending Articles