CHAPTER 8
Error handling refers to the programming practice of anticipating and coding for error conditions that may arise when your program runs. In this chapter, we're going to learn how to handle errors in Node.js.
We cannot assure our code runs well. Because of it, we should think about how to handle errors. To do so, we can use try..catch syntax. Here is the error handling format in Node.js:
try{ var c = n/b; if(c==Infinity) throw new Error('this error is caused by invalid operation'); }catch (err){ console.log(err); } |
Let’s write this code.
var n = 3; var b = 0; try{ var c = n/b; if(c==Infinity) throw new Error('this error is caused by invalid operation'); }catch (err){ console.log(err); } |
You can see the n/b operation returns an Infinity value. In this situation, we can throw our error so it will be caught.
If you run this code, you will get the program output shown in Figure 47.

Figure 47: Catching an error in Node.js
In the previous section, we used console.log() to print an error message in the console. Imagine there are many messages in the console and we want to identify which error message is. The simple solution is to apply font color to the message to identify an error occurring.
Alternatively we can use log4js-node. To install this module, write this script:
sudo npm install log4js -g |
For Windows platform, you should run it under administrator level. You can do it using RunAs in console
npm install log4js -g |
If you don’t install it globally, you can execute as follows:
npm install log4js |
If it is installed successfully, you will get a response output as shown in Figure 48.

Figure 48: Installing log4js-node
How do we use this module? Let’s write the sample code:
var log4js = require('log4js'); var logger = log4js.getLogger(); logger.info('Application is running'); logger.warn('Module cannot be loaded'); logger.error('Saved data was error'); logger.fatal('Server could not process'); logger.debug("Some debug messages"); |
Save it into a file called logger-demo.js. Run the file:
node logger-demo.js |
You will see the program output as shown in Figure 49. You can see log4js-node display color text is based on log type.
You could change the display text (default) by application category. Just write the category on getLogger(), for instance, myapplication.
var log4js = require('log4js'); var logger = log4js.getLogger('myapplication'); logger.info('Application is running'); logger.warn('Module cannot be loaded'); logger.error('Saved data was error'); logger.fatal('Server could not process'); logger.debug("Some debug messages"); |
Run it again and you will get a new output response, shown in Figure 50.

Figure 49: Running application with log4js-node

Figure 50: Changing default value with own category name
By default, log4js-node writes message on console. If you want to write all messages in a file, you can configure a logging file. To get started:
For instance, we store the log file on c:\temp\myapplication.log with the category name myapplication. Here is a code illustration:
var log4js = require('log4js'); log4js.loadAppender('file'); log4js.addAppender(log4js.appenders.file('c:\temp\myapplication.log'), 'myapplication'); var logger = log4js.getLogger('myapplication'); logger.info('Application is running'); logger.warn('Module cannot be loaded.'); logger.error('Saved data was error'); logger.fatal('Server could not process'); logger.debug("Some debug messages"); |
Run this code. Then, you will get a log file, myapplication.log. If you open it, you will see the file content as follows:
[2012-10-26 21:43:36.531] [INFO] myapplication - Application is running [2012-10-26 21:43:36.546] [WARN] myapplication - Module cannot be loaded [2012-10-26 21:43:36.547] [ERROR] myapplication - Saved data was error [2012-10-26 21:43:36.547] [FATAL] myapplication - Server could not process [2012-10-26 21:43:36.548] [DEBUG] myapplication - Some debug messages |
For further information about log4js-node, visit https://github.com/nomiddlename/log4js-node.