The following is a method that I use to handle / write all possible exceptions in an Express application. Any other way?
Error handling this problem, especially when working with Express. Let's start with the simplest scenario. Imagine our application is as follows:
console.log(ciao.ciao);
Running the application from the console, we get the following:
console.log(ciao.ciao);
^
ReferenceError: ciao is not defined
Please note that in the above example, this is an example of un Uncaught Exception due to the fact that the code written by us was not executed due to an error. To handle the error above, we could do the following:
process.on('uncaughtException', function (err) {
console.log('error', ' * we did it, we handled the error * ', err.stack.trim());
process.exit(1);
});
console.log(ciao.ciao);
If we run the application, we get:
error * we did it, we handled the error * ReferenceError: ciao is not defined
Attention please!!! "process.on ('uncaughtException' ..." will be DEPRACATED !!!
, , , , winston. :
var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ handleExceptions: true }),
]
});
console.log(ciao.ciao);
:
error: uncaughtException: ciao is not defined date=Wed Oct 22 2014 16:26:53 GMT+0200 (ora legale Europa occidentale), pid=8052, uid=null, gid=null, cwd=c:\Projects\Eclipse\WEBSITE\node_taxiapplication, execPath=C:\Program Files\nodejs\node.exe, version=v0.10.26, argv=[node, c:\Projects\Eclipse\WEBSI
TE\node_taxiapplication\test2.js], rss=13479936, heapTotal=7130752, heapUsed=2760024, loadavg=[0, 0, 0],...
winston , , pid (rss, heapTotal,...). , winston - , .
Express Server
:
var winston = require('winston');
var express = require('express');
var app = express();
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ handleExceptions: true }),
]
});
app.get('/', function(req, res, next) {
console.log(ciao.ciao);
});
app.listen(3000);
http://localhost:3000 :
ReferenceError: ciao is not defined at Object.handle (c:\Projects\Eclipse\WEBSITE\node_taxiapplicatio...
, , -.
- Winston?
Winston , Express, , Express, :
function(req, res, next) {
console.log(ciao.ciao);
});
So winston is out of the game in case of error inside of an Express route handler
, , - , winston , , "" Express. Express error :
var winston = require('winston');
var express = require('express');
var app = express();
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ handleExceptions: true }),
]
});
app.get('/', function(req, res, next) {
console.log(ciao.ciao);
});
app.use(function(err, req, res, next) {
logger.log('error', ' * logged by winston * ', err.stack.trim());
process.exit(1);
});
app.listen(3000);
http://localhost:3000, , , , winston:
error: * logged by winston * ReferenceError: ciao is not defined
, , winston, , , Express.
, winston,
, ! . , Express , :
app.get('/', function(req, res, next) {
console.log(ciao.ciao);
});
, , , Express:
app.get('/', function(req, res, next) {
setTimeout(function () {
console.log(ciao.ciao);
});
});
:
var winston = require('winston');
var express = require('express');
var app = express();
var logger = new (winston.Logger)({
transports: [ new (winston.transports.Console)({ handleExceptions: true }) ]
});
app.get('/', function(req, res, next) {
setTimeout(function () {
console.log(ciao.ciao);
}, 1);
});
app.listen(3000);
http://localhost:3000, , :
error: uncaughtException: ciao is not defined date=Thu Oct 23 2014 09:52:01 GMT+0200 (ora legale Europa occidentale), pid=5952, uid=null, gid=null, cwd=c:\Projects\Eclipse\WEBSITE\node_taxiapplication, execPath=C:\Program Files\nodejs\node.exe, version=v0.10.26, argv=[node, c:\Projects\Eclipse\WEBSI
TE\node_taxiapplication\test2.js], rss=18325504, heapTotal=15453568, heapUsed=4883192,
Hej, Express , winston ( ). , Express, .
, Express . Hej , Express.
setTimeout ( 1 ) Express ( Express), setTimeout - , , , .
var winston = require('winston');
var express = require('express');
var app = express();
var logger = new (winston.Logger)({
transports: [ new (winston.transports.Console)({ handleExceptions: true }) ]
});
app.get('/', function(req, res, next) {
console.log(ciao.ciao);
});
app.use(function(err, req, res, next) {
res.send('Arrrghhh!!! The Server is at the moment down.');
setTimeout(function () {
throw new Error(err.stack.trim());
}, 1);
});
app.listen(3000);
http://localhost:3000, "Arrrhhh!!!...", :
error: uncaughtException: ReferenceError: ciao is not defined date=Thu Oct 23 2014 10:00:51 GMT+0200 (ora legale Europa occidentale), pid=5792, uid=null, gid=null, cwd=c:\Projects\Eclipse\WEBSITE\node_taxiapplication, execPath=C:\Program Files\nodejs\node.exe, version=v0.10.26, argv=[node, c:\Projec
ts\Eclipse\WEBSITE\node_taxiapplication\test2.js], rss=18489344, heapTotal=15453568, heapUsed=5115092,...
, / winston: , , .